Skip to content

Commit c263518

Browse files
authored
[CharInv] Have find-replace auto-save would-be-lost changes (#3937)
1 parent 620aff8 commit c263518

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

src/goals/CharacterInventory/CharInv/CharacterDetail/FindAndReplace.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum FindAndReplaceId {
1717
}
1818

1919
interface FindAndReplaceProps {
20+
close: () => void;
2021
initialFindValue: string;
2122
}
2223

@@ -47,6 +48,7 @@ export default function FindAndReplace(
4748
)
4849
);
4950
setWarningDialogOpen(false);
51+
props.close();
5052
};
5153

5254
const dialogText = (

src/goals/CharacterInventory/CharInv/CharacterDetail/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ export default function CharacterDetail(
4949
<CharacterWords character={props.character} />
5050
</Grid2>
5151
<Grid2 size={12}>
52-
<FindAndReplace initialFindValue={props.character} />
52+
<FindAndReplace
53+
close={() => props.close()}
54+
initialFindValue={props.character}
55+
/>
5356
</Grid2>
5457
</Grid2>
5558
);

src/goals/CharacterInventory/CharInv/CharacterEntry.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
exit,
1818
setRejectedCharacters,
1919
setValidCharacters,
20-
uploadInventory,
20+
uploadAndExit,
2121
} from "goals/CharacterInventory/Redux/CharacterInventoryActions";
2222
import { useAppDispatch, useAppSelector } from "rootRedux/hooks";
2323
import { type StoreState } from "rootRedux/types";
@@ -51,7 +51,7 @@ export default function CharacterEntry(): ReactElement {
5151

5252
const save = async (): Promise<void> => {
5353
setSaveInProgress(true);
54-
await dispatch(uploadInventory());
54+
await dispatch(uploadAndExit());
5555
};
5656

5757
return (

src/goals/CharacterInventory/CharInv/tests/index.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { defaultState } from "rootRedux/types";
1515
jest.mock("goals/CharacterInventory/Redux/CharacterInventoryActions", () => ({
1616
exit: () => mockExit(),
1717
loadCharInvData: () => mockLoadCharInvData(),
18-
uploadInventory: () => mockUploadInventory(),
18+
uploadAndExit: () => mockUploadAndExit(),
1919
}));
2020
jest.mock("rootRedux/hooks", () => {
2121
return {
@@ -26,7 +26,7 @@ jest.mock("rootRedux/hooks", () => {
2626

2727
const mockExit = jest.fn();
2828
const mockLoadCharInvData = jest.fn();
29-
const mockUploadInventory = jest.fn();
29+
const mockUploadAndExit = jest.fn();
3030

3131
async function renderCharInvCreation(): Promise<void> {
3232
await act(async () => {
@@ -49,9 +49,9 @@ describe("CharInv", () => {
4949
});
5050

5151
it("saves inventory on save", async () => {
52-
expect(mockUploadInventory).toHaveBeenCalledTimes(0);
52+
expect(mockUploadAndExit).toHaveBeenCalledTimes(0);
5353
await userEvent.click(screen.getByTestId(CharInvCancelSaveIds.ButtonSave));
54-
expect(mockUploadInventory).toHaveBeenCalledTimes(1);
54+
expect(mockUploadAndExit).toHaveBeenCalledTimes(1);
5555
});
5656

5757
it("opens a dialogue on cancel, closes on no", async () => {

src/goals/CharacterInventory/Redux/CharacterInventoryActions.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,6 @@ export function uploadInventory() {
109109
const project = getState().currentProjectState.project;
110110
const charChanges = getCharChanges(project, charInvState);
111111
if (!charChanges.length) {
112-
exit();
113112
return;
114113
}
115114
await dispatch(
@@ -122,6 +121,12 @@ export function uploadInventory() {
122121
const changes = getState().goalsState.currentGoal.changes as CharInvChanges;
123122
dispatch(addCharInvChangesToGoal({ ...changes, charChanges }));
124123
await dispatch(asyncUpdateGoal());
124+
};
125+
}
126+
127+
export function uploadAndExit() {
128+
return async (dispatch: StoreStateDispatch) => {
129+
await dispatch(uploadInventory());
125130
exit();
126131
};
127132
}
@@ -188,6 +193,9 @@ function addWordChanges(wordChanges: FindAndReplaceChange) {
188193
* - Update the in-state character inventory. */
189194
export function findAndReplace(find: string, replace: string) {
190195
return async (dispatch: StoreStateDispatch) => {
196+
// First save pending inventory changes so they won't be lost.
197+
await dispatch(uploadInventory());
198+
191199
const changedWords = (await getFrontierWords()).filter((w) =>
192200
w.vernacular.includes(find)
193201
);

0 commit comments

Comments
 (0)