-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hooking up report user dialog with mutation
- Loading branch information
1 parent
b7bc668
commit f23e2e6
Showing
6 changed files
with
163 additions
and
98 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
This file was deleted.
Oops, something went wrong.
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
138 changes: 138 additions & 0 deletions
138
app/frontend/src/features/profile/actions/ReportUserDialog.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,138 @@ | ||
import { DialogProps, makeStyles, Snackbar } from "@material-ui/core"; | ||
import Alert from "components/Alert"; | ||
import Button from "components/Button"; | ||
import { | ||
Dialog, | ||
DialogActions, | ||
DialogContent, | ||
DialogContentText, | ||
DialogTitle, | ||
} from "components/Dialog"; | ||
import TextField from "components/TextField"; | ||
import { Empty } from "google-protobuf/google/protobuf/empty_pb"; | ||
import { Error as GrpcError } from "grpc-web"; | ||
import React, { useState } from "react"; | ||
import { useForm } from "react-hook-form"; | ||
import { useMutation } from "react-query"; | ||
import { service } from "service/index"; | ||
import type { ReportUserInput } from "service/user"; | ||
|
||
import { | ||
CANCEL, | ||
getReportDialogTitle, | ||
getReportUserExplainer, | ||
getReportUserSuccessMessage, | ||
REPORT_DETAILS, | ||
REPORT_REASON, | ||
SEND, | ||
} from "../constants"; | ||
import { useProfileUser } from "../hooks/useProfileUser"; | ||
|
||
const REPORT_USER_DIALOG_LABEL_ID = "report-user-dialog-title"; | ||
|
||
type ReportUserFormData = Omit<ReportUserInput, "userId">; | ||
|
||
function SuccessSnackbar({ name }: { name: string }) { | ||
const [open, setOpen] = useState(true); | ||
|
||
return ( | ||
<Snackbar | ||
autoHideDuration={6000} | ||
open={open} | ||
onClose={() => setOpen(false)} | ||
> | ||
<Alert severity="success">{getReportUserSuccessMessage(name)}</Alert> | ||
</Snackbar> | ||
); | ||
} | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
textField: { | ||
"& + &": { | ||
marginBlockStart: theme.spacing(2), | ||
}, | ||
}, | ||
})); | ||
|
||
export default function ReportUserDialog({ onClose, open }: DialogProps) { | ||
const classes = useStyles(); | ||
const { name, userId } = useProfileUser(); | ||
const { | ||
handleSubmit, | ||
register, | ||
reset: resetForm, | ||
} = useForm<ReportUserFormData>(); | ||
|
||
const { | ||
error, | ||
isLoading, | ||
isSuccess, | ||
mutate: reportUser, | ||
reset: resetMutation, | ||
} = useMutation<Empty, GrpcError, ReportUserFormData>( | ||
({ description, reason }) => | ||
service.user.reportUser({ description, reason, userId }), | ||
{ | ||
onSuccess: () => { | ||
onClose?.({}, "escapeKeyDown"); | ||
}, | ||
} | ||
); | ||
|
||
const onSubmit = handleSubmit(({ description, reason }) => { | ||
// trigger mutation | ||
reportUser({ description, reason }); | ||
}); | ||
|
||
return ( | ||
<> | ||
{isSuccess && <SuccessSnackbar name={name} />} | ||
<Dialog aria-labelledby={REPORT_USER_DIALOG_LABEL_ID} open={open}> | ||
<form onSubmit={onSubmit}> | ||
<DialogTitle id={REPORT_USER_DIALOG_LABEL_ID}> | ||
{getReportDialogTitle("Itsi")} | ||
</DialogTitle> | ||
<DialogContent> | ||
{error && <Alert severity="error">{error.message}</Alert>} | ||
<DialogContentText> | ||
{getReportUserExplainer(name)} | ||
</DialogContentText> | ||
<TextField | ||
className={classes.textField} | ||
id="user-report-reason" | ||
inputRef={register({ required: true })} | ||
label={REPORT_REASON} | ||
name="reason" | ||
fullWidth | ||
/> | ||
<TextField | ||
className={classes.textField} | ||
fullWidth | ||
id="user-report-details" | ||
inputRef={register({ required: true })} | ||
label={REPORT_DETAILS} | ||
multiline | ||
name="description" | ||
rows={4} | ||
rowsMax={4} | ||
/> | ||
</DialogContent> | ||
<DialogActions> | ||
<Button loading={isLoading} onClick={onSubmit} type="submit"> | ||
{SEND} | ||
</Button> | ||
<Button | ||
onClick={() => { | ||
resetForm(); | ||
resetMutation(); | ||
onClose?.({}, "escapeKeyDown"); | ||
}} | ||
> | ||
{CANCEL} | ||
</Button> | ||
</DialogActions> | ||
</form> | ||
</Dialog> | ||
</> | ||
); | ||
} |
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