|
| 1 | +import { ReactElement } from "react"; |
| 2 | + |
| 3 | +import { Box } from "@mui/material"; |
| 4 | + |
| 5 | +import { profileApi, settingsApi } from "src/apis"; |
| 6 | +import { Button, ConfirmationDialog, Typography } from "src/components/ui"; |
| 7 | +import { useDialog } from "src/core/dialog"; |
| 8 | +import { useTranslation } from "src/core/i18n"; |
| 9 | +import { useMutation } from "src/core/query"; |
| 10 | +import { CacheKeysConstants, useQuery } from "src/core/query"; |
| 11 | +import { useSnackbar } from "src/core/snackbar"; |
| 12 | +import { ErrorView } from "src/views/ErrorView"; |
| 13 | + |
| 14 | +import { SettingsPageNav } from "./SettingsPage.nav"; |
| 15 | +import { SettingsPageSkeleton } from "./SettingsPage.skeleton"; |
| 16 | + |
| 17 | +export function SettingsPageContainer(): ReactElement { |
| 18 | + const { t } = useTranslation("settings"); |
| 19 | + const { openDialog } = useDialog(); |
| 20 | + const snackbar = useSnackbar(); |
| 21 | + |
| 22 | + const { error, data, isPending } = useQuery({ |
| 23 | + queryKey: [CacheKeysConstants.Settings], |
| 24 | + queryFn: () => settingsApi.getCurrentSettings(), |
| 25 | + }); |
| 26 | + |
| 27 | + const deleteAccountMutation = useMutation({ |
| 28 | + onError: () => snackbar.success(t("danger-zone.delete-account.success")), |
| 29 | + onSuccess: () => snackbar.error(t("danger-zone.delete-account.error")), |
| 30 | + mutationFn: () => profileApi.deleteCurrentProfile(), |
| 31 | + }); |
| 32 | + |
| 33 | + if (error) { |
| 34 | + return ( |
| 35 | + <SettingsPageNav> |
| 36 | + <ErrorView /> |
| 37 | + </SettingsPageNav> |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + if (isPending) { |
| 42 | + return ( |
| 43 | + <SettingsPageNav> |
| 44 | + <SettingsPageSkeleton /> |
| 45 | + </SettingsPageNav> |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + if (!data) { |
| 50 | + return ( |
| 51 | + <SettingsPageNav> |
| 52 | + <ErrorView message="Settings not found" /> |
| 53 | + </SettingsPageNav> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + function handleClickDelete() { |
| 58 | + openDialog(ConfirmationDialog, { |
| 59 | + description: t("danger-zone.delete-account.description"), |
| 60 | + onConfirm: async (onClose) => { |
| 61 | + await deleteAccountMutation.mutateAsync(); |
| 62 | + onClose(); |
| 63 | + }, |
| 64 | + title: t("danger-zone.delete-account.title"), |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + return ( |
| 69 | + <Box |
| 70 | + sx={{ |
| 71 | + display: "flex", |
| 72 | + flexDirection: "column", |
| 73 | + }} |
| 74 | + > |
| 75 | + <SettingsPageNav> |
| 76 | + <Typography gutterBottom sx={{ paddingTop: 3 }} variant="h5"> |
| 77 | + {t("danger-zone.header")} |
| 78 | + </Typography> |
| 79 | + |
| 80 | + <Typography color="textSecondary" sx={{ pb: 2 }}> |
| 81 | + {t("danger-zone.description")} |
| 82 | + </Typography> |
| 83 | + |
| 84 | + <div> |
| 85 | + <Button |
| 86 | + color="error" |
| 87 | + loading={deleteAccountMutation.isPending} |
| 88 | + onClick={handleClickDelete} |
| 89 | + variant="contained" |
| 90 | + > |
| 91 | + {t("danger-zone.delete-account.button")} |
| 92 | + </Button> |
| 93 | + </div> |
| 94 | + </SettingsPageNav> |
| 95 | + </Box> |
| 96 | + ); |
| 97 | +} |
0 commit comments