-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Publication reports functionality
- Loading branch information
Showing
10 changed files
with
288 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 164 additions & 7 deletions
171
...icationModals/PublicationPackageReportUploadModal/PublicationPackageReportUploadModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,180 @@ | ||
import { FormikFileUpload } from '@pzh-ui/components' | ||
import { Form, Formik } from 'formik' | ||
import { | ||
Button, | ||
File, | ||
formatBytes, | ||
FormikFileUpload, | ||
Text, | ||
} from '@pzh-ui/components' | ||
import { TrashCan } from '@pzh-ui/icons' | ||
import { Form, Formik, FormikHelpers, FormikProps } from 'formik' | ||
import { useMemo } from 'react' | ||
|
||
import { | ||
usePublicationActReportsGet, | ||
usePublicationAnnouncementReportsGet, | ||
} from '@/api/fetchers' | ||
import { LoaderSpinner } from '@/components/Loader' | ||
import Modal from '@/components/Modal' | ||
import { useActions } from '@/components/Publications/PublicationPackages/components/actions' | ||
import useModalStore from '@/store/modalStore' | ||
|
||
import { ModalStateMap } from '../../types' | ||
|
||
const PublicationPackageReportUploadModal = () => { | ||
const handleSubmit = () => {} | ||
const setActiveModal = useModalStore(state => state.setActiveModal) | ||
const modalState = useModalStore( | ||
state => state.modalStates['publicationPackageReportUpload'] | ||
) as ModalStateMap['publicationPackageReportUpload'] | ||
|
||
const { uploadReports } = useActions(modalState || {}) | ||
|
||
const handleSubmit = ( | ||
payload: { uploaded_files: File[] }, | ||
helpers: FormikHelpers<{ uploaded_files: never[] }> | ||
) => { | ||
uploadReports | ||
.mutateAsync({ | ||
actPackageUuid: modalState.packageUUID, | ||
announcementPackageUuid: modalState.packageUUID, | ||
data: payload, | ||
}) | ||
.then(() => { | ||
helpers.setSubmitting(false) | ||
}) | ||
} | ||
|
||
return ( | ||
<Modal | ||
id="publicationPackageReportUpload" | ||
title="Upload rapporten" | ||
size="xl"> | ||
<Formik initialValues={{}} onSubmit={handleSubmit}> | ||
<Form> | ||
<FormikFileUpload name="uploaded_files" /> | ||
</Form> | ||
<Formik | ||
initialValues={{ | ||
uploaded_files: [], | ||
}} | ||
onSubmit={handleSubmit}> | ||
{props => <InnerForm {...props} />} | ||
</Formik> | ||
<div className="mt-12 flex justify-end"> | ||
<Button onPress={() => setActiveModal(null)}>Sluiten</Button> | ||
</div> | ||
</Modal> | ||
) | ||
} | ||
|
||
const InnerForm = <TData extends { uploaded_files: File[] }>({ | ||
values, | ||
setFieldValue, | ||
isSubmitting, | ||
}: FormikProps<TData>) => { | ||
const modalState = useModalStore( | ||
state => state.modalStates['publicationPackageReportUpload'] | ||
) as ModalStateMap['publicationPackageReportUpload'] | ||
|
||
const { data: actReports } = usePublicationActReportsGet( | ||
{ | ||
act_package_uuid: modalState.packageUUID, | ||
}, | ||
{ | ||
query: { | ||
enabled: modalState.publicationType === 'act', | ||
}, | ||
} | ||
) | ||
|
||
const { data: announcementReports } = usePublicationAnnouncementReportsGet( | ||
{ | ||
announcement_package_uuid: modalState.packageUUID, | ||
}, | ||
{ | ||
query: { | ||
enabled: modalState.publicationType === 'announcement', | ||
}, | ||
} | ||
) | ||
|
||
const reports = | ||
modalState.publicationType === 'act' ? actReports : announcementReports | ||
|
||
console.log(reports) | ||
|
||
const removeFile = (file: File) => () => { | ||
const newFiles = [...values.uploaded_files] | ||
newFiles.splice(newFiles.indexOf(file), 1) | ||
|
||
setFieldValue('uploaded_files', newFiles) | ||
} | ||
|
||
const count = useMemo( | ||
() => values.uploaded_files.length, | ||
[values.uploaded_files] | ||
) | ||
|
||
return ( | ||
<Form> | ||
<FormikFileUpload | ||
name="uploaded_files" | ||
hideDescription | ||
hideIcon | ||
hideSelectedFiles | ||
className="py-24" | ||
accept={{ '*': [] }} | ||
/> | ||
{!!values.uploaded_files.length && ( | ||
<div className="mt-6 rounded border border-pzh-gray-300 p-4"> | ||
<Text bold color="text-pzh-blue-500 mb-2"> | ||
Geselecteerde bestanden | ||
</Text> | ||
<ul className="flex flex-col gap-2"> | ||
{values.uploaded_files.map((file, index) => ( | ||
<li | ||
key={file.path || `file-${index}`} | ||
className="pzh-form-input overflow-hidden border-pzh-gray-200"> | ||
<div className="flex items-center justify-between gap-2 px-4"> | ||
<Text | ||
bold | ||
color="text-pzh-blue-500" | ||
className="truncate"> | ||
{file.path} | ||
</Text> | ||
<div className="flex items-center"> | ||
<Text | ||
as="span" | ||
size="s" | ||
color="text-pzh-gray-600" | ||
className="whitespace-nowrap"> | ||
{formatBytes(file.size)} | ||
</Text> | ||
<Button | ||
variant="default" | ||
onPress={removeFile(file)} | ||
aria-label="Verwijderen"> | ||
{!isSubmitting ? ( | ||
<TrashCan | ||
size={16} | ||
className="-mt-[2px] ml-4 text-pzh-red-500" | ||
/> | ||
) : ( | ||
<LoaderSpinner className="-mt-[2px] ml-4 text-pzh-blue-500" /> | ||
)} | ||
</Button> | ||
</div> | ||
</div> | ||
</li> | ||
))} | ||
</ul> | ||
<Button | ||
type="submit" | ||
variant="cta" | ||
className="mt-4" | ||
isDisabled={isSubmitting} | ||
isLoading={isSubmitting}> | ||
{count} {count > 1 ? 'Bestanden' : 'Bestand'} uploaden | ||
</Button> | ||
</div> | ||
)} | ||
</Form> | ||
) | ||
} | ||
|
||
export default PublicationPackageReportUploadModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.