-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrating-criteria-settings.tsx
More file actions
184 lines (166 loc) · 5.25 KB
/
rating-criteria-settings.tsx
File metadata and controls
184 lines (166 loc) · 5.25 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
import * as React from "react";
import { useState, useEffect, useCallback } from "react";
import type { CriterionDTO, SettingsDTO } from "../../../api/types/dto";
import { Spacer } from "../../base/flex";
import { SettingsSection } from "../settings-section";
import { Subsubheading } from "../../base/headings";
import { Button } from "../../base/button";
import { TextField, Switch, FormControlLabel, Stack } from "@mui/material";
import { api } from "../../../hooks/use-api";
import { useNotificationContext } from "../../../contexts/notification-context";
// TODO Seems more maintainable to me if the save button is fine-grained
// and not global. Remove the global Save button and add one individually for
// each SettingsSection component for consistency.
interface ICriterionEditorProps {
criterion: CriterionDTO;
onSave: (criterion: CriterionDTO) => Promise<void>;
onDelete: (criterion: CriterionDTO) => Promise<void>;
}
/**
* Edit or delete a single criteria.
*/
const CriterionEditor = React.memo(
({ criterion, onSave, onDelete }: ICriterionEditorProps) => {
// Use react.memo to avoid rerendering the component every time a character is typed
const [title, setTitle] = useState(criterion.title);
const [description, setDescription] = useState(criterion.description);
const validateAndSave = (changedCriterion: CriterionDTO) => {
if (changedCriterion.title.length === 0) {
return;
}
if (changedCriterion.description.length === 0) {
return;
}
onSave(changedCriterion);
};
return (
<Stack direction={{ sm: "column", md: "row" }} spacing={{ xs: 1, sm: 2 }}>
<TextField
error={title.length === 0}
value={title}
onChange={(event) => setTitle(event.target.value)}
placeholder="Title"
rows={1}
/>
<TextField
error={description.length === 0}
value={description}
onChange={(event) => setDescription(event.target.value)}
placeholder="Description"
rows={1}
sx={{ flex: 1 }}
/>
<Button
onClick={() => validateAndSave({ ...criterion, title, description })}
>
Save
</Button>
<Button onClick={() => onDelete(criterion)}>Delete</Button>
</Stack>
);
},
);
/**
* A component to edit criteria for rating projects.
*/
export const ProjectProjectSettings = () => {
// Load all criteria and render them
const [allCriteria, setAllCriteria] = useState<CriterionDTO[]>([]);
const [settings, setSettings] = useState<Partial<SettingsDTO>>({});
const { showNotification } = useNotificationContext();
// Do this only on mount
useEffect(() => {
api.getAllCriteria().then((criteria) => {
setAllCriteria([...criteria]);
});
api.getSettings().then((settings_) => {
setSettings(settings_);
});
}, []);
useEffect(() => {
// Only update if settings are loaded
if (settings.rating) {
api.updateSettings(settings as SettingsDTO);
}
}, [settings]);
const addCriterion = useCallback(async (): Promise<void> => {
const newCriterion = await api.createCriterion({
title: "",
description: "",
} as unknown as CriterionDTO);
setAllCriteria((prev) => [...prev, newCriterion]);
}, []);
const updateCriterion = useCallback(
async (changedCriterion: CriterionDTO): Promise<void> => {
await api.updateCriterion(changedCriterion.id, changedCriterion);
setAllCriteria((prev) =>
prev.map((criterion) => {
return criterion.id === changedCriterion.id
? changedCriterion
: criterion;
}),
);
showNotification("Saved criterion");
},
[],
);
const deleteCriterion = useCallback(
async (deletedCriterion: CriterionDTO): Promise<void> => {
await api.deleteCriterion(deletedCriterion.id);
setAllCriteria((prev) =>
prev.filter((criterion) => {
return criterion.id !== deletedCriterion.id;
}),
);
},
[],
);
const onSwitchChange = useCallback(
async (event: React.ChangeEvent<HTMLInputElement>) => {
const value = event.target.checked;
setSettings((prev) => {
const changedSettings = {
...prev,
rating: {
...prev.rating,
allowRatingProjects: value,
},
};
return changedSettings as Partial<SettingsDTO>;
});
},
[],
);
return (
<SettingsSection title="Project Rating">
<div>
<FormControlLabel
control={
<Switch
checked={settings?.project?.allowRatingProjects}
onChange={onSwitchChange}
/>
}
label="Allow users to rate other projects"
/>
</div>
<div>
<Subsubheading text="Edit Criteria" />
{allCriteria.map((criterion) => (
<React.Fragment key={criterion.id}>
<CriterionEditor
criterion={criterion}
onSave={updateCriterion}
onDelete={deleteCriterion}
/>
<Spacer />
</React.Fragment>
))}
</div>
<div>
<Button onClick={addCriterion}>Add</Button>
<Spacer />
</div>
</SettingsSection>
);
};