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