Skip to content

Commit 9683435

Browse files
committed
cleanup logic for wizard setup
1 parent 9ca852c commit 9683435

File tree

2 files changed

+71
-34
lines changed

2 files changed

+71
-34
lines changed

web-server/plugins/slycat-cca/js/components/CCAWizard.tsx

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,13 @@
44
import * as React from "react";
55
import { CCAModalContent } from "./CCAWizardContent";
66
import { CCAWizardSteps } from "./CCAWizardSteps";
7-
import { useCCAWizardFooter } from "./CCAWizardUtils";
8-
import { useAppDispatch, useAppSelector } from "./wizard-store/hooks";
97
import {
10-
resetCCAWizard,
11-
selectMid,
12-
selectPid,
13-
setMid,
14-
setPid,
15-
} from "./wizard-store/reducers/cCAWizardSlice";
16-
import client from "js/slycat-web-client";
8+
useCCAWizardFooter,
9+
useHandleClosingCallback,
10+
useHandleWizardSetup,
11+
} from "./CCAWizardUtils";
12+
import { useAppDispatch, useAppSelector } from "./wizard-store/hooks";
13+
import { selectMid, selectPid } from "./wizard-store/reducers/cCAWizardSlice";
1714

1815
interface CCAWizardParams {
1916
pid: string;
@@ -27,27 +24,13 @@ export const CCAWizard = (params: CCAWizardParams) => {
2724
const dispatch = useAppDispatch();
2825
const statePid = useAppSelector(selectPid);
2926
const stateMid = useAppSelector(selectMid);
27+
const handleClosingCallback = useHandleClosingCallback(setModalOpen, stateMid);
28+
const handleWizardSetup = useHandleWizardSetup(pid, statePid, stateMid, marking);
3029

3130
React.useEffect(() => {
3231
console.log("statePid, pid, selectMid", statePid, pid, stateMid);
3332
if (modalOpen) {
34-
if (!statePid) {
35-
dispatch(setPid(pid));
36-
}
37-
if (!stateMid && statePid) {
38-
// create the model on open so we have something to reference later
39-
client
40-
.post_project_models_fetch({
41-
pid: statePid,
42-
type: "cca",
43-
name: "",
44-
description: "",
45-
marking: marking ?? "",
46-
})
47-
.then((result) => {
48-
dispatch(setMid(result.id));
49-
});
50-
}
33+
handleWizardSetup()
5134
}
5235
}, [dispatch, pid, statePid, stateMid]);
5336

@@ -56,14 +39,7 @@ export const CCAWizard = (params: CCAWizardParams) => {
5639
key={"slycat-wizard"}
5740
// slycat-wizard is the standard wizard id from knockout
5841
modalId={"slycat-wizard"}
59-
closingCallBack={React.useCallback(() => {
60-
setModalOpen(false);
61-
if (stateMid) {
62-
console.log("delete");
63-
client.delete_model_fetch({ mid: stateMid });
64-
}
65-
dispatch(resetCCAWizard());
66-
}, [stateMid])}
42+
closingCallBack={handleClosingCallback}
6743
title={"New CCA Model"}
6844
footer={cCAWizardFooter}
6945
>

web-server/plugins/slycat-cca/js/components/CCAWizardUtils.tsx

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
import * as React from "react";
55
import { useAppDispatch, useAppSelector } from "./wizard-store/hooks";
66
import {
7+
resetCCAWizard,
78
selectDataLocation,
89
selectTab,
10+
setMid,
11+
setPid,
912
setTabName,
1013
TabNames,
1114
uploadFile,
1215
} from "./wizard-store/reducers/cCAWizardSlice";
16+
import client from "js/slycat-web-client";
1317

1418
export const useCCAWizardFooter = () => {
1519
const tabName = useAppSelector(selectTab);
@@ -53,3 +57,60 @@ export const useCCAWizardFooter = () => {
5357
);
5458
return React.useMemo(() => [backButton, nextButton], [tabName, dataLocation, dispatch]);
5559
};
60+
61+
/**
62+
* Function to handle setup for creating a cca model in the model wizard modal
63+
* @param pid project id
64+
* @param statePid redux project id
65+
* @param stateMid redux model id
66+
* @param marking initial marking for the model
67+
* @returns memoized () => void
68+
*/
69+
export const useHandleWizardSetup = (
70+
pid: string,
71+
statePid: string | undefined,
72+
stateMid: string | undefined,
73+
marking: string | undefined,
74+
) => {
75+
const dispatch = useAppDispatch();
76+
return React.useCallback(() => {
77+
if (!statePid) {
78+
dispatch(setPid(pid));
79+
}
80+
if (!stateMid && statePid) {
81+
// create the model on open so we have something to reference later
82+
client
83+
.post_project_models_fetch({
84+
pid: statePid,
85+
type: "cca",
86+
name: "",
87+
description: "",
88+
marking: marking ?? "",
89+
})
90+
.then((result) => {
91+
dispatch(setMid(result.id));
92+
});
93+
}
94+
}, [pid, statePid, stateMid, marking]);
95+
};
96+
97+
/**
98+
* Handle the cleanup for closing the cca wizard modal
99+
* @param setModalOpen function for setting local state for if the wizard is open
100+
* @param stateMid redux model id
101+
* @returns memoized () => void
102+
*/
103+
export const useHandleClosingCallback = (
104+
setModalOpen: (value: React.SetStateAction<boolean>) => void,
105+
stateMid: string | undefined,
106+
) => {
107+
const dispatch = useAppDispatch();
108+
return React.useCallback(() => {
109+
setModalOpen(false);
110+
if (stateMid) {
111+
console.log("delete");
112+
client.delete_model_fetch({ mid: stateMid });
113+
}
114+
dispatch(resetCCAWizard());
115+
}, [stateMid, setModalOpen]);
116+
};

0 commit comments

Comments
 (0)