-
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.
- Loading branch information
Showing
7 changed files
with
267 additions
and
237 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
106 changes: 97 additions & 9 deletions
106
src/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm.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
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
126 changes: 60 additions & 66 deletions
126
src/scenes/ProgressReports/EditForm/Steps/Media/MediaStep.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,84 +1,78 @@ | ||
import { useMutation } from '@apollo/client'; | ||
import { Box, Typography } from '@mui/material'; | ||
import { Fragment } from 'react'; | ||
import { useDropzone } from 'react-dropzone'; | ||
import { useUploadFileAsync } from '~/components/files/hooks'; | ||
import { DropOverlay } from '~/components/Upload/DropOverlay'; | ||
import { StepComponent } from '../step.types'; | ||
import { VariantAccordion } from '../VariantAccordion'; | ||
import { MediaInfoForm, MediaInfoFormProps } from './MediaInfoForm'; | ||
import { CreateMediaDocument, DeleteMediaDocument } from './MediaStep.graphql'; | ||
import { VariantMediaAccordion } from './VariantMediaAccordion'; | ||
import { FormProps } from "~/components/form"; | ||
import { MediaFormState } from "~/scenes/ProgressReports/EditForm/Steps/Media/MediaInfoForm"; | ||
|
||
// TODO find types for actual uploadVariant here | ||
export interface MediaVariantResponse { | ||
item: any[]; | ||
uploadVariant: any; | ||
} | ||
export const MediaStep: StepComponent = ({ report }) => { | ||
const [createMedia] = useMutation(CreateMediaDocument); | ||
const [deleteMedia] = useMutation(DeleteMediaDocument); | ||
const uploadFile = useUploadFileAsync(); | ||
|
||
const handleSubmit: FormProps<MediaFormState>["onSubmit"] = async (values) => { | ||
// use values.id to determine if we want to create, update, delete | ||
// delete = values.id + submitAction | ||
const [uploadedImageInfo, finalizeUpload] = await uploadFile(values); | ||
if (!uploadedImageInfo) { | ||
finalizeUpload.tap; | ||
return; | ||
const handleSubmit: MediaInfoFormProps['onSubmit'] = async (values) => { | ||
if (values.submitAction === 'delete') { | ||
if (!values.id) { | ||
return; | ||
} | ||
await deleteMedia({ | ||
variables: { | ||
deleteProgressReportMediaId: values.id, | ||
}, | ||
}); | ||
} | ||
await createMedia({ | ||
variables: { | ||
input: { | ||
file: uploadedImageInfo, | ||
reportId: report.id, | ||
variant: 'published', | ||
|
||
if (!values.id) { | ||
if (!values.newFile || values.newFile.length === 0) { | ||
return; | ||
} | ||
const [uploadedImageInfo, finalizeUpload] = await uploadFile( | ||
values.newFile[0] | ||
); | ||
await createMedia({ | ||
variables: { | ||
input: { | ||
reportId: report.id, | ||
file: uploadedImageInfo!, | ||
variant: values.variant.key, | ||
}, | ||
}, | ||
}, | ||
}).then(...finalizeUpload.tap); | ||
}).then(...finalizeUpload.tap); | ||
} | ||
}; | ||
|
||
const { | ||
getRootProps, | ||
getInputProps, | ||
isDragActive, | ||
open: openFileBrowser, | ||
} = useDropzone({ | ||
onDrop: handleFileUpload, | ||
noClick: true, | ||
noKeyboard: true, | ||
disabled: false, | ||
}); | ||
|
||
return ( | ||
<div {...getRootProps()}> | ||
<Box sx={{ maxWidth: 'md' }}> | ||
<Typography variant="h3" gutterBottom> | ||
Upload an image to go with your Report | ||
</Typography> | ||
{report.media.uploadableVariants | ||
.slice() | ||
.reverse() | ||
.map((variant) => { | ||
return ( | ||
<Fragment key={variant.key}> | ||
<VariantMediaAccordion | ||
deleteMedia={deleteMedia} | ||
openFileBrowser={openFileBrowser} | ||
response={{ | ||
item: report.media.items.filter( | ||
(item) => item.variant.key === variant.key | ||
), | ||
uploadVariant: variant, | ||
}} | ||
sensitivity={report.parent.sensitivity} | ||
initiallyOpen={false} | ||
/> | ||
</Fragment> | ||
); | ||
})} | ||
</Box> | ||
</div> | ||
<Box sx={{ maxWidth: 'md' }}> | ||
<Typography variant="h3" paragraph> | ||
Upload an image to go with your Report | ||
</Typography> | ||
{/* TODO merge lists */} | ||
{report.media.items.map((media) => ( | ||
<VariantAccordion variant={media.variant} key={media.id}> | ||
<MediaInfoForm | ||
variant={media.variant} | ||
sensitivity={report.sensitivity} | ||
existingMedia={media} | ||
onSubmit={handleSubmit} | ||
/> | ||
</VariantAccordion> | ||
))} | ||
{report.media.uploadableVariants | ||
.slice() | ||
.reverse() | ||
.map((variant) => { | ||
return ( | ||
<VariantAccordion variant={variant} key={variant.key}> | ||
<MediaInfoForm | ||
variant={variant} | ||
sensitivity={report.sensitivity} | ||
existingMedia={undefined} | ||
onSubmit={handleSubmit} | ||
/> | ||
</VariantAccordion> | ||
); | ||
})} | ||
</Box> | ||
); | ||
}; |
Oops, something went wrong.