|
4 | 4 | import * as React from "react";
|
5 | 5 | import { useAppDispatch, useAppSelector } from "./wizard-store/hooks";
|
6 | 6 | import {
|
| 7 | + resetCCAWizard, |
7 | 8 | selectDataLocation,
|
8 | 9 | selectTab,
|
| 10 | + setMid, |
| 11 | + setPid, |
9 | 12 | setTabName,
|
10 | 13 | TabNames,
|
11 | 14 | uploadFile,
|
12 | 15 | } from "./wizard-store/reducers/cCAWizardSlice";
|
| 16 | +import client from "js/slycat-web-client"; |
13 | 17 |
|
14 | 18 | export const useCCAWizardFooter = () => {
|
15 | 19 | const tabName = useAppSelector(selectTab);
|
@@ -53,3 +57,60 @@ export const useCCAWizardFooter = () => {
|
53 | 57 | );
|
54 | 58 | return React.useMemo(() => [backButton, nextButton], [tabName, dataLocation, dispatch]);
|
55 | 59 | };
|
| 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