Skip to content

Commit

Permalink
✨ Feat: 기본 카테고리 제목 수정 방지 & 스크랩 추가 시 invalidate
Browse files Browse the repository at this point in the history
- fix: 카테고리 url preview 썸네일 프로퍼티 수정
- feat: 기본 카테고리 제목 변경 시 에러 메세지
- improve: 카테고리 내에서 스크랩 추가 시 해당 카테고리 query invalidate

todo
- error message handling
  • Loading branch information
elbica committed Nov 25, 2022
1 parent 04a4388 commit 24c5c8b
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/application/hooks/api/scrap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ export const useSaveScrap = () => {
return useMutation({
mutationKey: ['saveScrap'],
mutationFn: (args: Parameters<typeof api.scrap.saveScrap>[0]) => api.scrap.saveScrap(args),
onSuccess: () => queryClient.clear(),
onSuccess: (d, v) => {
queryClient.invalidateQueries(['getCategories', v.category_id]);
queryClient.invalidateQueries(['getContentByCategory', v.category_id]);
},
});
};

Expand All @@ -68,8 +71,6 @@ export const useUpdateScrap = (id: number) => {
return useMutation({
mutationKey: ['updateScrap'],
mutationFn: (args: Parameters<typeof api.scrap.updateScrap>[0]) => api.scrap.updateScrap({ ...args, id }),
onSuccess: () => {
queryClient.invalidateQueries(['getScrapById', id]);
},
onSuccess: () => queryClient.invalidateQueries(['getScrapById', id]),
});
};
2 changes: 2 additions & 0 deletions src/application/utils/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const PASSWORD_REGEXP = /^.*(?=^.{8,15}$)(?=.*[a-zA-Z])(?=.*[!@#$%^&+=]).
export const ERR_CODE = {
CREATE_DUPLICATED_CATEGORY: 4004,
MODIFY_DUPLICATED_CATEGORY: 4005,
NOT_MODIFY_DEFAULT_CATEGORY: 4015,
} as const;

export const ERR_MESSAGE = {
DUPLICATED_TITLE: '이미 있는 제목입니다.',
NOT_MODIFY_DEFAULT_CATEGORY: '기본 카테고리는 수정할 수 없습니다.',
} as const;
4 changes: 2 additions & 2 deletions src/application/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const getSrcByType = (content: Scrap) => {
switch (content.scrap_type) {
export const getSrcByType = (content: Scrap | Category) => {
switch (content.scrap_type.toLowerCase()) {
case 'link':
return content.url_preview;
case 'image':
Expand Down
6 changes: 4 additions & 2 deletions src/components/category/Modal/CreateCategory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import InputModal from '@/components/common/Modal/Input';

interface CreateCategoryProps {
onSubmit?: (category: string, errorFn: Dispatch<SetStateAction<boolean>>) => void;
errMsg?: string;
}

const CreateCategory = ({ onSubmit }: CreateCategoryProps) => (
<InputModal title={'카테고리명'} errMsg={ERR_MESSAGE.DUPLICATED_TITLE} onSubmit={onSubmit} />
// TODO error handler로 에러 메세지 관리
const CreateCategory = ({ onSubmit, errMsg = ERR_MESSAGE.DUPLICATED_TITLE }: CreateCategoryProps) => (
<InputModal title={'카테고리명'} errMsg={errMsg} onSubmit={onSubmit} />
);

export default CreateCategory;
4 changes: 3 additions & 1 deletion src/containers/scrap/CategoryDetailContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useState } from 'react';
import { useGetContentByCategory, useUpdateCategory } from '@/application/hooks/api/category';
import useModal from '@/application/hooks/common/useModal';
import usePopup from '@/application/hooks/common/usePopup';
import { ERR_CODE } from '@/application/utils/constant';
import { ERR_CODE, ERR_MESSAGE } from '@/application/utils/constant';
import CreateCategory from '@/components/category/Modal/CreateCategory';
import PhotoListContainer from '@/containers/scrap/PhotoListContainer';

Expand Down Expand Up @@ -41,6 +41,7 @@ const CategoryDetailContainer = ({ select, info }: CategoryDetailContainerProps)
onClick={() =>
show(
<CreateCategory
errMsg={ERR_MESSAGE.NOT_MODIFY_DEFAULT_CATEGORY}
onSubmit={(category, setError) => {
mutation.mutate(
{ id: info.id, name: category },
Expand All @@ -52,6 +53,7 @@ const CategoryDetailContainer = ({ select, info }: CategoryDetailContainerProps)
onError: (err) => {
if (axios.isAxiosError(err)) {
err.response?.data.code === ERR_CODE.MODIFY_DUPLICATED_CATEGORY && setError(true);
err.response?.data.code === ERR_CODE.NOT_MODIFY_DEFAULT_CATEGORY && setError(true);
}
},
},
Expand Down
3 changes: 2 additions & 1 deletion src/containers/scrap/CategoryListContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { css } from '@emotion/react';
import React from 'react';

import { useGetCategories } from '@/application/hooks/api/category';
import { getSrcByType } from '@/application/utils/helper';
import CategoryListItem from '@/components/category/List/CategoryListItem';
interface CategoryListContainerProps {
select: boolean;
Expand Down Expand Up @@ -34,7 +35,7 @@ const CategoryListContainer = ({ select, onClickItem }: CategoryListContainerPro
<CategoryListItem
onClick={() => onClickItem({ id: category.id, name: category.name })}
select={select}
src={category.file_url ?? '/icon/scrap/defaultCategory.svg'}
src={getSrcByType(category) ?? '/icon/scrap/defaultCategory.svg'}
title={category.name}
key={category.id}
/>
Expand Down
3 changes: 2 additions & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ type ScrapType = 'image' | 'text' | 'link' | 'video' | 'pdf';
interface Category {
content: string;
file_url: string;
url_preview: string;
id: number;
name: string;
scrapType: ScrapType;
scrap_type: ScrapType;
}

interface Scrap {
Expand Down

0 comments on commit 24c5c8b

Please sign in to comment.