Skip to content

Commit 49ac070

Browse files
fix(content): change in input validation to use react query instead of redux
1 parent c24cb0d commit 49ac070

9 files changed

Lines changed: 30 additions & 58 deletions

File tree

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/AnswerOption.jsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import * as hooks from './hooks';
1919
import { ProblemTypeKeys } from '../../../../../data/constants/problem';
2020
import ExpandableTextArea from '../../../../../sharedComponents/ExpandableTextArea';
2121
import { answerRangeFormatRegex } from '../../../data/OLXParser';
22+
import { useValidateInputBlock } from '../../../data/apiHooks';
2223

2324
const AnswerOption = ({
2425
answer,
@@ -28,12 +29,10 @@ const AnswerOption = ({
2829
const dispatch = useDispatch();
2930

3031
const problemType = useSelector(selectors.problem.problemType);
31-
const isNumericInputValid = useSelector(selectors.problem.isNumericInputValid);
3232
const images = useSelector(selectors.app.images);
3333
const isLibrary = useSelector(selectors.app.isLibrary);
3434
const learningContextId = useSelector(selectors.app.learningContextId);
3535
const blockId = useSelector(selectors.app.blockId);
36-
3736
const removeAnswer = hooks.removeAnswer({ answer, dispatch });
3837
const setAnswer = hooks.setAnswer({ answer, hasSingleAnswer, dispatch });
3938
const setAnswerTitle = hooks.setAnswerTitle({
@@ -45,6 +44,7 @@ const AnswerOption = ({
4544
const setSelectedFeedback = hooks.setSelectedFeedback({ answer, hasSingleAnswer, dispatch });
4645
const setUnselectedFeedback = hooks.setUnselectedFeedback({ answer, hasSingleAnswer, dispatch });
4746
const { isFeedbackVisible, toggleFeedback } = hooks.useFeedback(answer);
47+
const { data = { is_valid: true }, mutate } = useValidateInputBlock();
4848

4949
const staticRootUrl = isLibrary
5050
? `${getConfig().STUDIO_BASE_URL}/library_assets/blocks/${blockId}/`
@@ -72,18 +72,21 @@ const AnswerOption = ({
7272
}
7373
if (problemType !== ProblemTypeKeys.NUMERIC || !answer.isAnswerRange) {
7474
return (
75-
<Form.Group isInvalid={!isNumericInputValid}>
75+
<Form.Group isInvalid={!data?.is_valid ?? true}>
7676
<Form.Control
7777
as="textarea"
7878
className="answer-option-textarea text-gray-500 small"
7979
autoResize
8080
rows={1}
8181
value={answer.title}
82-
onChange={setAnswerTitle}
82+
onChange={(e) => {
83+
setAnswerTitle(e);
84+
mutate({ title: e.target.value });
85+
}}
8386
placeholder={intl.formatMessage(messages.answerTextboxPlaceholder)}
8487

8588
/>
86-
{!isNumericInputValid && (
89+
{(!data?.is_valid ?? true) && (
8790
<Form.Control.Feedback type="invalid">
8891
<FormattedMessage {...messages.answerNumericErrorText} />
8992
</Form.Control.Feedback>

src/editors/containers/ProblemEditor/components/EditProblemView/AnswerWidget/hooks.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { StrictDict } from '../../../../../utils';
55
// should be re-thought and cleaned up to avoid this pattern.
66
// eslint-disable-next-line import/no-self-import
77
import * as module from './hooks';
8-
import { actions, thunkActions } from '../../../../../data/redux';
8+
import { actions } from '../../../../../data/redux';
99
import { ProblemTypeKeys } from '../../../../../data/constants/problem';
1010
import { fetchEditorContent } from '../hooks';
1111

@@ -29,17 +29,6 @@ export const setAnswer = ({ answer, hasSingleAnswer, dispatch }) => (payload) =>
2929
dispatch(actions.problem.updateAnswer({ id: answer.id, hasSingleAnswer, ...payload }));
3030
};
3131

32-
export const validateInputBlock = ({
33-
title, dispatch,
34-
}) => {
35-
if (!title) {
36-
return;
37-
}
38-
dispatch(thunkActions.problem.validateBlockNumericInput({
39-
title,
40-
}));
41-
};
42-
4332
export const setAnswerTitle = ({
4433
answer,
4534
hasSingleAnswer,
@@ -54,11 +43,6 @@ export const setAnswerTitle = ({
5443
if (isDirty !== undefined) {
5544
dispatch(actions.problem.setDirty(isDirty));
5645
}
57-
58-
// For numeric problems, validate input on title change
59-
if (problemType === ProblemTypeKeys.NUMERIC) {
60-
validateInputBlock({ title, dispatch });
61-
}
6246
};
6347

6448
export const setSelectedFeedback = ({ answer, hasSingleAnswer, dispatch }) => (value) => {
@@ -129,5 +113,4 @@ export default {
129113
useFeedback,
130114
isSingleAnswerProblem,
131115
useAnswerContainer,
132-
validateInputBlock,
133116
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useMutation } from '@tanstack/react-query';
2+
import { getConfig } from '@edx/frontend-platform';
3+
import api from '@src/editors/data/services/cms/api';
4+
5+
const getApiBaseUrl = () => getConfig().STUDIO_BASE_URL;
6+
7+
export const useValidateInputBlock = () => useMutation({
8+
mutationFn: async ({ title }) => {
9+
try {
10+
const res = await api.validateBlockNumericInput({ studioEndpointUrl: `${getApiBaseUrl()}`, data: { formula: title } });
11+
return res.data;
12+
} catch (err) {
13+
return {
14+
is_valid: false,
15+
error: err.response?.data?.error ?? 'Unknown error',
16+
};
17+
}
18+
},
19+
});

src/editors/data/redux/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ export interface EditorState {
157157
rawOLX: string;
158158
rawMarkdown: string;
159159
problemType: null | ProblemType | AdvancedProblemType;
160-
isNumericInputValid: boolean;
161160
/**
162161
* Is the "markdown" editor currently active (as opposed to visual or advanced editors)
163162
* This is confusingly named, and different from `isMarkdownEditorEnabledForContext`

src/editors/data/redux/problem/reducers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const initialState: EditorState['problem'] = {
1313
rawMarkdown: '',
1414
isMarkdownEditorEnabled: false,
1515
problemType: null,
16-
isNumericInputValid: true,
1716
question: '',
1817
answers: [],
1918
correctAnswerCount: 0,

src/editors/data/redux/problem/selectors.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ export const simpleSelectors = {
1818
defaultSettings: mkSimpleSelector(problemData => problemData.defaultSettings),
1919
completeState: mkSimpleSelector(problemData => problemData),
2020
isDirty: mkSimpleSelector(problemData => problemData.isDirty),
21-
isNumericInputValid: mkSimpleSelector(problemData => problemData.isNumericInputValid),
2221
};
2322

2423
export default simpleSelectors;

src/editors/data/redux/thunkActions/problem.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,19 +138,6 @@ export const initializeProblem = (blockValue) => (dispatch, getState) => {
138138
}
139139
};
140140

141-
export const validateBlockNumericInput = ({ title, ...rest }) => (dispatch) => {
142-
dispatch(requests.validateNumericInput({
143-
title,
144-
...rest,
145-
onSuccess: (response) => {
146-
dispatch(actions.problem.updateField({ isNumericInputValid: response.data.is_valid }));
147-
},
148-
onFailure: () => {
149-
dispatch(actions.problem.updateField({ isNumericInputValid: false }));
150-
},
151-
}));
152-
};
153-
154141
export default {
155-
initializeProblem, switchEditor, switchToAdvancedEditor, fetchAdvancedSettings, validateBlockNumericInput,
142+
initializeProblem, switchEditor, switchToAdvancedEditor, fetchAdvancedSettings,
156143
};

src/editors/data/redux/thunkActions/requests.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -485,21 +485,6 @@ export const uploadVideo = ({ data, ...rest }) => (dispatch, getState) => {
485485
}));
486486
};
487487

488-
export const validateNumericInput = ({ title, ...rest }) => (dispatch, getState) => {
489-
dispatch(module.networkRequest({
490-
requestKey: RequestKeys.validateBlockNumericInput,
491-
promise: api.validateBlockNumericInput({
492-
blockId: selectors.app.blockId(getState()),
493-
blockType: selectors.app.blockType(getState()),
494-
learningContextId: selectors.app.learningContextId(getState()),
495-
data: { formula: title },
496-
studioEndpointUrl: selectors.app.studioEndpointUrl(getState()),
497-
title: selectors.app.blockTitle(getState()),
498-
}),
499-
...rest,
500-
}));
501-
};
502-
503488
export default StrictDict({
504489
fetchBlock,
505490
fetchStudioView,
@@ -522,5 +507,4 @@ export default StrictDict({
522507
fetchVideoFeatures,
523508
uploadVideo,
524509
getHandlerlUrl,
525-
validateNumericInput,
526510
});

src/editors/data/services/cms/api.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,9 @@ export const apiMethods = {
392392
),
393393
validateBlockNumericInput: ({
394394
studioEndpointUrl,
395-
blockId,
396395
data,
397396
}) => post(
398-
urls.validateNumericInputUrl({ studioEndpointUrl, blockId }),
397+
urls.validateNumericInputUrl({ studioEndpointUrl }),
399398
data,
400399
),
401400
};

0 commit comments

Comments
 (0)