-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProblemDisplay.tsx
More file actions
209 lines (188 loc) · 6.13 KB
/
ProblemDisplay.tsx
File metadata and controls
209 lines (188 loc) · 6.13 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import React, { useState } from 'react';
import { useHistory } from 'react-router-dom';
import styled from 'styled-components';
import MarkdownEditor from 'rich-markdown-editor';
import { Problem } from '../../../api/Problem';
import { PureTextInputTitle } from '../../core/Input';
import {
SmallButton,
InvertedSmallButton,
TextButton,
} from '../../core/Button';
import { SmallHeaderText } from '../../core/Text';
import Loading from '../../core/Loading';
import ErrorMessage from '../../core/Error';
import { FlexBareContainer, SettingsContainer } from '../../core/Container';
import { useAppSelector, useProblemEditable } from '../../../util/Hook';
import { ProblemHelpModal } from '../../core/HelpModal';
import OptionsPanel from './OptionsPanel';
import TestCaseEditor from './TestCaseEditor';
import { InlineIcon } from '../../core/Icon';
const defaultDescription: string = [
'Replace this line with a short description.',
'### Parameters',
'1. `name`: Replace this with a description of the parameter.',
'### Example Test Case',
'**Input**',
'`name`: `value`',
'**Output**',
'`value`',
'**Explanation**',
'Replace this line with an explanation of this example test case.',
].join('\n\n');
const MainContent = styled.div`
text-align: left;
padding: 20px;
flex: 6;
`;
const TopButtonsContainer = styled.div`
margin-left: auto;
`;
const SettingsContainerHighPadding = styled(SettingsContainer)`
padding: 3rem;
`;
const TitleDescriptionSeparator = styled.hr`
margin: 1rem 0;
border: none;
border-top: 1px solid ${({ theme }) => theme.colors.border};
`;
const StyledMarkdownEditor = styled(MarkdownEditor)`
p {
font-family: ${({ theme }) => theme.font};
}
// The specific list of attributes to have dark text color.
.ProseMirror > p, blockquote, h1, h2, h3, ul, ol, table {
color: ${({ theme }) => theme.colors.text};
}
`;
const TextButtonLink = styled(TextButton)`
color: ${({ theme }) => theme.colors.gray};
padding: 0;
margin-top: 30px;
text-decoration: underline;
text-align: left;
`;
const InlineProblemIcon = styled(InlineIcon)`
margin: 10px;
height: 1rem;
`;
type ProblemDisplayParams = {
problem: Problem,
actionText: string,
onClick: (newProblem: Problem) => void,
editMode: boolean,
};
function ProblemDisplay(props: ProblemDisplayParams) {
const {
problem, onClick, actionText, editMode,
} = props;
const history = useHistory();
const { firebaseUser } = useAppSelector((state) => state.account);
const problemEditable = useProblemEditable(firebaseUser, problem);
const [newProblem, setNewProblem] = useState<Problem>(problem);
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const [helpModal, setHelpModal] = useState<boolean>(false);
// Variable used to force refresh the editor.
const [refreshEditor, setRefreshEditor] = useState<number>(0);
// Handle updating of normal text fields
const handleChange = (e: any) => {
const { name, value } = e.target;
setNewProblem({
...newProblem,
[name]: value,
});
};
// Handle description change
const handleDescriptionChange = (value: string) => handleChange({ target: { name: 'description', value } });
return (
<>
<ProblemHelpModal
show={helpModal}
exitModal={() => setHelpModal(false)}
/>
<MainContent>
<FlexBareContainer>
<SmallHeaderText>Problem</SmallHeaderText>
<InlineProblemIcon
onClick={() => setHelpModal(true)}
>
help_outline
</InlineProblemIcon>
<TopButtonsContainer>
<InvertedSmallButton
onClick={() => {
if (JSON.stringify(problem) === JSON.stringify(newProblem)
// eslint-disable-next-line no-alert
|| window.confirm('Go back? Your unsaved changes will be lost.')) {
// Use the return link if available; otherwise, use dashboard.
if (history.action !== 'POP') {
history.goBack();
} else {
history.push('/');
}
}
}}
>
Back
</InvertedSmallButton>
{problemEditable ? (
<SmallButton
onClick={() => onClick(newProblem)}
>
{actionText}
</SmallButton>
) : null}
</TopButtonsContainer>
</FlexBareContainer>
<SettingsContainerHighPadding>
<PureTextInputTitle
placeholder="Write a nice title"
name="name"
value={newProblem.name}
onChange={handleChange}
readOnly={!problemEditable}
/>
<TitleDescriptionSeparator />
<StyledMarkdownEditor
key={refreshEditor}
placeholder="Write a nice description"
defaultValue={newProblem.description}
onChange={(getNewValue) => handleDescriptionChange(getNewValue())}
readOnly={!problemEditable}
/>
{
(newProblem.description.length === 0 || newProblem.description === '\\\n') ? (
<TextButtonLink
onClick={() => {
// Add default description and force refresh editor.
handleDescriptionChange(defaultDescription);
setRefreshEditor(refreshEditor + 1);
}}
>
Not sure what to write? Add a template description.
</TextButtonLink>
) : null
}
</SettingsContainerHighPadding>
{editMode && problemEditable
? (
<TestCaseEditor
newProblem={newProblem}
setNewProblem={setNewProblem}
/>
) : null}
{loading ? <Loading /> : null}
{error ? <ErrorMessage message={error} /> : null}
</MainContent>
<OptionsPanel
newProblem={newProblem}
setNewProblem={setNewProblem}
editMode={editMode}
setLoading={setLoading}
setError={setError}
/>
</>
);
}
export default ProblemDisplay;