email settings
This commit is contained in:
1 parent
819c0ca05a
commit
3faff5dc54
16 files changed
+665
-33
No files matched your search
@@ -3,16 +3,23 @@ import { BasePage, ConfigBox } from "@/components/design-elements";
|
||||
import { AutoComplete, Select, LanguageSelector } from "@/components/inputs";
|
||||
import MenuItem from "@mui/material/MenuItem";
|
||||
import Typography from "@mui/material/Typography";
|
||||
import { SettingsForm } from "@/pages/settings/SettingsForm";
|
||||
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useMemo, useState } from "react";
|
||||
import { setTimezone, setMode } from "@/features/uiSlice";
|
||||
import { useAppSelector, useAppDispatch } from "@/hooks/AppHooks";
|
||||
import timezones from "@/utils/timezones.json";
|
||||
import type { ITimeZone } from "@/types/timezone";
|
||||
import type { SelectChangeEvent } from "@mui/material/Select";
|
||||
import { useGet, usePatch, usePost } from "@/hooks/UseApi";
|
||||
import type { ApiResponse } from "@/hooks/UseApi";
|
||||
import { z } from "zod";
|
||||
import { systemSettingsSchema } from "@/validation/zod";
|
||||
|
||||
const SettingsPage = () => {
|
||||
type FormValues = z.infer<typeof systemSettingsSchema>;
|
||||
|
||||
const theme = useTheme();
|
||||
const dispatch = useAppDispatch();
|
||||
const timezone = useAppSelector((state) => state.ui.timezone);
|
||||
@@ -24,13 +31,29 @@ const SettingsPage = () => {
|
||||
const tz = timezones.find((tz) => tz._id === timezone);
|
||||
setSelectedTimezone(tz || null);
|
||||
}, [timezone]);
|
||||
|
||||
const { post, loading: posting } = usePost<Partial<FormValues>, any>();
|
||||
const { patch, loading: patching } = usePatch<Partial<FormValues>, any>();
|
||||
|
||||
const { response, loading } = useGet<ApiResponse<any>>("/settings");
|
||||
const settings = response?.data || {};
|
||||
const initialSettings = useMemo(() => settings, [settings]);
|
||||
|
||||
const onSubmit = async (data: Partial<FormValues>) => {
|
||||
const res = await patch("/settings", data);
|
||||
console.log(res?.data);
|
||||
};
|
||||
|
||||
const onTest = async (data: Partial<FormValues>) => {
|
||||
const res = await post("/settings/test-transport", data);
|
||||
};
|
||||
return (
|
||||
<BasePage>
|
||||
<ConfigBox
|
||||
title="UI Settings"
|
||||
subtitle="Configure time zone, UI mode, and language preferences"
|
||||
rightContent={
|
||||
<Stack spacing={theme.spacing(8)}>
|
||||
<Stack spacing={theme.spacing(10)}>
|
||||
<Stack spacing={theme.spacing(2)}>
|
||||
<Typography>Time zone</Typography>
|
||||
|
||||
@@ -62,6 +85,12 @@ const SettingsPage = () => {
|
||||
</Stack>
|
||||
}
|
||||
/>
|
||||
<SettingsForm
|
||||
onSubmit={onSubmit}
|
||||
onTest={onTest}
|
||||
initialData={initialSettings}
|
||||
loading={posting || loading || patching}
|
||||
/>
|
||||
</BasePage>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
import Stack from "@mui/material/Stack";
|
||||
import Box from "@mui/material/Box";
|
||||
import { Button, TextInput, Switch, TextLink } from "@/components/inputs";
|
||||
import { ConfigBox } from "@/components/design-elements";
|
||||
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { systemSettingsSchema } from "@/validation/zod";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useForm, Controller, type SubmitHandler } from "react-hook-form";
|
||||
import { useInitForm } from "@/hooks/forms/UseInitSettingsForm";
|
||||
import { useEffect } from "react";
|
||||
import { Typography } from "@mui/material";
|
||||
type FormValues = z.infer<typeof systemSettingsSchema>;
|
||||
|
||||
export const SettingsForm = ({
|
||||
initialData,
|
||||
onSubmit,
|
||||
onTest,
|
||||
loading,
|
||||
}: {
|
||||
initialData?: Partial<FormValues>;
|
||||
onSubmit: SubmitHandler<FormValues>;
|
||||
onTest: (data: Partial<FormValues>) => Promise<void>;
|
||||
loading?: boolean;
|
||||
}) => {
|
||||
const { defaults } = useInitForm({ initialData: initialData });
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
const {
|
||||
handleSubmit,
|
||||
control,
|
||||
reset,
|
||||
watch,
|
||||
formState: { errors },
|
||||
} = useForm<FormValues>({
|
||||
resolver: zodResolver(systemSettingsSchema) as any,
|
||||
defaultValues: defaults,
|
||||
mode: "onChange",
|
||||
});
|
||||
|
||||
const formValues = watch();
|
||||
|
||||
useEffect(() => {
|
||||
reset(defaults);
|
||||
}, [initialData, reset, defaults]);
|
||||
|
||||
return (
|
||||
<Stack
|
||||
spacing={theme.spacing(10)}
|
||||
component="form"
|
||||
onSubmit={handleSubmit(onSubmit)}
|
||||
>
|
||||
<ConfigBox
|
||||
title={t("settingsPage.emailSettings.title")}
|
||||
subtitle={t("settingsPage.emailSettings.description")}
|
||||
leftContent={
|
||||
<Stack>
|
||||
<TextLink
|
||||
text={t("settingsPage.emailSettings.descriptionTransport")}
|
||||
linkText={t("settingsPage.emailSettings.linkTransport")}
|
||||
href="https://nodemailer.com/smtp"
|
||||
target="_blank"
|
||||
/>
|
||||
<Box
|
||||
component={"pre"}
|
||||
sx={{
|
||||
fontFamily: "monospace",
|
||||
p: 2,
|
||||
borderRadius: 1,
|
||||
overflow: "auto",
|
||||
}}
|
||||
>
|
||||
<code>
|
||||
{JSON.stringify(
|
||||
{
|
||||
host: formValues.systemEmailHost,
|
||||
port: formValues.systemEmailPort,
|
||||
secure: formValues.systemEmailSecure,
|
||||
auth: {
|
||||
user:
|
||||
formValues.systemEmailUser ||
|
||||
formValues.systemEmailAddress,
|
||||
pass: "<your_password>",
|
||||
},
|
||||
name: formValues.systemEmailConnectionHost || "localhost",
|
||||
pool: formValues.systemEmailPool,
|
||||
tls: {
|
||||
rejectUnauthorized:
|
||||
formValues.systemEmailRejectUnauthorized,
|
||||
ignoreTLS: formValues.systemEmailIgnoreTLS,
|
||||
requireTLS: formValues.systemEmailRequireTLS,
|
||||
servername: formValues.systemEmailTLSServername,
|
||||
},
|
||||
},
|
||||
null,
|
||||
2
|
||||
)}
|
||||
</code>
|
||||
</Box>
|
||||
</Stack>
|
||||
}
|
||||
rightContent={
|
||||
<Stack spacing={theme.spacing(10)}>
|
||||
<Controller
|
||||
name="systemEmailHost"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="text"
|
||||
fieldLabel={t("settingsPage.emailSettings.labelHost")}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailHost}
|
||||
helperText={
|
||||
errors.systemEmailHost ? errors.systemEmailHost.message : ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailPort"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="number"
|
||||
fieldLabel={t("settingsPage.emailSettings.labelPort")}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailPort}
|
||||
helperText={
|
||||
errors.systemEmailPort ? errors.systemEmailPort.message : ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailUser"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="text"
|
||||
fieldLabel={t("settingsPage.emailSettings.labelUser")}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailUser}
|
||||
helperText={
|
||||
errors.systemEmailUser ? errors.systemEmailUser.message : ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailAddress"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="text"
|
||||
fieldLabel={t("settingsPage.emailSettings.labelAddress")}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailAddress}
|
||||
helperText={
|
||||
errors.systemEmailAddress
|
||||
? errors.systemEmailAddress.message
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailPassword"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="password"
|
||||
fieldLabel={t("settingsPage.emailSettings.labelPassword")}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailPassword}
|
||||
helperText={
|
||||
errors.systemEmailPassword
|
||||
? errors.systemEmailPassword.message
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailTLSServername"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="text"
|
||||
fieldLabel={t(
|
||||
"settingsPage.emailSettings.labelTLSServername"
|
||||
)}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailTLSServername}
|
||||
helperText={
|
||||
errors.systemEmailTLSServername
|
||||
? errors.systemEmailTLSServername.message
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailConnectionHost"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<TextInput
|
||||
{...field}
|
||||
type="text"
|
||||
fieldLabel={t(
|
||||
"settingsPage.emailSettings.labelConnectionHost"
|
||||
)}
|
||||
fullWidth
|
||||
error={!!errors.systemEmailConnectionHost}
|
||||
helperText={
|
||||
errors.systemEmailConnectionHost
|
||||
? errors.systemEmailConnectionHost.message
|
||||
: ""
|
||||
}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailSecure"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<Stack direction="row" alignItems={"center"}>
|
||||
<Switch {...field} checked={field.value} />
|
||||
<Typography>
|
||||
{t("settingsPage.emailSettings.labelSecure")}
|
||||
</Typography>
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailPool"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<Stack direction="row" alignItems={"center"}>
|
||||
<Switch {...field} checked={field.value} />
|
||||
|
||||
<Typography>
|
||||
{t("settingsPage.emailSettings.labelPool")}
|
||||
</Typography>
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailIgnoreTLS"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<Stack direction="row" alignItems={"center"}>
|
||||
<Switch {...field} checked={field.value} />
|
||||
|
||||
<Typography>
|
||||
{t("settingsPage.emailSettings.labelIgnoreTLS")}
|
||||
</Typography>
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailRequireTLS"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<Stack direction="row" alignItems={"center"}>
|
||||
<Switch {...field} checked={field.value} />
|
||||
|
||||
<Typography>
|
||||
{t("settingsPage.emailSettings.labelRequireTLS")}
|
||||
</Typography>
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name="systemEmailRejectUnauthorized"
|
||||
control={control}
|
||||
render={({ field }) => (
|
||||
<Stack direction="row" alignItems={"center"}>
|
||||
<Switch {...field} checked={field.value} />
|
||||
|
||||
<Typography>
|
||||
{t("settingsPage.emailSettings.labelRejectUnauthorized")}
|
||||
</Typography>
|
||||
</Stack>
|
||||
)}
|
||||
/>
|
||||
</Stack>
|
||||
}
|
||||
/>
|
||||
<Stack gap={theme.spacing(4)} direction="row" justifyContent="flex-end">
|
||||
<Button
|
||||
loading={loading}
|
||||
variant="contained"
|
||||
color="accent"
|
||||
onClick={() => {
|
||||
onTest(formValues);
|
||||
}}
|
||||
>
|
||||
Test transport config
|
||||
</Button>
|
||||
<Button
|
||||
loading={loading}
|
||||
type="submit"
|
||||
variant="contained"
|
||||
color="accent"
|
||||
>
|
||||
{t("settingsSave")}
|
||||
</Button>
|
||||
</Stack>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user