Skip to content

Commit

Permalink
Use hook for isArchetype
Browse files Browse the repository at this point in the history
  • Loading branch information
ibolton336 committed Sep 18, 2023
1 parent 0d05aa1 commit efd2627
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
} from "@patternfly/react-core";
import AngleLeftIcon from "@patternfly/react-icons/dist/esm/icons/angle-left-icon";
import { Link } from "react-router-dom";
import { useTranslation } from "react-i18next";
import { Paths } from "@app/Paths";
import { ConditionalRender } from "@app/components/ConditionalRender";
import { AppPlaceholder } from "@app/components/AppPlaceholder";
Expand All @@ -25,6 +24,7 @@ import { Assessment, Questionnaire } from "@app/api/models";
import QuestionnaireSectionTabTitle from "./components/questionnaire-section-tab-title";
import { AxiosError } from "axios";
import { formatPath } from "@app/utils/utils";
import useIsArchetype from "@app/hooks/useIsArchetype";

export enum SummaryType {
Assessment = "Assessment",
Expand All @@ -44,8 +44,8 @@ const QuestionnaireSummary: React.FC<QuestionnaireSummaryProps> = ({
isFetching = false,
fetchError = null,
}) => {
const { t } = useTranslation();
const isArchetype = location.pathname.includes("/archetypes/");
const isArchetype = useIsArchetype();

const [activeSectionIndex, setActiveSectionIndex] = useState<"all" | number>(
"all"
);
Expand Down Expand Up @@ -84,27 +84,20 @@ const QuestionnaireSummary: React.FC<QuestionnaireSummaryProps> = ({
if (!summaryData) {
return <div>No data available.</div>;
}

const dynamicPath = isArchetype
? formatPath(Paths.archetypeAssessmentActions, {
archetypeId: (summaryData as Assessment)?.archetype?.id,
})
: formatPath(Paths.applicationAssessmentActions, {
applicationId: (summaryData as Assessment)?.application?.id,
});

const BreadcrumbPath =
summaryType === SummaryType.Assessment ? (
<Breadcrumb>
<BreadcrumbItem>
{isArchetype ? (
<Link
to={formatPath(Paths.archetypeAssessmentActions, {
archetypeId: (summaryData as Assessment)?.archetype?.id,
})}
>
Assessment
</Link>
) : (
<Link
to={formatPath(Paths.applicationAssessmentActions, {
applicationId: (summaryData as Assessment)?.application?.id,
})}
>
Assessment
</Link>
)}
<Link to={dynamicPath}>Assessment</Link>
</BreadcrumbItem>
<BreadcrumbItem to="#" isActive>
{summaryData?.name}
Expand Down
17 changes: 17 additions & 0 deletions client/src/app/hooks/useIsArchetype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";

const useIsArchetype = () => {
const location = useLocation();
const [isArchetype, setIsArchetype] = useState(
location.pathname.includes("/archetypes/")
);

useEffect(() => {
setIsArchetype(location.pathname.includes("/archetypes/"));
}, [location.pathname]);

return isArchetype;
};

export default useIsArchetype;
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import { AppPlaceholder } from "@app/components/AppPlaceholder";
import AssessmentActionsTable from "./components/assessment-actions-table";
import { useFetchArchetypeById } from "@app/queries/archetypes";
import { useFetchApplicationById } from "@app/queries/applications";
import useIsArchetype from "@app/hooks/useIsArchetype";

const AssessmentActions: React.FC = () => {
const { applicationId, archetypeId } = useParams<AssessmentActionsRoute>();
const isArchetype = location.pathname.includes("/archetypes/");
const isArchetype = useIsArchetype();

const { archetype } = useFetchArchetypeById(archetypeId);
const { application } = useFetchApplicationById(applicationId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useQueryClient } from "@tanstack/react-query";
import { TrashIcon } from "@patternfly/react-icons";
import { ConfirmDialog } from "@app/components/ConfirmDialog";
import { getAssessmentsByItemId } from "@app/api/rest";
import useIsArchetype from "@app/hooks/useIsArchetype";

enum AssessmentAction {
Take = "Take",
Expand All @@ -42,7 +43,7 @@ interface DynamicAssessmentActionsRowProps {
const DynamicAssessmentActionsRow: FunctionComponent<
DynamicAssessmentActionsRowProps
> = ({ questionnaire, application, archetype, assessment }) => {
const isArchetype = location.pathname.includes("/archetypes/");
const isArchetype = useIsArchetype();
const history = useHistory();
const { t } = useTranslation();
const queryClient = useQueryClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Paths } from "@app/Paths";
import { Application, Assessment } from "@app/api/models";
import { useFetchApplicationById } from "@app/queries/applications";
import { useFetchArchetypeById } from "@app/queries/archetypes";
import useIsArchetype from "@app/hooks/useIsArchetype";

export interface AssessmentPageHeaderProps {
assessment?: Assessment;
Expand All @@ -20,7 +21,7 @@ export const AssessmentPageHeader: React.FC<AssessmentPageHeaderProps> = ({
}) => {
const { t } = useTranslation();
const history = useHistory();
const isArchetype = location.pathname.includes("/archetypes/");
const isArchetype = useIsArchetype();

const { archetype } = useFetchArchetypeById(assessment?.archetype?.id);
const { application } = useFetchApplicationById(assessment?.application?.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { formatPath, getAxiosErrorMessage } from "@app/utils/utils";
import { Paths } from "@app/Paths";
import { yupResolver } from "@hookform/resolvers/yup";
import { AssessmentStakeholdersForm } from "../assessment-stakeholders-form/assessment-stakeholders-form";
import useIsArchetype from "@app/hooks/useIsArchetype";

export const SAVE_ACTION_KEY = "saveAction";

Expand Down Expand Up @@ -64,7 +65,7 @@ export const AssessmentWizard: React.FC<AssessmentWizardProps> = ({
assessment,
isOpen,
}) => {
const isArchetype = location.pathname.includes("/archetypes/");
const isArchetype = useIsArchetype();
const queryClient = useQueryClient();
const { questionnaires } = useFetchQuestionnaires();
const onHandleUpdateAssessmentSuccess = () => {
Expand Down

0 comments on commit efd2627

Please sign in to comment.