@@ -4,21 +4,15 @@ import type { CriterionDTO, SettingsDTO } from "../../../api/types/dto";
44import { Spacer } from "../../base/flex" ;
55import { SettingsSection } from "../settings-section" ;
66import { Subsubheading } from "../../base/headings" ;
7- import {
8- TextField ,
9- Switch ,
10- FormControlLabel ,
11- Stack ,
12- Button ,
13- } from "@mui/material" ;
7+ import { Button } from "../../base/button" ;
8+ import { TextField , Switch , FormControlLabel , Stack } from "@mui/material" ;
149import { api } from "../../../hooks/use-api" ;
10+ import { useNotificationContext } from "../../../contexts/notification-context" ;
1511
1612// TODO Seems more maintainable to me if the save button is fine-grained
1713// and not global. Remove the global Save button and add one individually for
1814// each SettingsSection component for consistency.
1915
20- // TODO use our own button style
21-
2216interface ICriterionEditorProps {
2317 criterion : CriterionDTO ;
2418 onSave : ( criterion : CriterionDTO ) => Promise < void > ;
@@ -34,31 +28,41 @@ const CriterionEditor = React.memo(
3428 const [ title , setTitle ] = useState ( criterion . title ) ;
3529 const [ description , setDescription ] = useState ( criterion . description ) ;
3630
31+ const validateAndSave = ( changedCriterion : CriterionDTO ) => {
32+ if ( changedCriterion . title . length === 0 ) {
33+ return ;
34+ }
35+
36+ if ( changedCriterion . description . length === 0 ) {
37+ return ;
38+ }
39+
40+ onSave ( changedCriterion ) ;
41+ } ;
42+
3743 return (
3844 < Stack direction = { { sm : "column" , md : "row" } } spacing = { { xs : 1 , sm : 2 } } >
3945 < TextField
46+ error = { title . length === 0 }
4047 value = { title }
4148 onChange = { ( event ) => setTitle ( event . target . value ) }
42- placeholder = "title "
49+ placeholder = "Title "
4350 rows = { 1 }
4451 />
4552 < TextField
53+ error = { description . length === 0 }
4654 value = { description }
4755 onChange = { ( event ) => setDescription ( event . target . value ) }
4856 placeholder = "Description"
4957 rows = { 1 }
5058 sx = { { flex : 1 } }
5159 />
5260 < Button
53- fullWidth = { false }
54- variant = "contained"
55- onClick = { ( ) => onSave ( { ...criterion , title, description } ) }
61+ onClick = { ( ) => validateAndSave ( { ...criterion , title, description } ) }
5662 >
5763 Save
5864 </ Button >
59- < Button variant = "contained" onClick = { ( ) => onDelete ( criterion ) } >
60- Delete
61- </ Button >
65+ < Button onClick = { ( ) => onDelete ( criterion ) } > Delete</ Button >
6266 </ Stack >
6367 ) ;
6468 } ,
@@ -72,6 +76,8 @@ export const ProjectRatingSettings = () => {
7276 const [ allCriteria , setAllCriteria ] = useState < CriterionDTO [ ] > ( [ ] ) ;
7377 const [ settings , setSettings ] = useState < Partial < SettingsDTO > > ( { } ) ;
7478
79+ const { showNotification } = useNotificationContext ( ) ;
80+
7581 // Do this only on mount
7682 useEffect ( ( ) => {
7783 api . getAllCriteria ( ) . then ( ( criteria ) => {
@@ -92,8 +98,8 @@ export const ProjectRatingSettings = () => {
9298
9399 const addCriterion = useCallback ( async ( ) : Promise < void > => {
94100 const newCriterion = await api . createCriterion ( {
95- title : "title " ,
96- description : "description " ,
101+ title : "" ,
102+ description : "" ,
97103 } as unknown as CriterionDTO ) ;
98104 setAllCriteria ( ( prev ) => [ ...prev , newCriterion ] ) ;
99105 } , [ ] ) ;
@@ -108,6 +114,8 @@ export const ProjectRatingSettings = () => {
108114 : criterion ;
109115 } ) ,
110116 ) ;
117+
118+ showNotification ( "Saved criterion" ) ;
111119 } ,
112120 [ ] ,
113121 ) ;
@@ -168,9 +176,7 @@ export const ProjectRatingSettings = () => {
168176 ) ) }
169177 </ div >
170178 < div >
171- < Button fullWidth = { false } variant = "contained" onClick = { addCriterion } >
172- Add
173- </ Button >
179+ < Button onClick = { addCriterion } > Add</ Button >
174180 < Spacer />
175181 </ div >
176182 </ SettingsSection >
0 commit comments