forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCategoryEditPage.tsx
136 lines (122 loc) · 4.19 KB
/
CategoryEditPage.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { zodResolver } from "@hookform/resolvers/zod";
import { Box, Typography } from "@mui/material";
import React, { FC, useCallback, useEffect } from "react";
import { useForm } from "react-hook-form";
import { useNavigate } from "react-router";
import { AironeLink } from "components";
import { CategoryForm } from "components/category/CategoryForm";
import {
Schema,
schema,
} from "components/category/categoryForm/CategoryFormSchema";
import { AironeBreadcrumbs } from "components/common/AironeBreadcrumbs";
import { Loading } from "components/common/Loading";
import { PageHeader } from "components/common/PageHeader";
import { SubmitButton } from "components/common/SubmitButton";
import { useAsyncWithThrow } from "hooks/useAsyncWithThrow";
import { useFormNotification } from "hooks/useFormNotification";
import { usePrompt } from "hooks/usePrompt";
import { useTypedParams } from "hooks/useTypedParams";
import { aironeApiClient } from "repository/AironeApiClient";
import { listCategoryPath, topPath } from "routes/Routes";
import {
extractAPIException,
isResponseError,
} from "services/AironeAPIErrorUtil";
export const CategoryEditPage: FC = () => {
const { categoryId } = useTypedParams<{ categoryId?: number }>();
const willCreate = categoryId == null;
const navigate = useNavigate();
const { enqueueSubmitResult } = useFormNotification("カテゴリ", willCreate);
const {
formState: { isValid, isDirty, isSubmitting, isSubmitSuccessful },
handleSubmit,
reset,
setError,
setValue,
control,
} = useForm<Schema>({
resolver: zodResolver(schema),
mode: "onBlur",
});
usePrompt(
isDirty && !isSubmitSuccessful,
"編集した内容は失われてしまいますが、このページを離れてもよろしいですか?"
);
const category = useAsyncWithThrow(async () => {
return categoryId != null
? await aironeApiClient.getCategory(categoryId)
: undefined;
}, [categoryId]);
useEffect(() => {
!category.loading && category.value != null && reset(category.value);
}, [category.loading]);
useEffect(() => {
isSubmitSuccessful && navigate(listCategoryPath());
}, [isSubmitSuccessful]);
const handleSubmitOnValid = useCallback(
async (category: Schema) => {
// Note: This might not necessary any more
category = { ...category, priority: Number(category.priority) };
try {
if (willCreate) {
await aironeApiClient.createCategory(category);
} else {
await aironeApiClient.updateCategory(categoryId, category);
}
enqueueSubmitResult(true);
} catch (e) {
if (e instanceof Error && isResponseError(e)) {
await extractAPIException<Schema>(
e,
(message) => enqueueSubmitResult(false, `詳細: "${message}"`),
(name, message) => {
setError(name, { type: "custom", message: message });
enqueueSubmitResult(false);
}
);
} else {
enqueueSubmitResult(false);
}
}
},
[categoryId]
);
const handleCancel = async () => {
navigate(-1);
};
if (category.loading) {
return <Loading />;
}
return (
<Box className="container-fluid">
<AironeBreadcrumbs>
<Typography component={AironeLink} to={topPath()}>
Top
</Typography>
<Typography component={AironeLink} to={listCategoryPath()}>
カテゴリ一覧
</Typography>
<Typography color="textPrimary">カテゴリ編集</Typography>
</AironeBreadcrumbs>
<PageHeader
title={
category.value != null ? category.value.name : "新規カテゴリの作成"
}
description={category.value != null ? "カテゴリ編集" : undefined}
>
<SubmitButton
name="保存"
disabled={
!isDirty || !isValid || isSubmitting || isSubmitSuccessful
//category.value?. === false
}
isSubmitting={isSubmitting}
handleSubmit={handleSubmit(handleSubmitOnValid)}
handleCancel={handleCancel}
/>
</PageHeader>
<CategoryForm control={control} setValue={setValue} />
</Box>
);
};