From 4366aa29127826a18d0bd2efe368f8a236e0155e Mon Sep 17 00:00:00 2001 From: sujikim Date: Wed, 29 Nov 2023 13:54:48 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=8F=84=EC=84=9C=EB=93=B1=EB=A1=9D=20?= =?UTF-8?q?=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC=20=EC=A0=9C=EB=8C=80?= =?UTF-8?q?=EB=A1=9C=20=EC=A0=81=EC=9A=A9=EB=90=98=EC=A7=80=20=EC=95=8A?= =?UTF-8?q?=EB=8D=98=20=EB=AC=B8=EC=A0=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/books/useGetBooksCreate.ts | 51 ++++++------------- src/api/books/usePostBooksCreate.ts | 48 ++++++----------- src/component/addbook/AddBook.tsx | 10 ++-- .../addbook/AddBookDisplayBasicBookInfo.tsx | 9 ++-- ...AddBookRegisterBookWithUsersExtraInput.tsx | 40 +++++++-------- src/type/BookInfo.ts | 1 - 6 files changed, 58 insertions(+), 101 deletions(-) diff --git a/src/api/books/useGetBooksCreate.ts b/src/api/books/useGetBooksCreate.ts index efef74e3..c17513f5 100644 --- a/src/api/books/useGetBooksCreate.ts +++ b/src/api/books/useGetBooksCreate.ts @@ -1,38 +1,16 @@ -import { useState, useEffect } from "react"; -import { useApi } from "../../hook/useApi"; -import { compareExpect } from "../../util/typeCheck"; -import getErrorMessage from "../../constant/error"; -import { Book } from "../../type"; +import { useState } from "react"; +import { useApi } from "~/hook/useApi"; +import getErrorMessage from "~/constant/error"; +import { type BookInfo } from "~/type"; -export const useGetBooksCreate = (defalutBook: Book) => { - const [isbnQuery, setIsbnQuery] = useState(""); +export const useGetBooksCreate = (defalutBook: Omit) => { const [bookInfo, setBookInfo] = useState(defalutBook); const [errorMessage, setErrorMessage] = useState(""); - const { request } = useApi("get", "books/create", { - isbnQuery, - }); - - const expectedItem = [ - { key: "title", type: "string", isNullable: false }, - { key: "author", type: "string", isNullable: false }, - { key: "publisher", type: "string", isNullable: false }, - { key: "pubdate", type: "string", isNullable: false }, - { key: "category", type: "string", isNullable: false }, - { key: "image", type: "string", isNullable: true }, - ]; + const { requestWithUrl } = useApi(); const refineResponse = (response: any) => { - const books = compareExpect( - "books/create", - [response.data.bookInfo], - expectedItem, - ); - setBookInfo({ - ...books[0], - isbn: isbnQuery, - koreanDemicalClassification: books[0].category, - }); + setBookInfo(response.data.bookInfo); setErrorMessage(""); }; @@ -46,11 +24,14 @@ export const useGetBooksCreate = (defalutBook: Book) => { setBookInfo(defalutBook); }; - useEffect(() => { - if (isbnQuery && isbnQuery.length) { - request(refineResponse, displayError); - } - }, [isbnQuery]); + const fetchData = (isbnQuery: string) => { + if (!isbnQuery) return; + requestWithUrl("get", "books/create", { + data: { isbnQuery }, + onSuccess: refineResponse, + onError: displayError, + }); + }; - return { bookInfo, errorMessage, fetchData: setIsbnQuery, setBookInfo }; + return { bookInfo, errorMessage, fetchData, setBookInfo }; }; diff --git a/src/api/books/usePostBooksCreate.ts b/src/api/books/usePostBooksCreate.ts index 38e5cc84..c9b73313 100644 --- a/src/api/books/usePostBooksCreate.ts +++ b/src/api/books/usePostBooksCreate.ts @@ -1,29 +1,17 @@ -import { useState, useEffect } from "react"; -import { useApi } from "../../hook/useApi"; -import getErrorMessage from "../../constant/error"; -import { compareExpect } from "../../util/typeCheck"; -import { BookInfo } from "../../type"; +import { useState } from "react"; +import { useNavigate } from "react-router-dom"; +import { useApi } from "~/hook/useApi"; +import getErrorMessage from "~/constant/error"; +import { type BookInfo } from "~/type"; export const usePostBooksCreate = () => { - const [newBookInfo, setNewBookInfo] = useState(null); const [message, setMessage] = useState(""); - - const { request } = useApi("post", "books/create", newBookInfo); - - const expectedItem = [ - { key: "title", type: "string", isNullable: false }, - { key: "isbn", type: "string", isNullable: true }, - { key: "author", type: "string", isNullable: false }, - { key: "publisher", type: "string", isNullable: false }, - { key: "pubdate", type: "string", isNullable: false }, - { key: "categoryId", type: "string", isNullable: false }, - { key: "image", type: "string", isNullable: true }, - { key: "donator", type: "string", isNullable: true }, - ]; + const navigate = useNavigate(); + const { requestWithUrl } = useApi(); const displaySuccess = () => { setMessage("등록되었습니다!"); - window.location.reload(); + navigate(0); }; const displayError = (error: any) => { @@ -32,19 +20,13 @@ export const usePostBooksCreate = () => { setMessage(`실패했습니다. ${errorMessage} `); }; - const registerBook = (newBook: BookInfo) => { - const [book] = compareExpect("books/create", [newBook], expectedItem); - setNewBookInfo(book); + const registerBook = (newBook: Omit) => { + requestWithUrl("post", "books/create", { + data: newBook, + onSuccess: displaySuccess, + onError: displayError, + }); }; - useEffect(() => { - if (newBookInfo) { - request(displaySuccess, displayError); - } - }, [newBookInfo]); - - return { - message, - registerBook, - }; + return { message, registerBook }; }; diff --git a/src/component/addbook/AddBook.tsx b/src/component/addbook/AddBook.tsx index 1e6c15c1..b03fb141 100644 --- a/src/component/addbook/AddBook.tsx +++ b/src/component/addbook/AddBook.tsx @@ -1,5 +1,5 @@ import { useState } from "react"; -import { useGetBooksCreate } from "../../api/books/useGetBooksCreate"; +import { useGetBooksCreate } from "~/api/books/useGetBooksCreate"; import RegisterBookWithUsersExtraInput from "./AddBookRegisterBookWithUsersExtraInput"; import DisplayBasicBookInfo from "./AddBookDisplayBasicBookInfo"; @@ -9,9 +9,9 @@ import Banner from "../utils/Banner"; import BarcodeReader from "../utils/BarcodeReader"; import InquireBoxTitle from "../utils/InquireBoxTitle"; -import { bookManagementTabList } from "../../constant/tablist"; -import Book from "../../asset/img/admin_icon.svg"; -import "../../asset/css/AddBook.css"; +import { bookManagementTabList } from "~/constant/tablist"; +import Book from "~/asset/img/admin_icon.svg"; +import "~/asset/css/AddBook.css"; const AddBook = () => { const [isUsingBarcodeReader, setUsingBarcodeReader] = useState(true); @@ -22,7 +22,7 @@ const AddBook = () => { author: "", publisher: "", pubdate: "", - koreanDemicalClassification: "", + category: "", }; const { bookInfo, errorMessage, fetchData, setBookInfo } = diff --git a/src/component/addbook/AddBookDisplayBasicBookInfo.tsx b/src/component/addbook/AddBookDisplayBasicBookInfo.tsx index 50582964..dd1a8c69 100644 --- a/src/component/addbook/AddBookDisplayBasicBookInfo.tsx +++ b/src/component/addbook/AddBookDisplayBasicBookInfo.tsx @@ -1,5 +1,5 @@ import { ChangeEventHandler, useState } from "react"; -import { BookInfo } from "../../type"; +import { BookInfo } from "~/type"; const labelText: { [key: string]: string; @@ -10,8 +10,8 @@ const labelText: { }; type Props = { - bookInfo: BookInfo & { [key: string]: string }; - setBookInfo: (bookinfo: BookInfo) => void; + bookInfo: Omit; + setBookInfo: (bookinfo: Omit) => void; }; const DisplayBasicBookInfo = ({ bookInfo, setBookInfo }: Props) => { @@ -48,6 +48,7 @@ const DisplayBasicBookInfo = ({ bookInfo, setBookInfo }: Props) => { /> {Object.keys(labelText).map(key => { + const bookInfoKey = key as keyof Omit; return (