MyClassForm.tsx (2118B)
1 import { 2 FormControl, 3 InputLabel, 4 Select, 5 MenuItem, 6 Box, 7 Stack, 8 } from '@mui/material'; 9 import { useSnackbar } from 'notistack'; 10 import { useCallback } from 'react'; 11 12 import useMyClass from 'client/hooks/useMyClass'; 13 import { GRADE_CLASSES } from 'common/constant'; 14 import { zodGradeClass } from 'common/utils/zod'; 15 16 import type { SelectChangeEvent } from '@mui/material'; 17 18 export default function MyClassForm() { 19 const { enqueueSnackbar } = useSnackbar(); 20 const [cookieValue, setCookieValue, removeCookieValue] = useMyClass(); 21 22 const handleClassChange = useCallback( 23 (e: SelectChangeEvent) => { 24 const value = e.target.value; 25 const validation = zodGradeClass.safeParse(value); 26 27 if (validation.success) { 28 setCookieValue(validation.data); 29 enqueueSnackbar({ 30 message: `${validation.data} を所属クラスとして設定しました。\nホーム画面からの起動時に ${validation.data} の時間割を表示します。`, 31 variant: 'success', 32 autoHideDuration: 5000, 33 }); 34 } else if (value === 'unset' || value === '') { 35 removeCookieValue(); 36 enqueueSnackbar({ 37 message: 38 '所属クラスの設定を削除しました。\nホーム画面からの起動時はトップページを表示します。', 39 variant: 'success', 40 autoHideDuration: 5000, 41 }); 42 } else { 43 enqueueSnackbar({ 44 message: 'エラーが発生しました。', 45 variant: 'error', 46 autoHideDuration: 5000, 47 }); 48 } 49 }, 50 [enqueueSnackbar, removeCookieValue, setCookieValue], 51 ); 52 53 return ( 54 <Stack direction='row' spacing={2} alignItems='center'> 55 <Box width='10rem'> 56 <FormControl fullWidth> 57 <InputLabel id='class-selection'>所属クラス</InputLabel> 58 <Select 59 labelId='class-selection' 60 label='所属クラス' 61 value={cookieValue ?? ''} 62 onChange={handleClassChange} 63 > 64 <MenuItem value='unset'>設定しない</MenuItem> 65 {GRADE_CLASSES.map((g_c) => ( 66 <MenuItem value={g_c} key={g_c}> 67 {g_c} 68 </MenuItem> 69 ))} 70 </Select> 71 </FormControl> 72 </Box> 73 </Stack> 74 ); 75 }