Skip to content

Commit

Permalink
Simplify variant group distinction in submit handler
Browse files Browse the repository at this point in the history
  • Loading branch information
CarsonF committed Sep 27, 2023
1 parent 4064aeb commit 259a4e6
Showing 1 changed file with 25 additions and 43 deletions.
68 changes: 25 additions & 43 deletions src/scenes/ProgressReports/EditForm/Steps/Media/MediaStep.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,53 +49,35 @@ export const MediaStep: StepComponent = ({ report }) => {
return;
}

const newFileForExistingGroup =
values.newFile && values.newFile.length > 0
? values.newFile[0]
: undefined;
const newFileForNewGroup =
values.newVersion && values.newVersion.length > 0
? values.newVersion[0]
: undefined;
const newFile = newFileForExistingGroup || newFileForNewGroup;

if (!values.id) {
// newFile = a completely different image
// newVersion = an edited version of a previous image
// return if there is either no newFile or newVersion
if (
(!values.newFile || values.newFile.length === 0) &&
(!values.newVersion ||
values.newVersion.length === 0 ||
!latestVariantGroupId)
) {
console.log(values.newFile, values.newVersion);
return;
}
// logic for a newFile
if (values.newFile && values.newFile.length > 0) {
const [uploadedImageInfo, finalizeUpload] = await uploadFile(
values.newFile[0]
);
const input: UploadProgressReportMedia = {
reportId: report.id,
file: uploadedImageInfo!,
variant: values.variant.key,
};
await createMedia({
variables: { input },
}).then(...finalizeUpload.tap);
return;
}
if (latestVariantGroupId) {
// logic for newVersion
if (values.newVersion && values.newVersion.length > 0) {
const [uploadedImageInfo, finalizeUpload] = await uploadFile(
values.newVersion[0]
);
const input: UploadProgressReportMedia = {
reportId: report.id,
file: uploadedImageInfo!,
variant: values.variant.key,
variantGroup: latestVariantGroupId,
};
await createMedia({
variables: { input },
}).then(...finalizeUpload.tap);
}
if (!newFile) {
return;
}
const [uploadedImageInfo, finalizeUpload] = await uploadFile(newFile);
const input: UploadProgressReportMedia = {
reportId: report.id,
file: uploadedImageInfo,
variant: values.variant.key,
variantGroup: newFileForExistingGroup
? latestVariantGroupId
: undefined,
};
await createMedia({
variables: { input },
}).then(...finalizeUpload.tap);
return;
}

if (values.id) {
await updateMedia({
variables: {
Expand Down

0 comments on commit 259a4e6

Please sign in to comment.