refactor: unify bank form types and actions
This commit is contained in:
@@ -5,6 +5,8 @@ import { validationMessage } from "@/shared/i18n";
|
||||
import type { ExpenseCategory } from "../api/types";
|
||||
const MAX_CATEGORY_NAME_LENGTH = 80;
|
||||
|
||||
type CategoryValues = z.infer<ReturnType<typeof categorySchema>>;
|
||||
|
||||
function categorySchema(categories?: ExpenseCategory[], editingId?: string) {
|
||||
return z.object({
|
||||
name: z
|
||||
@@ -27,4 +29,4 @@ function categorySchema(categories?: ExpenseCategory[], editingId?: string) {
|
||||
});
|
||||
}
|
||||
|
||||
export { categorySchema };
|
||||
export { categorySchema, type CategoryValues };
|
||||
|
||||
@@ -12,16 +12,14 @@ import { type FC } from "react";
|
||||
import { FormProvider, useController, useFormContext } from "react-hook-form";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import type { CategoryValues } from "../../model/category.schema";
|
||||
|
||||
import { bankOperationAppearance } from "../../model/operation-appearance";
|
||||
import { BankFeedback } from "../BankFeedback";
|
||||
import { useCategoryManagerVm } from "./CategoryManager.vm";
|
||||
|
||||
type CategoryManagerProps = {
|
||||
onClose: () => void;
|
||||
onCreated?: (id: string) => void;
|
||||
};
|
||||
|
||||
type CategoryValues = { name: string };
|
||||
import {
|
||||
type CategoryManagerProps,
|
||||
useCategoryManagerVm,
|
||||
} from "./CategoryManager.vm";
|
||||
|
||||
const CategoryFields: FC = () => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
useUpdateExpenseCategory,
|
||||
} from "../../api/mutations";
|
||||
import { useBankCategories } from "../../api/queries";
|
||||
import { categorySchema } from "../../model/category.schema";
|
||||
import { categorySchema, type CategoryValues } from "../../model/category.schema";
|
||||
import { useBankFormFocus } from "../useBankFormFocus";
|
||||
|
||||
type CategoryManagerProps = {
|
||||
@@ -20,8 +20,6 @@ type CategoryManagerProps = {
|
||||
onCreated?: (id: string) => void;
|
||||
};
|
||||
|
||||
type CategoryValues = { name: string };
|
||||
|
||||
function useCategoryManagerVm(props: CategoryManagerProps) {
|
||||
useBankFormFocus();
|
||||
const [opened, setOpened] = useState(true);
|
||||
@@ -43,6 +41,13 @@ function useCategoryManagerVm(props: CategoryManagerProps) {
|
||||
const isPending = !opened || methods.formState.isSubmitting;
|
||||
const onRequestClose = () => !isPending && setOpened(false);
|
||||
|
||||
const finishEditing = () => {
|
||||
editSessionRef.current++;
|
||||
setEditing(undefined);
|
||||
setLatest(undefined);
|
||||
methods.reset({ name: "" });
|
||||
};
|
||||
|
||||
const onSubmit: ComponentProps<"form">["onSubmit"] = (event) => {
|
||||
void methods.handleSubmit(async (values) => {
|
||||
setError(undefined);
|
||||
@@ -61,10 +66,7 @@ function useCategoryManagerVm(props: CategoryManagerProps) {
|
||||
if (result) props.onCreated?.(result.id);
|
||||
}
|
||||
|
||||
editSessionRef.current++;
|
||||
setEditing(undefined);
|
||||
setLatest(undefined);
|
||||
methods.reset({ name: "" });
|
||||
finishEditing();
|
||||
}
|
||||
catch (error_) {
|
||||
setError(error_);
|
||||
@@ -90,10 +92,7 @@ function useCategoryManagerVm(props: CategoryManagerProps) {
|
||||
};
|
||||
|
||||
const onCancel = () => {
|
||||
editSessionRef.current++;
|
||||
setEditing(undefined);
|
||||
setLatest(undefined);
|
||||
methods.reset({ name: "" });
|
||||
finishEditing();
|
||||
setError(undefined);
|
||||
};
|
||||
|
||||
@@ -131,4 +130,4 @@ function useCategoryManagerVm(props: CategoryManagerProps) {
|
||||
};
|
||||
}
|
||||
|
||||
export { useCategoryManagerVm };
|
||||
export { type CategoryManagerProps, useCategoryManagerVm };
|
||||
|
||||
Reference in New Issue
Block a user