From f9f26b3f40e7cc32f8fd32ab909c373ebd7c0f82 Mon Sep 17 00:00:00 2001 From: Ben Warzeski Date: Thu, 16 Nov 2023 02:11:56 +0000 Subject: [PATCH] feat: action mock toggles --- src/data/services/lms/hooks/actions/index.ts | 94 +++++++++---------- .../services/lms/hooks/selectors/oraConfig.ts | 7 -- src/hooks/assessment.js | 1 - 3 files changed, 45 insertions(+), 57 deletions(-) diff --git a/src/data/services/lms/hooks/actions/index.ts b/src/data/services/lms/hooks/actions/index.ts index a22b5bce..44d01332 100644 --- a/src/data/services/lms/hooks/actions/index.ts +++ b/src/data/services/lms/hooks/actions/index.ts @@ -1,3 +1,4 @@ +import React from 'react'; import { useQueryClient, useMutation } from '@tanstack/react-query'; import { camelCaseObject } from '@edx/frontend-platform'; @@ -6,70 +7,65 @@ import { stepNames, stepRoutes, queryKeys } from 'constants'; import { progressKeys } from 'constants/mockData'; import * as api from 'data/services/lms/api'; -import { AssessmentData } from 'data/services/lms/types'; +// import { AssessmentData } from 'data/services/lms/types'; import { loadState } from 'data/services/lms/fakeData/dataStates'; +import { useTestDataPath } from 'hooks/test'; import { useViewStep } from 'hooks/routing'; -import { useCreateMutationAction } from './utils'; export * from './files'; -export const useSubmitAssessment = ({ onSuccess }) => useMutation({ - mutationFn: (data: AssessmentData) => ( - api.submitAssessment(data).then((response) => { - console.log({ submitAssessmentResponse: response }); - return response; - }) - ), - onSuccess, +const apiLog = (apiMethod, name) => (data) => apiMethod(data).then(response => { + console.log({ [name]: response }); + return response; }); -export const useSubmitResponse = () => { +export const useSubmitAssessment = ({ onSuccess } = {}) => { + const testDataPath = useTestDataPath(); + const apiFn = api.submitAssessment; + const mockFn = React.useCallback((data) => Promise.resolve(data), []); + return useMutation({ + mutationFn: apiLog(testDataPath ? mockFn : apiFn, 'submittedAssessment'), + onSuccess, + }); +}; + +export const useSubmitResponse = ({ onSuccess } = {}) => { + const testDataPath = useTestDataPath(); const step = useViewStep(); const queryClient = useQueryClient(); + + const mockFn = React.useCallback(() => { + const state = camelCaseObject(loadState({ + view: stepRoutes[step], + progressKey: progressKeys.studentTraining, + })); + queryClient.setQueryData([queryKeys.pageData], state); + return Promise.resolve(state); + }, [queryClient, step]); + const apiFn = api.submitAssessment; + return useMutation({ - mutationFn: () => { - const state = camelCaseObject(loadState({ - view: stepRoutes[step], - progressKey: progressKeys.studentTraining, - })); - console.log({ state }); - queryClient.setQueryData([queryKeys.pageData], state); - return Promise.resolve(state); - }, + mutationFn: apiLog(testDataPath ? mockFn : apiFn, 'submittedResponse'), + onSuccess, }); - /* - return useCreateMutationAction( - async (data: any, queryClient) => { - // TODO: submit response - await api.submitResponse(data); - queryClient.invalidateQueries([queryKeys.pageData, false]); - }, - ); - */ }; -export const useSaveResponse = () => useCreateMutationAction( - async (data: any, queryClient) => { - // TODO: save response for later - await api.saveResponse(data); - // queryClient.invalidateQueries([queryKeys.pageData, false]); - return Promise.resolve(data); - }, -); +export const useSaveDraftResponse = ({ onSuccess } = {}) => { + // const queryClient = useQueryClient(); + const testDataPath = useTestDataPath(); + + const apiFn = apiLog(api.saveResponse, 'saveDraftResponse'); + + const mockFn = React.useCallback((data) => Promise.resolve(data), []); + + return useMutation({ + mutationFn: apiLog(testDataPath ? mockFn : apiFn, 'savedDraftResponse'), + onSuccess, + }); +}; export const useRefreshPageData = () => { const queryClient = useQueryClient(); - const step = useViewStep(); - /* - return () => { - queryClient.invalidateQueries({ queryKey: queryKeys.pageData }); - console.log("invalidated") - }; - */ - /* Test facilitation */ - return () => { - console.log("REFRESH"); - return queryClient.invalidateQueries({ queryKey: queryKeys.pageData }); - }; + return () => queryClient.invalidateQueries({ queryKey: queryKeys.pageData }); }; diff --git a/src/data/services/lms/hooks/selectors/oraConfig.ts b/src/data/services/lms/hooks/selectors/oraConfig.ts index 8f655e7a..15bee7f8 100644 --- a/src/data/services/lms/hooks/selectors/oraConfig.ts +++ b/src/data/services/lms/hooks/selectors/oraConfig.ts @@ -46,13 +46,6 @@ export const useEffectiveGradeStep = () => { const order = useAssessmentStepOrder(); return order[order.length - 1]; }; -export const useFinalStep = () => { - const steps = useAssessmentStepOrder().filter(step => step !== stepNames.staff); - if (steps.length) { - return steps[steps.length - 1]; - } - return stepNames.submission; -}; export const useRubricConfig = (): types.RubricConfig => useORAConfigData().rubricConfig; export const useEmptyRubric = () => { diff --git a/src/hooks/assessment.js b/src/hooks/assessment.js index 1a6de676..9dd94cc9 100644 --- a/src/hooks/assessment.js +++ b/src/hooks/assessment.js @@ -72,7 +72,6 @@ export const useCheckTrainingSelection = () => { export const useInitializeAssessment = () => { const emptyRubric = lmsSelectors.useEmptyRubric(); const setFormFields = reduxHooks.useSetFormFields(); - const setHasSubmitted = reduxHooks.useSetHasSubmitted(); return React.useCallback(() => { setFormFields(emptyRubric); }, []);