|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useStore } from "@/data/store"; |
| 4 | +import { PageSection } from "@/modules"; |
| 5 | +import { useTranslations } from "next-intl"; |
| 6 | +import { useState } from "react"; |
| 7 | + |
| 8 | +import FormModal from "@/components/FormModal"; |
| 9 | +import useQueryAlerts from "@/hooks/useQueryAlerts"; |
| 10 | +import OrganisationsSubsidiaryEditForm from "@/modules/OrganisationsSubsidiariesEditForm"; |
| 11 | +import OrganisationsSubsidiariesTable from "@/modules/OrganisationsSubsidiariesTable"; |
| 12 | +import useMutationWithConfirmation from "@/queries/useMutationWithConfirmation"; |
| 13 | +import { Subsidiary } from "@/types/application"; |
| 14 | +import Button from "@mui/material/Button"; |
| 15 | +import { deleteSubsidiaryQuery } from "@/services/subsidiaries"; |
| 16 | +import useMutationUpdateSubsidiary from "../../queries/useMutationUpdateSubsidiary"; |
| 17 | + |
| 18 | +const NAMESPACE_TRANSLATION = "Organisations.Parents"; |
| 19 | + |
| 20 | +interface OrganisationsParentsProps { |
| 21 | + onDeleteSuccess?: () => void; |
| 22 | + onEditSuccess?: () => void; |
| 23 | +} |
| 24 | + |
| 25 | +export default function OrganisationsParents({ |
| 26 | + onDeleteSuccess, |
| 27 | + onEditSuccess, |
| 28 | +}: OrganisationsParentsProps) { |
| 29 | + const organisation = useStore(state => state.config.organisation); |
| 30 | + const t = useTranslations(NAMESPACE_TRANSLATION); |
| 31 | + const [activeParent, setActiveParent] = useState<Subsidiary>(); |
| 32 | + |
| 33 | + const { mutateAsync: mutateUpdateAysnc, ...restUpdateState } = |
| 34 | + useMutationUpdateSubsidiary(); |
| 35 | + |
| 36 | + const { showConfirm } = useMutationWithConfirmation(deleteSubsidiaryQuery(), { |
| 37 | + onSuccess: () => { |
| 38 | + onDeleteSuccess?.(); |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + useQueryAlerts(restUpdateState, { |
| 43 | + onSuccess: () => { |
| 44 | + setActiveParent(undefined); |
| 45 | + onEditSuccess?.(); |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + const parents = organisation?.subsidiaries?.filter(s => s.is_parent) ?? []; |
| 50 | + |
| 51 | + return ( |
| 52 | + <> |
| 53 | + <PageSection heading={t("heading")} description={t("description")}> |
| 54 | + <div> |
| 55 | + <OrganisationsSubsidiariesTable |
| 56 | + data={parents} |
| 57 | + t={t} |
| 58 | + isPaginated={false} |
| 59 | + onEdit={parent => setActiveParent(parent)} |
| 60 | + onDelete={parent => |
| 61 | + showConfirm({ |
| 62 | + params: { |
| 63 | + subsidiaryId: parent.id, |
| 64 | + organisationId: organisation.id, |
| 65 | + isParent: true, |
| 66 | + }, |
| 67 | + }) |
| 68 | + } |
| 69 | + /> |
| 70 | + </div> |
| 71 | + <Button |
| 72 | + variant="outlined" |
| 73 | + onClick={() => setActiveParent({})} |
| 74 | + sx={{ mt: 2 }}> |
| 75 | + {t("addParentButton")} |
| 76 | + </Button> |
| 77 | + </PageSection> |
| 78 | + <FormModal |
| 79 | + open={!!activeParent} |
| 80 | + heading={activeParent?.id ? t("edit") : t("add")}> |
| 81 | + <OrganisationsSubsidiaryEditForm |
| 82 | + t={t} |
| 83 | + mutateState={restUpdateState} |
| 84 | + defaultValues={activeParent} |
| 85 | + onCancel={() => setActiveParent(undefined)} |
| 86 | + onSubmit={(payload: Subsidiary | Partial<Subsidiary>) => { |
| 87 | + mutateUpdateAysnc({ |
| 88 | + payload: { ...payload, is_parent: true }, |
| 89 | + params: { |
| 90 | + organisationId: organisation.id, |
| 91 | + subsidiaryId: activeParent?.id, |
| 92 | + }, |
| 93 | + }); |
| 94 | + }} |
| 95 | + /> |
| 96 | + </FormModal> |
| 97 | + </> |
| 98 | + ); |
| 99 | +} |
0 commit comments