2025-11-13 13:47:26 -08:00
|
|
|
import Stack from "@mui/material/Stack";
|
|
|
|
|
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";
|
2025-11-19 23:55:29 +00:00
|
|
|
import { SettingsForm } from "@/pages/settings/SettingsForm";
|
2025-11-13 13:47:26 -08:00
|
|
|
|
|
|
|
|
import { useTheme } from "@mui/material/styles";
|
2025-11-19 23:55:29 +00:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
2025-11-13 13:47:26 -08:00
|
|
|
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";
|
2025-11-19 23:55:29 +00:00
|
|
|
import { useGet, usePatch, usePost } from "@/hooks/UseApi";
|
|
|
|
|
import type { ApiResponse } from "@/hooks/UseApi";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { systemSettingsSchema } from "@/validation/zod";
|
2025-11-13 13:47:26 -08:00
|
|
|
|
|
|
|
|
const SettingsPage = () => {
|
2025-11-19 23:55:29 +00:00
|
|
|
type FormValues = z.infer<typeof systemSettingsSchema>;
|
|
|
|
|
|
2025-11-13 13:47:26 -08:00
|
|
|
const theme = useTheme();
|
|
|
|
|
const dispatch = useAppDispatch();
|
|
|
|
|
const timezone = useAppSelector((state) => state.ui.timezone);
|
|
|
|
|
const mode = useAppSelector((state) => state.ui.mode);
|
|
|
|
|
const [selectedTimezone, setSelectedTimezone] = useState<ITimeZone | null>(
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const tz = timezones.find((tz) => tz._id === timezone);
|
|
|
|
|
setSelectedTimezone(tz || null);
|
|
|
|
|
}, [timezone]);
|
2025-11-19 23:55:29 +00:00
|
|
|
|
|
|
|
|
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>) => {
|
2025-11-20 19:27:22 +00:00
|
|
|
const res = await patch("/settings/email", data);
|
2025-11-19 23:55:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onTest = async (data: Partial<FormValues>) => {
|
|
|
|
|
const res = await post("/settings/test-transport", data);
|
|
|
|
|
};
|
2025-11-13 13:47:26 -08:00
|
|
|
return (
|
|
|
|
|
<BasePage>
|
|
|
|
|
<ConfigBox
|
|
|
|
|
title="UI Settings"
|
|
|
|
|
subtitle="Configure time zone, UI mode, and language preferences"
|
|
|
|
|
rightContent={
|
2025-11-19 23:55:29 +00:00
|
|
|
<Stack spacing={theme.spacing(10)}>
|
2025-11-13 13:47:26 -08:00
|
|
|
<Stack spacing={theme.spacing(2)}>
|
|
|
|
|
<Typography>Time zone</Typography>
|
|
|
|
|
|
|
|
|
|
<AutoComplete
|
|
|
|
|
value={selectedTimezone}
|
|
|
|
|
options={timezones as ITimeZone[]}
|
|
|
|
|
getOptionLabel={(option) => option.name}
|
|
|
|
|
onChange={(_, timezone: ITimeZone) => {
|
|
|
|
|
dispatch(setTimezone(timezone._id));
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</Stack>
|
|
|
|
|
<Stack spacing={theme.spacing(2)}>
|
|
|
|
|
<Typography>UI Mode</Typography>
|
|
|
|
|
<Select
|
|
|
|
|
value={mode}
|
|
|
|
|
onChange={(e: SelectChangeEvent<string>) => {
|
|
|
|
|
dispatch(setMode(e.target.value));
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<MenuItem value="light">Light</MenuItem>
|
|
|
|
|
<MenuItem value="dark">Dark</MenuItem>
|
|
|
|
|
</Select>
|
|
|
|
|
</Stack>
|
|
|
|
|
<Stack spacing={theme.spacing(2)}>
|
|
|
|
|
<Typography>Language</Typography>
|
|
|
|
|
<LanguageSelector />
|
|
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2025-11-19 23:55:29 +00:00
|
|
|
<SettingsForm
|
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
|
onTest={onTest}
|
|
|
|
|
initialData={initialSettings}
|
|
|
|
|
loading={posting || loading || patching}
|
|
|
|
|
/>
|
2025-11-13 13:47:26 -08:00
|
|
|
</BasePage>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SettingsPage;
|