forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
150 lines (133 loc) · 4.95 KB
/
index.jsx
File metadata and controls
150 lines (133 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import React, { useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { useIntl, FormattedMessage } from '@edx/frontend-platform/i18n';
import {
Container,
Button,
AlertModal,
ActionRow,
} from '@openedx/paragon';
import PropTypes from 'prop-types';
import { useEditorContext } from '@src/editors/EditorContext';
import AnswerWidget from './AnswerWidget';
import SettingsWidget from './SettingsWidget';
import QuestionWidget from './QuestionWidget';
import EditorContainer from '../../../EditorContainer';
import RawEditor from '../../../../sharedComponents/RawEditor';
import { ProblemTypeKeys } from '../../../../data/constants/problem';
import {
checkIfEditorsDirty, parseState, saveWarningModalToggle, getContent,
} from './hooks';
import './index.scss';
import messages from './messages';
import ExplanationWidget from './ExplanationWidget';
import { saveBlock } from '../../../../hooks';
import { selectors } from '../../../../data/redux';
const EditProblemView = ({ returnFunction }) => {
const intl = useIntl();
const dispatch = useDispatch();
const editorRef = useRef(null);
const analytics = useSelector(selectors.app.analytics);
const lmsEndpointUrl = useSelector(selectors.app.lmsEndpointUrl);
const returnUrl = useSelector(selectors.app.returnUrl);
const problemType = useSelector(selectors.problem.problemType);
const problemState = useSelector(selectors.problem.completeState);
const isDirty = useSelector(selectors.problem.isDirty);
const isMarkdownEditorEnabledSelector = useSelector(selectors.problem.isMarkdownEditorEnabled);
const { isMarkdownEditorEnabledForContext } = useEditorContext();
const isMarkdownEditorEnabled = isMarkdownEditorEnabledSelector && isMarkdownEditorEnabledForContext;
const isAdvancedProblemType = problemType === ProblemTypeKeys.ADVANCED;
const { isSaveWarningModalOpen, openSaveWarningModal, closeSaveWarningModal } = saveWarningModalToggle();
const checkIfDirty = () => {
if (isAdvancedProblemType && editorRef?.current) {
return editorRef.current.observer?.lastChange !== 0;
}
return isDirty || checkIfEditorsDirty();
};
return (
<EditorContainer
getContent={() => getContent({
problemState,
openSaveWarningModal,
isAdvancedProblemType,
isMarkdownEditorEnabled,
editorRef,
lmsEndpointUrl,
})}
isDirty={checkIfDirty}
returnFunction={returnFunction}
>
<AlertModal
title={isAdvancedProblemType
? intl.formatMessage(messages.olxSettingDiscrepancyTitle)
: intl.formatMessage(messages.noAnswerTitle)}
isOpen={isSaveWarningModalOpen}
onClose={closeSaveWarningModal}
footerNode={(
<ActionRow>
<Button variant="tertiary" onClick={closeSaveWarningModal}>
<FormattedMessage {...messages.saveWarningModalCancelButtonLabel} />
</Button>
<Button
onClick={() => saveBlock({
content: parseState({
problem: problemState,
isAdvanced: isAdvancedProblemType,
isMarkdown: isMarkdownEditorEnabled,
ref: editorRef,
lmsEndpointUrl,
})(),
returnFunction,
destination: returnUrl,
dispatch,
analytics,
})}
>
<FormattedMessage {...messages.saveWarningModalSaveButtonLabel} />
</Button>
</ActionRow>
)}
>
{isAdvancedProblemType ? (
<FormattedMessage {...messages.olxSettingDiscrepancyBodyExplanation} />
) : (
<>
<div>
<FormattedMessage {...messages.saveWarningModalBodyQuestion} />
</div>
<div>
<FormattedMessage {...messages.noAnswerBodyExplanation} />
</div>
</>
)}
</AlertModal>
<div className="editProblemView d-flex flex-row flex-nowrap justify-content-end">
{isAdvancedProblemType || isMarkdownEditorEnabled ? (
<Container fluid className="advancedEditorTopMargin p-0">
<RawEditor
editorRef={editorRef}
lang={isMarkdownEditorEnabled ? 'markdown' : 'xml'}
content={isMarkdownEditorEnabled ? problemState.rawMarkdown : problemState.rawOLX}
/>
</Container>
) : (
<span className="flex-grow-1 mb-5">
<QuestionWidget />
<ExplanationWidget />
<AnswerWidget problemType={problemType} />
</span>
)}
<span className="editProblemView-settingsColumn">
<SettingsWidget problemType={problemType} editorRef={editorRef} />
</span>
</div>
</EditorContainer>
);
};
EditProblemView.defaultProps = {
returnFunction: null,
};
EditProblemView.propTypes = {
returnFunction: PropTypes.func,
};
export default EditProblemView;