Skip to content

Commit

Permalink
Refactor: 숙소 등록 플로우 리팩토링 (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
xxxjinn authored Jan 27, 2024
1 parent 47a6902 commit b787459
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 14 deletions.
15 changes: 10 additions & 5 deletions src/components/init/ButtonContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ROUTES } from '@/constants/routes';
import { useRecoilState, useSetRecoilState } from 'recoil';
import {
isUpdatedAccommodationState,
isUpdatedRoomState,
roomPrevButtonState,
userInputValueState,
} from '@stores/init/atoms';
Expand All @@ -27,9 +28,11 @@ export const ButtonContainer = ({
const setIsClickPrev = useSetRecoilState(roomPrevButtonState);

const handlePreviousClick = () => {
if (window.location.pathname === ROUTES.INIT_ACCOMMODATION_REGISTRATION)
navigate(-1);
else if (window.location.pathname === ROUTES.INIT_ROOM_REGISTRATION) {
if (window.location.pathname === ROUTES.INIT_ACCOMMODATION_REGISTRATION) {
const accommodationId = getCookie('accommodationId');
if (accommodationId) navigate(`/${accommodationId}${ROUTES.MAIN}`);
else navigate(ROUTES.INIT);
} else if (window.location.pathname === ROUTES.INIT_ROOM_REGISTRATION) {
setIsClickPrev(true);
navigate(ROUTES.INIT_ACCOMMODATION_REGISTRATION);
}
Expand All @@ -41,6 +44,7 @@ export const ButtonContainer = ({
const setIsUpdatedAccommodation = useSetRecoilState(
isUpdatedAccommodationState,
);
const setIsUpdatedRoom = useSetRecoilState(isUpdatedRoomState);

const imageUrls: { url: string }[] = userInputValue[0].images.map(
(image) => ({ url: image.url }),
Expand Down Expand Up @@ -73,6 +77,7 @@ export const ButtonContainer = ({
onSuccess(data) {
accommodationId = data.data.accommodationId;
setIsUpdatedAccommodation(false);
setIsUpdatedRoom(false);

const cookieAccommodationId = getCookie('accommodationId');
if (!cookieAccommodationId) setCookie('accommodationId', accommodationId);
Expand Down Expand Up @@ -111,7 +116,7 @@ export const ButtonContainer = ({
if (error instanceof AxiosError) {
message.error({
content: '요청에 실패했습니다. 잠시 후 다시 시도해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
if (
Expand All @@ -124,7 +129,7 @@ export const ButtonContainer = ({
) {
message.error({
content: '요청을 실패했습니다. 관리자에게 문의해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
if (error.response?.data.code === RESPONSE_CODE.NOT_FOUND_MEMBER) {
Expand Down
21 changes: 21 additions & 0 deletions src/components/init/CheckBoxContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ export const CheckBoxContainer = ({
}
}, [defaultValue]);

useEffect(() => {
return () => {
setSelectedInitRoomOptions({
airCondition: false,
tv: false,
internet: false,
});
setSelectedAccommodationOptions({
cooking: false,
parking: false,
pickup: false,
barbecue: false,
fitness: false,
karaoke: false,
sauna: false,
sports: false,
seminar: false,
});
};
}, []);

const handleCheckboxChange = (event: CheckboxChangeEvent) => {
const checkedOption = event.target.value;

Expand Down
6 changes: 6 additions & 0 deletions src/components/init/ImageUploadContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export const ImageUploadContainer = ({
}
}, [images]);

useEffect(() => {
return () => {
setImageFile([]);
};
}, []);

const [imageFile, setImageFile] = useRecoilState(imageFileState);
const setRemovedImageFile = useSetRecoilState(deletedImageFileState);

Expand Down
4 changes: 2 additions & 2 deletions src/components/init/init-info-confirmation/RoomItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const RoomItem = () => {
if (userInputValue[0].rooms.length === 1) {
message.error({
content: '최소 1개의 객실이 등록되어야 합니다.',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
return;
}
Expand All @@ -60,7 +60,7 @@ export const RoomItem = () => {

message.success({
content: '삭제되었습니다.',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/constants/init/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const NAME_REGEX = /[^ㄱ-ㅎ가-힣A-Za-z0-9\s]/g;
export const NAME_REGEX = /[^ㄱ-ㅎㅏ-ㅣ가-힣A-Za-z0-9\s]/g;

export const IMAGE_MAX_COUNT = 5;
export const IMAGE_MAX_CAPACITY = 30;
12 changes: 10 additions & 2 deletions src/pages/init-accommodation-registration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ export const InitAccommodationRegistration = () => {
if (error instanceof AxiosError) {
message.error({
content: '요청에 실패했습니다. 잠시 후 다시 시도해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
if (error.response?.data.code === RESPONSE_CODE.IMAGE_SAVE_FAIL) {
message.error({
content: '요청을 실패했습니다. 관리자에게 문의해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
},
Expand Down Expand Up @@ -235,6 +235,7 @@ export const InitAccommodationRegistration = () => {

useEffect(() => {
window.scrollTo(0, 0);

if (
accommodationData.isAccommodationEdit ||
isClickedPrevButton ||
Expand All @@ -254,6 +255,13 @@ export const InitAccommodationRegistration = () => {
type: userInputValue[0].type as defaultAccommodation['type'],
});
}
if (
!accommodationData.isAccommodationEdit &&
updatedAccommodationInfo &&
userInputValue[0].rooms.length !== 0
) {
navigate(ROUTES.INIT_INFO_CONFIRMATION);
}
}, []);

return (
Expand Down
8 changes: 7 additions & 1 deletion src/pages/init-info-confirmation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { AccommodationInfo } from '@components/init/init-info-confirmation/Accom
import { RoomInfo } from '@components/init/init-info-confirmation/RoomInfo';
import { getCookie } from '@hooks/sign-in/useSignIn';
import {
addRoomState,
isUpdatedAccommodationState,
isUpdatedRoomState,
userInputValueState,
} from '@stores/init/atoms';
import { useEffect } from 'react';
Expand All @@ -18,10 +20,14 @@ export const InitInfoConfirmation = () => {
const setUpdatedAccommodationInfo = useSetRecoilState(
isUpdatedAccommodationState,
);
const setUpdatedRoomInfo = useSetRecoilState(isUpdatedRoomState);
const setIsAddRoomState = useSetRecoilState(addRoomState);

useEffect(() => {
window.scrollTo(0, 0);
setUpdatedAccommodationInfo(true);
setIsAddRoomState(false);
if (userInputValue[0].name !== '') setUpdatedAccommodationInfo(true);
if (userInputValue[0].rooms.length !== 0) setUpdatedRoomInfo(true);
}, []);

if (userInputValue[0].name === '') {
Expand Down
17 changes: 14 additions & 3 deletions src/pages/init-room-registration/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
checkedRoomOptions,
imageFileState,
isUpdatedAccommodationState,
isUpdatedRoomState,
userInputValueState,
} from '@stores/init/atoms';
import { capacityHasError, priceHasError } from '@stores/room/atoms';
Expand Down Expand Up @@ -67,6 +68,8 @@ export const InitRoomRegistration = () => {

const [isAddRoom, setIsAddRoom] = useRecoilState(addRoomState);

const [isUpdatedRoom, setIsUpdatedRoom] = useRecoilState(isUpdatedRoomState);

useEffect(() => {
window.scrollTo(0, 0);

Expand Down Expand Up @@ -101,6 +104,14 @@ export const InitRoomRegistration = () => {
images: userInputValue[0].rooms[index].images,
options: userInputValue[0].rooms[index].options,
});
} else if (
isUpdatedAccommodation &&
isUpdatedRoom &&
!isAddRoom &&
userInputValue[0].editRoomIndex === -1
) {
navigate(ROUTES.INIT_INFO_CONFIRMATION);
setIsUpdatedRoom(false);
}
}, []);

Expand Down Expand Up @@ -189,13 +200,13 @@ export const InitRoomRegistration = () => {
if (error instanceof AxiosError) {
message.error({
content: '요청에 실패했습니다. 잠시 후 다시 시도해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
if (error.response?.data.code === RESPONSE_CODE.IMAGE_SAVE_FAIL) {
message.error({
content: '요청을 실패했습니다. 관리자에게 문의해주세요',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
}
},
Expand All @@ -221,7 +232,7 @@ export const InitRoomRegistration = () => {
setSameRoomName(true);
message.error({
content: '동일한 객실명의 상품이 이미 존재합니다.',
style: { marginTop: '210px' },
style: { marginTop: '64px' },
});
window.scrollTo({ top: 0, behavior: 'smooth' });
return;
Expand Down
12 changes: 12 additions & 0 deletions src/stores/init/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ export const isUpdatedAccommodationState = atom({
});

/** 객실 정보를 입력했는지 여부 */
const { persistAtom: updateRoomPersist } = recoilPersist({
key: 'isUpdatedRoomState',
storage: localStorage,
});

export const isUpdatedRoomState = atom({
key: 'isUpdatedRoom',
default: false,
effects_UNSTABLE: [updateRoomPersist],
});

/** 객실 추가하기 버튼을 눌렀는지 여부 */
const { persistAtom: addRoomPersist } = recoilPersist({
key: 'addRoomState',
storage: localStorage,
Expand Down

0 comments on commit b787459

Please sign in to comment.