Skip to content

Commit

Permalink
Send only IDs for custom list manual update entries. (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdilauro authored Jan 18, 2025
1 parent a60099b commit c88bda3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
9 changes: 3 additions & 6 deletions src/reducers/__tests__/customListEditor-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2518,12 +2518,9 @@ describe("custom list editor reducer", () => {
expect(formData.get("id")).to.equal("123");
expect(formData.get("name")).to.equal("My New List");
expect(formData.get("collections")).to.equal("[1,2]");
expect(formData.get("entries")).to.equal(
'[{"id":"book2","title":"Little Women"},{"id":"book91","title":"Huckleberry Finn"}]'
);
expect(formData.get("deletedEntries")).to.equal(
'[{"id":"book90","title":"Wuthering Heights"}]'
);
// The form data entries should have only the ids of the appropriate entries.
expect(formData.get("entries")).to.equal('[{"id":"book2"}]');
expect(formData.get("deletedEntries")).to.equal('[{"id":"book90"}]');
});

it("should include an auto update query if the list is auto updating", () => {
Expand Down
76 changes: 48 additions & 28 deletions src/reducers/customListEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,48 +154,68 @@ export const buildAdvSearchQueryString = (
export const getCustomListEditorFormData = (
state: CustomListEditorState
): FormData => {
const data = new (window as any).FormData();

const { id, properties, entries, searchParams } = state;
const { name, collections, autoUpdate } = properties.current;

const data = new (window as any).FormData();
if (id) {
data.append("id", id);
}

const { name, collections, autoUpdate } = properties.current;

data.append("name", name);
data.append("collections", JSON.stringify(collections));

if (autoUpdate) {
data.append("auto_update", "true");

data.append(
"auto_update_query",
buildAdvSearchQueryString(searchParams.current, 0, false)
);

data.append(
"auto_update_facets",
buildSearchFacetString(searchParams.current)
);
updateCustomListEditorAutoUpdateFormData(data, searchParams.current);
} else {
const { baseline, current, removed } = entries;

data.append("entries", JSON.stringify(current));

const entriesById = baseline.reduce((ids, entry) => {
ids[entry.id] = entry;
updateCustomListEditorManualUpdateFormData(data, entries);
}

return ids;
}, {});
return data;
};

const deletedEntries = Object.keys(removed).map((id) => entriesById[id]);
/**
* Update the form data with manual update custom list properties.
* The data should include only the `id`s for each entry. Other properties
* are excluded.
*
* @param data The form data to update
* @param entries The baseline (initial), current, and removed item entries
*/
const updateCustomListEditorManualUpdateFormData = (
data: FormData,
{ baseline, current, removed }: CustomListEditorEntriesData
) => {
const baselineEntriesById = baseline.reduce((ids, entry) => {
ids[entry.id] = { id: entry.id };
return ids;
}, {});
const addedEntries = current
.filter((entry) => !baselineEntriesById[entry.id])
.map((entry) => ({ id: entry.id }));
const deletedEntries = Object.keys(removed).map(
(id) => baselineEntriesById[id]
);

data.append("deletedEntries", JSON.stringify(deletedEntries));
}
data.append("entries", JSON.stringify(addedEntries));
data.append("deletedEntries", JSON.stringify(deletedEntries));
};

return data;
/**
* Update the form data with auto update search properties.
*
* @param data The form data to update
* @param currentParams The current search parameters
*/
const updateCustomListEditorAutoUpdateFormData = (
data: FormData,
currentParams: CustomListEditorSearchParams
) => {
data.append("auto_update", "true");
data.append(
"auto_update_query",
buildAdvSearchQueryString(currentParams, 0, false)
);
data.append("auto_update_facets", buildSearchFacetString(currentParams));
};

/**
Expand Down

0 comments on commit c88bda3

Please sign in to comment.