Skip to content

Commit

Permalink
feat: Just pass key in payload
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Feb 8, 2024
1 parent 48d11be commit b7b31fd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 73 deletions.
35 changes: 19 additions & 16 deletions editor.planx.uk/src/@planx/components/FileUpload/Public.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import ErrorWrapper from "ui/shared/ErrorWrapper";
import { array } from "yup";

import {
Condition,
FileList,
PASSPORT_REQUESTED_FILES_KEY,
RequestedFile,
} from "../FileUploadAndLabel/model";
import { PrivateFileUpload } from "../shared/PrivateFileUpload/PrivateFileUpload";
import { getPreviouslySubmittedData, makeData } from "../shared/utils";
Expand Down Expand Up @@ -55,16 +54,6 @@ const FileUpload: React.FC<Props> = (props) => {
const [slots, setSlots] = useState<FileUploadSlot[]>(recoveredSlots ?? []);
const [validationError, setValidationError] = useState<string>();

const existingRequestedFiles: RequestedFile[] =
useStore(
(state) => state.computePassport().data?.[PASSPORT_REQUESTED_FILES_KEY],
) || [];

const requestedFile: RequestedFile = {
fn: props.fn,
condition: Condition.AlwaysRequired,
};

const uploadedFiles = (slots: FileUploadSlot[]) =>
makeData(
props,
Expand All @@ -82,17 +71,31 @@ const FileUpload: React.FC<Props> = (props) => {
})),
);

const updatedRequestedFiles = () => {
const emptyFileList = { required: [], recommended: [], optional: [] };

const { required, recommended, optional }: FileList =
useStore.getState().computePassport().data?.[
PASSPORT_REQUESTED_FILES_KEY
] || emptyFileList;

return {
[PASSPORT_REQUESTED_FILES_KEY]: {
required: [...required, props.fn],
recommended,
optional,
},
};
};

const handleSubmit = () => {
slotsSchema
.validate(slots)
.then(() => {
props.handleSubmit({
data: {
...uploadedFiles(slots).data,
[PASSPORT_REQUESTED_FILES_KEY]: [
...existingRequestedFiles,
requestedFile,
],
...updatedRequestedFiles(),
},
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,23 +660,9 @@ describe("Submitting data", () => {
const requestedFiles = submitted?.data?._requestedFiles;

expect(requestedFiles).toBeDefined();
expect(requestedFiles).toHaveLength(3);
expect(requestedFiles).toEqual(
expect.arrayContaining([
expect.objectContaining({
fn: "roofPlan",
condition: Condition.AlwaysRequired,
}),
expect.objectContaining({
fn: "heritage",
condition: Condition.AlwaysRecommended,
}),
expect.objectContaining({
fn: "utilityBill",
condition: Condition.NotRequired,
}),
]),
);
expect(requestedFiles.required).toContain("roofPlan");
expect(requestedFiles.recommended).toContain("heritage");
expect(requestedFiles.optional).toContain("utilityBill");
});

it("appends to the list of existing requested files", async () => {
Expand All @@ -691,12 +677,11 @@ describe("Submitting data", () => {
filename: "file.jpg",
},
],
[PASSPORT_REQUESTED_FILES_KEY]: [
{
fn: "anotherFileType",
condition: Condition.AlwaysRequired,
},
],
[PASSPORT_REQUESTED_FILES_KEY]: {
required: ["anotherFileType"],
recommended: [],
optional: [],
},
},
},
};
Expand Down Expand Up @@ -750,28 +735,13 @@ describe("Submitting data", () => {
const submitted = handleSubmit.mock.calls[0][0];
const requestedFiles = submitted?.data?._requestedFiles;

expect(requestedFiles).toEqual(
expect.arrayContaining([
// Existing files from previous components
expect.objectContaining({
fn: "anotherFileType",
condition: Condition.AlwaysRequired,
}),
// Requested files from this component
expect.objectContaining({
fn: "roofPlan",
condition: Condition.AlwaysRequired,
}),
expect.objectContaining({
fn: "heritage",
condition: Condition.AlwaysRecommended,
}),
expect.objectContaining({
fn: "utilityBill",
condition: Condition.NotRequired,
}),
]),
);
// Existing files from previous components
expect(requestedFiles.required).toContain("anotherFileType");

// Requested files from this component
expect(requestedFiles.required).toContain("roofPlan");
expect(requestedFiles.recommended).toContain("heritage");
expect(requestedFiles.optional).toContain("utilityBill");
});
});

Expand Down
36 changes: 24 additions & 12 deletions editor.planx.uk/src/@planx/components/FileUploadAndLabel/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,37 +238,49 @@ const formatRequestedFiles = ({
const hasSlots = (userFile: UserFile): userFile is UserFileWithSlots =>
Boolean(userFile?.slots);

const getUpdatedRequestedFiles = (fileList: FileList) => {
const emptyFileList = { required: [], recommended: [], optional: [] };

const { required, recommended, optional }: FileList =
useStore.getState().computePassport().data?.[
PASSPORT_REQUESTED_FILES_KEY
] || emptyFileList;

return {
[PASSPORT_REQUESTED_FILES_KEY]: {
required: [...required, ...fileList.required.map(({ fn }) => fn)],
recommended: [
...recommended,
...fileList.recommended.map(({ fn }) => fn),
],
optional: [...optional, ...fileList.optional.map(({ fn }) => fn)],
},
};
};

/**
* Generate payload for FileUploadAndLabel breadcrumb
* Not responsible for validation - this happens at the component level
*/
export const generatePayload = (fileList: FileList): Store.userData => {
const newPassportData: Store.userData["data"] = {};

const requestedFiles = [
const uploadedFiles = [
...fileList.required,
...fileList.recommended,
...fileList.optional,
];

const uploadedFiles = requestedFiles.filter(hasSlots);
].filter(hasSlots);

uploadedFiles.forEach((userFile) => {
newPassportData[userFile.fn] = formatUserFiles(userFile);
});

const existingRequestedFiles: FileType[] =
useStore.getState().computePassport().data?.[
PASSPORT_REQUESTED_FILES_KEY
] || [];
const requestedFiles = getUpdatedRequestedFiles(fileList);

return {
data: {
...newPassportData,
[PASSPORT_REQUESTED_FILES_KEY]: [
...existingRequestedFiles,
...requestedFiles.map(formatRequestedFiles),
],
...requestedFiles,
},
};
};
Expand Down

0 comments on commit b7b31fd

Please sign in to comment.