|
| 1 | +/* eslint-disable react/jsx-curly-brace-presence */ |
| 2 | +import { Button, Checkbox } from '@material-ui/core'; |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { useHistory, useParams } from 'react-router-dom'; |
| 5 | +import Dialog from '@material-ui/core/Dialog'; |
| 6 | +import DialogActions from '@material-ui/core/DialogActions'; |
| 7 | +import DialogContent from '@material-ui/core/DialogContent'; |
| 8 | +import DialogContentText from '@material-ui/core/DialogContentText'; |
| 9 | +import DialogTitle from '@material-ui/core/DialogTitle'; |
| 10 | +import { |
| 11 | + useBlockPurchasesMutation, |
| 12 | + useDeleteStudentMutation, |
| 13 | + useStudentQuery, |
| 14 | +} from '../../__generated__/types'; |
| 15 | + |
| 16 | +export function StudentInfoPage() { |
| 17 | + const { studentId, classId } = useParams<Record<string, string>>(); |
| 18 | + const [checked, setChecked] = useState(true); |
| 19 | + const [open, setOpen] = useState(false); |
| 20 | + const history = useHistory(); |
| 21 | + |
| 22 | + const [blockPurchases] = useBlockPurchasesMutation({}); |
| 23 | + |
| 24 | + const handleClickOpen = () => { |
| 25 | + setOpen(true); |
| 26 | + }; |
| 27 | + |
| 28 | + const handleClose = () => { |
| 29 | + setOpen(false); |
| 30 | + }; |
| 31 | + |
| 32 | + const onDeleteCompleted = () => { |
| 33 | + console.log('student deleted!'); |
| 34 | + handleClose(); |
| 35 | + history.goBack(); |
| 36 | + }; |
| 37 | + const [deleteStudent] = useDeleteStudentMutation({ onCompleted: onDeleteCompleted }); |
| 38 | + |
| 39 | + const handleDeleteStudent = () => { |
| 40 | + deleteStudent({ |
| 41 | + variables: { |
| 42 | + studentId, |
| 43 | + courseId: classId, |
| 44 | + }, |
| 45 | + }).catch((e) => console.log(e)); |
| 46 | + }; |
| 47 | + |
| 48 | + const handleChange = () => { |
| 49 | + blockPurchases({ |
| 50 | + variables: { |
| 51 | + studentId, |
| 52 | + courseId: classId, |
| 53 | + blocked: !checked, |
| 54 | + }, |
| 55 | + }).catch((e) => console.log(e)); |
| 56 | + setChecked(!checked); |
| 57 | + }; |
| 58 | + const { loading, error, data } = useStudentQuery({ |
| 59 | + variables: { |
| 60 | + studentId, |
| 61 | + courseId: classId, |
| 62 | + }, |
| 63 | + fetchPolicy: 'network-only', |
| 64 | + }); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + if (!data) { |
| 68 | + return; |
| 69 | + } |
| 70 | + setChecked(data.student.purchaseBlocked); |
| 71 | + }, [data]); |
| 72 | + |
| 73 | + if (loading) return <div>Loading...</div>; |
| 74 | + if (error) { |
| 75 | + console.log(error); |
| 76 | + return <>{error}</>; |
| 77 | + } |
| 78 | + if (!data) { |
| 79 | + return <>Data could not be retrieved</>; |
| 80 | + } |
| 81 | + |
| 82 | + const { student } = data; |
| 83 | + return ( |
| 84 | + <div> |
| 85 | + <h3> |
| 86 | + {student.firstName} {student.lastName} |
| 87 | + </h3> |
| 88 | + <p>User Id: {student.studentId}</p> |
| 89 | + <p>Points: {student.points}</p> |
| 90 | + <p>Total Points Earned: {student.totalPointsAwarded}</p> |
| 91 | + <p>Total Points Spent: {student.totalPointsSpent}</p> |
| 92 | + <div> |
| 93 | + Purchases Blocked |
| 94 | + <Checkbox |
| 95 | + checked={checked} |
| 96 | + onChange={handleChange} |
| 97 | + inputProps={{ 'aria-label': 'primary checkbox' }} |
| 98 | + /> |
| 99 | + </div> |
| 100 | + <Button |
| 101 | + style={{ |
| 102 | + width: 100, |
| 103 | + marginTop: 20, |
| 104 | + backgroundColor: '#DC143C', |
| 105 | + color: 'white', |
| 106 | + }} |
| 107 | + onClick={handleClickOpen} |
| 108 | + data-testid="create-btn" |
| 109 | + > |
| 110 | + Delete Student |
| 111 | + </Button> |
| 112 | + <Dialog |
| 113 | + open={open} |
| 114 | + onClose={handleClose} |
| 115 | + aria-labelledby="alert-dialog-title" |
| 116 | + aria-describedby="alert-dialog-description" |
| 117 | + > |
| 118 | + <DialogTitle id="alert-dialog-title">{'Delete student?'}</DialogTitle> |
| 119 | + <DialogContent> |
| 120 | + <DialogContentText id="alert-dialog-description"> |
| 121 | + All purchases, receipts, point data, and activity notifications for this student |
| 122 | + will be removed. This action can not be undone. |
| 123 | + </DialogContentText> |
| 124 | + </DialogContent> |
| 125 | + <DialogActions> |
| 126 | + <Button onClick={handleClose} color="primary"> |
| 127 | + Cancel |
| 128 | + </Button> |
| 129 | + <Button onClick={handleDeleteStudent} color="primary" autoFocus> |
| 130 | + Delete Student |
| 131 | + </Button> |
| 132 | + </DialogActions> |
| 133 | + </Dialog> |
| 134 | + </div> |
| 135 | + ); |
| 136 | +} |
0 commit comments