mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-19 07:55:25 +02:00
feat: Add SettingsForm mutation handling
This commit is contained in:
parent
c1edcd290d
commit
db072002fe
@ -382,7 +382,8 @@
|
|||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"title": "Global Settings",
|
"title": "Global Settings",
|
||||||
"description": "Configure global settings for your instance."
|
"description": "Configure global settings for your instance.",
|
||||||
|
"successMessage": "Settings have been saved successfully!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -4,6 +4,7 @@ import {TbCursorText} from "react-icons/tb"
|
|||||||
import {useTranslation} from "react-i18next"
|
import {useTranslation} from "react-i18next"
|
||||||
import {MdCheck, MdOutlineChangeCircle, MdTextFormat} from "react-icons/md"
|
import {MdCheck, MdOutlineChangeCircle, MdTextFormat} from "react-icons/md"
|
||||||
import {BsImage} from "react-icons/bs"
|
import {BsImage} from "react-icons/bs"
|
||||||
|
import {AxiosError} from "axios"
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Checkbox,
|
Checkbox,
|
||||||
@ -16,12 +17,19 @@ import {
|
|||||||
Typography,
|
Typography,
|
||||||
} from "@mui/material"
|
} from "@mui/material"
|
||||||
import {LoadingButton} from "@mui/lab"
|
import {LoadingButton} from "@mui/lab"
|
||||||
|
import {useMutation} from "@tanstack/react-query"
|
||||||
|
|
||||||
import {AdminSettings} from "~/server-types"
|
import {AdminSettings} from "~/server-types"
|
||||||
import {StringPoolField, createPool} from "~/components"
|
import {StringPoolField, createPool} from "~/components"
|
||||||
|
import {updateAdminSettings} from "~/apis"
|
||||||
|
import {useErrorSuccessSnacks} from "~/hooks"
|
||||||
|
import {queryClient} from "~/constants/react-query"
|
||||||
|
import {parseFastAPIError} from "~/utils"
|
||||||
|
import {DEFAULT_ADMIN_SETTINGS} from "~/constants/admin-settings"
|
||||||
|
|
||||||
export interface SettingsFormProps {
|
export interface SettingsFormProps {
|
||||||
settings: AdminSettings
|
settings: AdminSettings
|
||||||
|
queryKey: readonly string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
const DEFAULT_POOLS = createPool({
|
const DEFAULT_POOLS = createPool({
|
||||||
@ -30,8 +38,9 @@ const DEFAULT_POOLS = createPool({
|
|||||||
"0123456789": "0-9",
|
"0123456789": "0-9",
|
||||||
})
|
})
|
||||||
|
|
||||||
export default function SettingsForm({settings}: SettingsFormProps) {
|
export default function SettingsForm({settings, queryKey}: SettingsFormProps) {
|
||||||
const {t} = useTranslation()
|
const {t} = useTranslation()
|
||||||
|
const {showSuccess, showError} = useErrorSuccessSnacks()
|
||||||
|
|
||||||
const validationSchema = yup.object().shape({
|
const validationSchema = yup.object().shape({
|
||||||
randomEmailIdMinLength: yup
|
randomEmailIdMinLength: yup
|
||||||
@ -74,9 +83,40 @@ export default function SettingsForm({settings}: SettingsFormProps) {
|
|||||||
.label(t("routes.AdminRoute.forms.settings.allowStatistics.label")),
|
.label(t("routes.AdminRoute.forms.settings.allowStatistics.label")),
|
||||||
})
|
})
|
||||||
|
|
||||||
const formik = useFormik<AdminSettings>({
|
const {mutateAsync} = useMutation<AdminSettings, AxiosError, Partial<AdminSettings>>(
|
||||||
|
async settings => {
|
||||||
|
// Set values to `null` that are their defaults
|
||||||
|
const strippedSettings = Object.fromEntries(
|
||||||
|
Object.entries(settings as AdminSettings).map(([key, value]) => {
|
||||||
|
if (value === DEFAULT_ADMIN_SETTINGS[key as keyof AdminSettings]) {
|
||||||
|
return [key, null]
|
||||||
|
}
|
||||||
|
|
||||||
|
return [key, value]
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
|
||||||
|
return updateAdminSettings(strippedSettings)
|
||||||
|
},
|
||||||
|
{
|
||||||
|
onError: showError,
|
||||||
|
onSuccess: newSettings => {
|
||||||
|
showSuccess(t("routes.AdminRoute.settings.successMessage"))
|
||||||
|
|
||||||
|
queryClient.setQueryData<AdminSettings>(queryKey, newSettings)
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
const formik = useFormik<AdminSettings & {detail?: string}>({
|
||||||
validationSchema,
|
validationSchema,
|
||||||
onSubmit: console.log,
|
onSubmit: async (values, {setErrors}) => {
|
||||||
|
try {
|
||||||
|
await mutateAsync(values)
|
||||||
|
} catch (error) {
|
||||||
|
setErrors(parseFastAPIError(error as AxiosError))
|
||||||
|
}
|
||||||
|
},
|
||||||
initialValues: settings,
|
initialValues: settings,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user