-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
273b630
commit 7b931bf
Showing
6 changed files
with
224 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
frontend/src/Components/ModalComponents/Edit/EditAnswerModal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
Box, | ||
Typography, | ||
Modal, | ||
TextField, | ||
Grid, | ||
Checkbox, | ||
FormControlLabel, | ||
} from "@mui/material"; | ||
import { formStyle, style } from "../modalStyles"; | ||
import { enqueueSnackbar } from "notistack"; | ||
import ActionsButtons from "../ActionsButtons"; | ||
import EditIcon from "@mui/icons-material/Edit"; | ||
import { editAnswer } from "../../../logic/answers"; | ||
import { Answer } from "../../../logic/interfaces"; | ||
|
||
const EditAnswerModal = (props: { | ||
open: boolean; | ||
handleClose: () => void; | ||
answer: Answer; | ||
quizId: number; | ||
updateAnswer: (answer: Answer) => void; | ||
}) => { | ||
const handleEdit = async () => { | ||
const response = await editAnswer(props.answer, props.quizId); | ||
if (response.status === "updated") { | ||
enqueueSnackbar("Answer updated!", { variant: "success" }); | ||
props.handleClose(); | ||
} else { | ||
enqueueSnackbar("Something went wrong!", { variant: "error" }); | ||
} | ||
}; | ||
return ( | ||
<Modal open={props.open} onClose={props.handleClose}> | ||
<Box sx={style}> | ||
<Grid container sx={formStyle}> | ||
<Grid item> | ||
<Typography id="modal-modal-title" variant="h6" component="h2"> | ||
Edit answer | ||
</Typography> | ||
</Grid> | ||
<Grid item> | ||
<TextField | ||
placeholder={"Text"} | ||
fullWidth | ||
value={props.answer.text} | ||
onChange={(event) => | ||
props.updateAnswer({ | ||
...props.answer, | ||
text: event.target.value, | ||
}) | ||
} | ||
/> | ||
</Grid> | ||
<Grid item> | ||
<FormControlLabel | ||
control={ | ||
<Checkbox | ||
checked={props.answer.is_correct} | ||
onChange={(event) => | ||
props.updateAnswer({ | ||
...props.answer, | ||
is_correct: event.target.checked, | ||
}) | ||
} | ||
/> | ||
} | ||
label="Correct" | ||
/> | ||
</Grid> | ||
</Grid> | ||
<ActionsButtons | ||
cancel={props.handleClose} | ||
submit={handleEdit} | ||
submitText={"Edit"} | ||
submitIcon={<EditIcon />} | ||
/> | ||
</Box> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default EditAnswerModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useState } from "react"; | ||
import { TableRow, TableCell, IconButton } from "@mui/material"; | ||
import { Answer } from "../../logic/interfaces"; | ||
import { useConfirm } from "material-ui-confirm"; | ||
import EditIcon from "@mui/icons-material/Edit"; | ||
import DeleteIcon from "@mui/icons-material/Delete"; | ||
import EditAnswerModal from "../ModalComponents/Edit/EditAnswerModal"; | ||
import { enqueueSnackbar } from "notistack"; | ||
import { deleteAnswer } from "../../logic/answers"; | ||
|
||
const AnswerRow = (props: { | ||
answerRow: Answer; | ||
answerNumber: number; | ||
quizId: number; | ||
}) => { | ||
const confirm = useConfirm(); | ||
const { answerRow } = props; | ||
const [hide, setHide] = useState<boolean>(false); | ||
const [editedAnswer, setEditedAnswer] = useState<Answer>(answerRow); | ||
const [openEditAnswerModal, setOpenEditAnswerModal] = | ||
useState<boolean>(false); | ||
|
||
const handleDelete = async () => { | ||
if (answerRow === null) return; | ||
confirm({ | ||
description: "Are you sure you want to delete this answer?", | ||
}) | ||
.then(async () => { | ||
const response = await deleteAnswer( | ||
props.quizId, | ||
answerRow.question_id, | ||
answerRow.id, | ||
); | ||
if (response.status === "deleted") { | ||
enqueueSnackbar("Answer deleted!", { variant: "success" }); | ||
setHide(true); | ||
} else { | ||
enqueueSnackbar("Something went wrong!", { variant: "error" }); | ||
} | ||
}) | ||
.catch(() => { | ||
enqueueSnackbar("Answer not deleted!", { variant: "info" }); | ||
}); | ||
}; | ||
|
||
const updateAnswer = (answer: Answer) => { | ||
setEditedAnswer(answer); | ||
}; | ||
|
||
return ( | ||
<> | ||
{!hide && ( | ||
<> | ||
<TableRow key={answerRow.id}> | ||
<TableCell | ||
sx={{ | ||
width: "10px", | ||
}} | ||
> | ||
{props.answerNumber + 1} | ||
</TableCell> | ||
<TableCell component="th" scope="row"> | ||
{answerRow.text} | ||
</TableCell> | ||
<TableCell>{answerRow.is_correct ? "Yes" : "No"}</TableCell> | ||
<TableCell> | ||
<IconButton | ||
onClick={() => { | ||
setOpenEditAnswerModal(true); | ||
}} | ||
> | ||
<EditIcon /> | ||
</IconButton> | ||
<IconButton onClick={handleDelete}> | ||
<DeleteIcon /> | ||
</IconButton> | ||
</TableCell> | ||
</TableRow> | ||
|
||
<EditAnswerModal | ||
open={openEditAnswerModal} | ||
handleClose={() => setOpenEditAnswerModal(false)} | ||
answer={editedAnswer} | ||
quizId={props.quizId} | ||
updateAnswer={updateAnswer} | ||
/> | ||
</> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default AnswerRow; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters