1- import React from "react" ;
1+ import React , { useContext } from "react" ;
22import TableCell from "@mui/material/TableCell" ;
33import TableRow from "@mui/material/TableRow" ;
44import { Button } from "@mui/material" ;
55import { ScheduleHour } from "../types" ;
66import { tableInnerItemStyle } from "../styles" ;
7+ import { UserContext } from "../../../context/userContext" ;
78
89type ScheduleClassTableRowHourProps = {
910 scheduleHour : ScheduleHour ;
@@ -12,8 +13,16 @@ type ScheduleClassTableRowHourProps = {
1213export function ScheduleClassTableRowHour ( {
1314 scheduleHour
1415} : ScheduleClassTableRowHourProps ) {
15- const hasFreeSpots = ( ) => {
16- return scheduleHour . availableSpots > "0" ;
16+ const { email } = useContext ( UserContext ) ;
17+
18+ const isUserInList = scheduleHour . usersList . includes ( email ) ;
19+
20+ const canSchedule = ( ) => {
21+ return scheduleHour . availableSpots > "0" && ! isUserInList ;
22+ } ;
23+
24+ const canCancel = ( ) => {
25+ return isUserInList ;
1726 } ;
1827
1928 return (
@@ -24,12 +33,12 @@ export function ScheduleClassTableRowHour({
2433 < TableCell align = 'center' > { scheduleHour . numberOfSpots } </ TableCell >
2534 < TableCell align = 'center' > { scheduleHour . availableSpots } </ TableCell >
2635 < TableCell align = 'center' >
27- < Button variant = 'contained' color = 'success' disabled = { ! hasFreeSpots ( ) } >
36+ < Button variant = 'contained' color = 'success' disabled = { ! canSchedule ( ) } >
2837 Schedule
2938 </ Button >
3039 </ TableCell >
3140 < TableCell align = 'center' >
32- < Button variant = 'outlined' color = 'error' >
41+ < Button variant = 'outlined' color = 'error' disabled = { ! canCancel ( ) } >
3342 Cancel
3443 </ Button >
3544 </ TableCell >
0 commit comments