bazarr/frontend/src/pages/Settings/utilities/FormValues.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

import { LOG } from "@/utilities/console";
import type { UseFormReturnType } from "@mantine/form";
2022-05-31 15:49:04 +00:00
import { createContext, useCallback, useContext, useRef } from "react";
export const FormContext = createContext<UseFormReturnType<FormValues> | null>(
null
);
2022-05-31 15:49:04 +00:00
export function useFormValues() {
const context = useContext(FormContext);
if (context === null) {
throw new Error("useFormValues must be used within a FormContext");
}
return context;
}
export function useStagedValues() {
const form = useFormValues();
return { ...form.values.settings };
2022-05-31 15:49:04 +00:00
}
export function useFormActions() {
const form = useFormValues();
const formRef = useRef(form);
formRef.current = form;
const update = useCallback((object: LooseObject) => {
LOG("info", `Updating values`, object);
formRef.current.setValues((values) => {
const changes = { ...values.settings, ...object };
return { ...values, settings: changes };
});
}, []);
const setValue = useCallback((v: unknown, key: string) => {
LOG("info", `Updating value of ${key}`, v);
formRef.current.setValues((values) => {
const changes = { ...values.settings, [key]: v };
return { ...values, settings: changes };
});
}, []);
2022-05-31 15:49:04 +00:00
return { update, setValue };
}
export type FormKey = keyof FormValues;
export type FormValues = {
// Settings that saved to the backend
settings: LooseObject;
// Settings that saved to the frontend
// storages: LooseObject;
2022-05-31 15:49:04 +00:00
};