Skip to content

Commit

Permalink
feat: Capture FileList to breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Feb 5, 2024
1 parent b180df9 commit cf28660
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Store } from "pages/FlowEditor/lib/store";
import { FileWithPath } from "react-dropzone";

import { FileUploadSlot } from "../FileUpload/Public";
import { TYPES } from "../types";
import {
mockFileList,
mockFileListManyTagsOneSlot,
Expand Down Expand Up @@ -419,6 +420,17 @@ describe("generatePayload function", () => {
expect(result.data?.fileFn?.[1].filename).toEqual("second.jpg");
expect(result.data?.fileFn?.[2].filename).toEqual("third.jpg");
});

it("captures the original file list in state", () => {
const result = generatePayload(mockFileList);

expect(result).toHaveProperty("state");
expect(result.state).toHaveProperty("requestedFiles");
expect(result.state?.requestedFiles).toMatchObject(mockFileList);

expect(result.state).toHaveProperty("type");
expect(result.state?.type).toEqual(TYPES.FileUploadAndLabel);
});
});

describe("getRecoveredData function", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { FileWithPath } from "react-dropzone";

import { FileUploadSlot } from "../FileUpload/Public";
import { MoreInformation, parseMoreInformation } from "../shared";
import { TYPES } from "../types";

/**
* Conditions which can apply to a rule
Expand Down Expand Up @@ -243,7 +244,13 @@ export const generatePayload = (fileList: FileList): Store.userData => {
newPassportData[userFile.fn] = formatUserFiles(userFile);
});

return { data: newPassportData };
return {
data: newPassportData,
state: {
requestedFiles: fileList,
type: TYPES.FileUploadAndLabel,
},
};
};

const getCachedSlotsFromPreviousData = (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { TYPES } from "@planx/components/types";

import { vanillaStore } from "../../store";
import { Store } from './../../store/index';

const { getState, setState } = vanillaStore;
const { record } = getState();

test("record() captures state in breadcrumbs", () => {
setState({
flow: {
_root: {
edges: ["a", "b"],
},
a: {
type: TYPES.Statement,
edges: ["c"],
},
b: {
type: TYPES.Statement,
},
c: {
type: TYPES.Response,
edges: ["d"],
},
d: {
type: TYPES.Statement,
edges: ["e", "f"],
},
e: { type: TYPES.Response },
f: { type: TYPES.Response },
},
});

const state: Store.NodeState = {
type: TYPES.Statement, someStateData: { hello: "world", number: 42, isLive: true, deepObject: { hello: "again" } }
};

const data: Store.userData = { data: { projectType: "extension" } };

record("a", { answers: ["c"], data, state });

// State captured in breadcrumbs
expect(getState().breadcrumbs).toEqual({
a: { answers: ["c"], auto: false, data, state },
});

// State not captured in passport
expect(getState().computePassport()).toEqual({ data });
});
9 changes: 9 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,21 @@ export declare namespace Store {
export type Store = Record<string | number | symbol, unknown>;
export type nodeId = string;
export type flow = Record<nodeId, node>;
/**
* State of node at time user visited it
* State will be stored to breadcrumbs, but not captured in passport
*/
export type NodeState = {
type: TYPES,
[key: string]: any,
};
export type userData = {
answers?: Array<string>;
data?: Record<string, any>;
auto?: boolean;
override?: Record<string, any>;
feedback?: string;
state?: NodeState;
};
export type breadcrumbs = Record<nodeId, userData>;
export type cachedBreadcrumbs = Record<nodeId, userData> | undefined;
Expand Down
2 changes: 2 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,13 @@ export const previewStore: StateCreator<
auto = false,
override,
feedback,
state,
} = userData;

const breadcrumb: Store.userData = { auto: Boolean(auto) };
if (answers?.length > 0) breadcrumb.answers = answers;
if (feedback) breadcrumb.feedback = feedback;
if (state) breadcrumb.state = state;

const filteredData = objectWithoutNullishValues(data);
if (Object.keys(filteredData).length > 0) breadcrumb.data = filteredData;
Expand Down
7 changes: 4 additions & 3 deletions editor.planx.uk/src/pages/Preview/Questions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ const Questions = ({ previewEnvironment }: QuestionsProps) => {
answers = [],
auto = false,
feedback = undefined,
state = undefined,
} = (() => {
try {
const { answers = [], data, auto, feedback } = userData as any;
return { answers: answers.filter(Boolean), data, auto, feedback };
const { answers = [], data, auto, feedback, state } = userData as any;
return { answers: answers.filter(Boolean), data, auto, feedback, state };
} catch (err) {
return {};
}
})();

record(id, { answers, data, auto, feedback });
record(id, { answers, data, auto, feedback, state });
};

const goBack = useCallback(() => {
Expand Down

0 comments on commit cf28660

Please sign in to comment.