Skip to content

Commit

Permalink
fix: 도서등록 카테고리 제대로 적용되지 않던 문제 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
not-using committed Nov 29, 2023
1 parent dc2e985 commit 4366aa2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 101 deletions.
51 changes: 16 additions & 35 deletions src/api/books/useGetBooksCreate.ts
Original file line number Diff line number Diff line change
@@ -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<BookInfo, "id">) => {
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("");
};

Expand All @@ -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 };
};
48 changes: 15 additions & 33 deletions src/api/books/usePostBooksCreate.ts
Original file line number Diff line number Diff line change
@@ -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<BookInfo | null>(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) => {
Expand All @@ -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<BookInfo, "id">) => {
requestWithUrl("post", "books/create", {
data: newBook,
onSuccess: displaySuccess,
onError: displayError,
});
};

useEffect(() => {
if (newBookInfo) {
request(displaySuccess, displayError);
}
}, [newBookInfo]);

return {
message,
registerBook,
};
return { message, registerBook };
};
10 changes: 5 additions & 5 deletions src/component/addbook/AddBook.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand All @@ -22,7 +22,7 @@ const AddBook = () => {
author: "",
publisher: "",
pubdate: "",
koreanDemicalClassification: "",
category: "",
};

const { bookInfo, errorMessage, fetchData, setBookInfo } =
Expand Down
9 changes: 5 additions & 4 deletions src/component/addbook/AddBookDisplayBasicBookInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChangeEventHandler, useState } from "react";
import { BookInfo } from "../../type";
import { BookInfo } from "~/type";

const labelText: {
[key: string]: string;
Expand All @@ -10,8 +10,8 @@ const labelText: {
};

type Props = {
bookInfo: BookInfo & { [key: string]: string };
setBookInfo: (bookinfo: BookInfo) => void;
bookInfo: Omit<BookInfo, "id" | "books">;
setBookInfo: (bookinfo: Omit<BookInfo, "id" | "books">) => void;
};

const DisplayBasicBookInfo = ({ bookInfo, setBookInfo }: Props) => {
Expand Down Expand Up @@ -48,6 +48,7 @@ const DisplayBasicBookInfo = ({ bookInfo, setBookInfo }: Props) => {
/>
</label>
{Object.keys(labelText).map(key => {
const bookInfoKey = key as keyof Omit<BookInfo, "id" | "books">;
return (
<label htmlFor={key} className="add-book__book-info__text">
<span className="font-16-bold add-book__book-info__text-key">
Expand All @@ -57,7 +58,7 @@ const DisplayBasicBookInfo = ({ bookInfo, setBookInfo }: Props) => {
className="add-book__basic-info__input "
type="text"
id={key}
value={bookInfo[key]}
value={bookInfo[bookInfoKey]}
onChange={onChangeInput}
required
/>
Expand Down
40 changes: 17 additions & 23 deletions src/component/addbook/AddBookRegisterBookWithUsersExtraInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ import {
ChangeEventHandler,
FormEventHandler,
} from "react";
import { category, koreanDemicalClassification } from "../../constant/category";
import { usePostBooksCreate } from "../../api/books/usePostBooksCreate";
import { BookInfo } from "../../type";
import {
category,
koreanDemicalClassification as 중앙도서관도서분류,
} from "~/constant/category";
import { usePostBooksCreate } from "~/api/books/usePostBooksCreate";
import { BookInfo } from "~/type";

type Props = {
bookInfo: BookInfo;
bookInfo: Omit<BookInfo, "id">;
};

const RegisterBookWithUsersExtraInput = ({ bookInfo }: Props) => {
const [isDevBook, setIsDevBook] = useState(false);
const [isDevBook, setIsDevBook] = useState<"" | "true" | "false">("");
const [categoryId, setCategoryId] = useState("");
const donator = useRef<HTMLInputElement>(null);
const isReadyToPost = categoryId && bookInfo.title && bookInfo.author;

useEffect(() => {
setIsDevBook(false);
setIsDevBook("");
setCategoryId("");
}, [bookInfo]);

const { message, registerBook } = usePostBooksCreate();

const onChangeCategory: ChangeEventHandler<HTMLSelectElement> = e => {
setCategoryId(e.currentTarget.value);
};

const onSubmit: FormEventHandler = e => {
e.preventDefault();
registerBook({
Expand All @@ -38,17 +38,11 @@ const RegisterBookWithUsersExtraInput = ({ bookInfo }: Props) => {
});
};

const isReadyToPost = () => {
return categoryId && bookInfo.title && bookInfo.author;
};

const setDev: ChangeEventHandler<HTMLSelectElement> = e => {
const value = e.currentTarget.value === "true";
if (!value && bookInfo?.koreanDemicalClassification) {
const id = koreanDemicalClassification.find(
i => i.id === bookInfo.koreanDemicalClassification,
)?.categoryId;
id && setCategoryId(id);
const { value } = e.currentTarget;
if (value === "true" || value === "false") {
const id = 중앙도서관도서분류.find(i => i.id === bookInfo.category);
if (id) setCategoryId(id.categoryId);
setIsDevBook(value);
}
};
Expand All @@ -75,11 +69,11 @@ const RegisterBookWithUsersExtraInput = ({ bookInfo }: Props) => {
name="category"
id="category-select"
value={categoryId}
onChange={onChangeCategory}
onChange={e => setCategoryId(e.currentTarget.value)}
>
<option value="">카테고리를 선택하세요</option>
{category
?.filter(items => items.isDev === isDevBook)
?.filter(items => `${items.isDev}` === isDevBook)
?.map(element => {
return (
<option value={element.id} key={element.id}>
Expand All @@ -92,7 +86,7 @@ const RegisterBookWithUsersExtraInput = ({ bookInfo }: Props) => {
<p className="color-red">기부자 정보</p>
<input type="text" id="donator" ref={donator} />
<p className="add-book__create-form__errror-Message">{message}</p>
<button type="submit" className={isReadyToPost() && "red"}>
<button type="submit" className={isReadyToPost && "red"}>
등록하기
</button>
</form>
Expand Down
1 change: 0 additions & 1 deletion src/type/BookInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export type BookInfo = {
publisher: string;
publishedAt?: string;
image?: string;
koreanDemicalClassification?: string;
donator?: string;
books?: Book[];
};
Expand Down

0 comments on commit 4366aa2

Please sign in to comment.