Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] 삭제된 addText가 포함된 PR이 머지되어 발생하는 오류 해결 #764

Merged
merged 2 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions frontend/src/components/PostForm/CategoryWrapper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { useCategoryList } from '@hooks/query/category/useCategoryList';
import MultiSelect from '@components/common/MultiSelect';
import { Option } from '@components/common/MultiSelect/types';

import { POST_CATEGORY } from '@constants/policy';

import { changeCategoryToOption } from '@utils/post/changeCategoryToOption';

interface CategoryWrapperProps {
Expand All @@ -30,6 +32,7 @@ export default function CategoryWrapper({ multiSelectHook }: CategoryWrapperProp
handleOptionAdd={handleOptionAdd}
handleOptionDelete={handleOptionDelete}
placeholder="카테고리를 선택해주세요."
maxOptionCount={POST_CATEGORY.MAX_AMOUNT}
/>
);
}
6 changes: 1 addition & 5 deletions frontend/src/components/PostForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ export default function PostForm({ data, mutate, isSubmitting }: PostFormProps)

const categorySelectHook = useMultiSelect(categoryIds ?? [], POST_CATEGORY.MAX_AMOUNT);
const { text: writingTitle, handleTextChange: handleTitleChange } = useText(title ?? '');
const {
text: writingContent,
handleTextChange: handleContentChange,
addText: addContent,
} = useText(content ?? '');
const { text: writingContent, handleTextChange: handleContentChange } = useText(content ?? '');
const writingOptionHook = useWritingOption(
serverVoteInfo?.options.map(option => ({
...option,
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/components/common/MultiSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ interface MultiSelectProps {
handleOptionAdd: (newItem: Option) => void;
handleOptionDelete: (optionId: number) => void;
placeholder?: string;
maxOptionCount?: number;
}

export default function MultiSelect({
selectedOptionList,
optionList,
handleOptionAdd,
handleOptionDelete,
maxOptionCount,
placeholder = '여러 개의 옵션을 선택해주세요',
}: MultiSelectProps) {
const [isOpen, setIsOpen] = useState(false);
Expand All @@ -35,6 +37,15 @@ export default function MultiSelect({
setIsOpen(!isOpen);
};

const addOption = ({ id, name }: Option) => {
handleOptionAdd({ id, name });

// selectedOptionList.length 는 추가되기 이전의 값
if (maxOptionCount && selectedOptionList.length + 1 >= maxOptionCount) {
setIsOpen(false);
}
};

const handleOutsideClick = (event: MouseEvent) => {
if (wrapperRef.current && !wrapperRef.current.contains(event.target as Node)) {
setIsOpen(false);
Expand Down Expand Up @@ -90,7 +101,7 @@ export default function MultiSelect({
aria-label={`클릭시 ${name} 추가`}
onClick={e => {
e.stopPropagation();
handleOptionAdd({ id, name });
addOption({ id, name });
}}
>
{name}
Expand Down
8 changes: 6 additions & 2 deletions frontend/src/hooks/useMultiSelect.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { useState } from 'react';
import { useContext, useState } from 'react';

import type { Option } from '@components/common/MultiSelect/types';

import { ToastContext } from './context/toast';

export const useMultiSelect = (initialSelectedOptionList: Option[], optionCountLimit?: number) => {
const [selectedOptionList, setSelectedOptionList] = useState(initialSelectedOptionList);
const { addMessage } = useContext(ToastContext);

const handleOptionAdd = (newItem: Option) => {
if (optionCountLimit && optionCountLimit === selectedOptionList.length) {
alert(`${optionCountLimit}개까지 선택 가능합니다!`);
addMessage(`${optionCountLimit}개까지 선택 가능합니다!`);
return;
}

setSelectedOptionList([...selectedOptionList, newItem]);
};

Expand Down
Loading