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

장소 추가 시 해당 Day 탭 유지 #289

Merged
merged 2 commits into from
Jan 28, 2024
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
12 changes: 8 additions & 4 deletions src/components/Plan/PlanSectionTop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {
} from '@api/socket';
import { useEffect } from 'react';
import { useRecoilState } from 'recoil';
import { dayState, dateState } from '@recoil/plan';
import { calculateDayAndDate } from '@utils/utils';
import { dayState, dateState, isFirstLoadState } from '@recoil/plan';
import { calculateDayAndDate, resetVisitDateAndTab } from '@utils/utils';
import { useGetTrips } from '@hooks/useGetTrips';
import { visitDateState } from '@recoil/socket';
import { useState } from 'react';
Expand All @@ -39,6 +39,8 @@ const PlanSectionTop = () => {
tripBudget,
} = useContext(socketContext);
const [, setVisitDate] = useRecoilState(visitDateState);
const [isFirstLoad, setIsFirstLoad] = useRecoilState(isFirstLoadState);

const { startDate, endDate } = useGetTrips();
const [isEnter, setIsEnter] = useState(false);

Expand All @@ -52,12 +54,13 @@ const PlanSectionTop = () => {
}

useEffect(() => {
if (startDate) {
if (isFirstLoad && startDate) {
setVisitDate({ visitDate: startDate });
setIsFirstLoad(false);
}
setDay(DayArr);
setDate(DateArr);
}, [startDate, endDate]);
}, [startDate, endDate, isFirstLoad]);

useEffect(() => {
callBackPub(() => pubEnterMember(tripId));
Expand Down Expand Up @@ -93,6 +96,7 @@ const PlanSectionTop = () => {
showBack={true}
backHandler={() => {
navigate(-1);
resetVisitDateAndTab();
}}
showShare={true}
shareHandler={() => {
Expand Down
3 changes: 3 additions & 0 deletions src/components/Trip/PlanTripButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PlanColorIcon, RightIcon } from '@components/common/icons/Icons';
import { useNavigate } from 'react-router-dom';
import { getMember } from '@api/member';
import { resetVisitDateAndTab } from '@utils/utils';

const PlanTripButton = () => {
const navigate = useNavigate();
Expand All @@ -16,6 +17,8 @@ const PlanTripButton = () => {
}
};

resetVisitDateAndTab();

return (
<button
onClick={handleButtonClick}
Expand Down
1 change: 0 additions & 1 deletion src/components/Trip/TripPreference.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ const TripPreference: React.FC = () => {

useEffect(() => {
if (tripPreference) {
console.log('tripPreference', tripPreference);
setA([
tripPreference?.data?.data?.planningCount,
tripPreference?.data?.data?.planningTotalCount,
Expand Down
22 changes: 20 additions & 2 deletions src/components/addToList/addToOurPlace/AddtoListBtn.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRecoilValue, useRecoilState } from 'recoil';
import { useRecoilState } from 'recoil';
import { selectedItemsState } from '@recoil/listItem';
import { ButtonPrimary } from '@components/common/button/Button';
import { postTripsLike } from '@api/trips';
Expand All @@ -7,6 +7,8 @@ import { pubAddTripItem } from '@api/socket';
import { useContext } from 'react';
import { socketContext } from '@hooks/useSocket';
import { visitDateState } from '@recoil/socket';
import { calculateDayAndDate } from '@utils/utils';
import { tapState } from '@recoil/plan';

const AddToListButton = ({
apiType,
Expand All @@ -16,13 +18,26 @@ const AddToListButton = ({
const { id: tripId } = useParams();
const [selectedTourItemIds, setSelectedTourItemIds] =
useRecoilState(selectedItemsState);
const visitDate = useRecoilValue(visitDateState);
const [visitDate, setVisitDate] = useRecoilState(visitDateState);
const { callBackPub } = useContext(socketContext);
const navigate = useNavigate();
const { tripInfo } = useContext(socketContext);
const startDate = tripInfo?.data?.startDate;
const endDate = tripInfo?.data?.endDate;
const [, setTapState] = useRecoilState(tapState);

const handleAddClick = async () => {
if (tripId) {
try {
// calculateDayAndDate 함수를 호출하여 DateArr 얻기
const { DateArr } = calculateDayAndDate(startDate ?? '', endDate ?? '');

// visitDate와 DateArr 비교하여 인덱스 찾기
const dayIndex = DateArr.findIndex(
(date) => date === visitDate?.visitDate,
);
const selectedDay = `${dayIndex}`;

if (apiType === 'postTripsLike') {
await postTripsLike(tripId, selectedTourItemIds);
navigate(`/trip/${tripId}`);
Expand All @@ -36,7 +51,10 @@ const AddToListButton = ({
newTripItems,
};

setVisitDate(visitDate);
setTapState(selectedDay);
callBackPub(() => pubAddTripItem(pubAddTripItemData, tripId));

setTimeout(() => {
setSelectedTourItemIds([]);
navigate(`/trip/${tripId}/plan`);
Expand Down
12 changes: 6 additions & 6 deletions src/components/common/icons/Icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ export const LeftIcon = () => {
viewBox="0 0 9 16"
fill="none">
<path
fill-rule="evenodd"
clip-rule="evenodd"
fillRule="evenodd"
clipRule="evenodd"
d="M8.24116 1.13262C8.55699 1.41694 8.58027 1.90096 8.29315 2.21371L2.58306 8.43359L8.29315 14.6535C8.58026 14.9662 8.55699 15.4502 8.24116 15.7346C7.92534 16.0189 7.43656 15.9958 7.14944 15.6831L0.49414 8.43359L7.14944 1.1841C7.43656 0.871356 7.92534 0.848308 8.24116 1.13262Z"
fill="#1E1E1E"
/>
Expand Down Expand Up @@ -695,14 +695,14 @@ export const SearchIcon: React.FC<IconProps> = ({
height={size}
fill={fill}>
<path
fill-rule="evenodd"
clip-rule="evenodd"
fillRule="evenodd"
clipRule="evenodd"
d="M8.00156 2.27344C4.88115 2.27344 2.35156 4.80303 2.35156 7.92344C2.35156 11.0438 4.88115 13.5734 8.00156 13.5734C11.122 13.5734 13.6516 11.0438 13.6516 7.92344C13.6516 4.80303 11.122 2.27344 8.00156 2.27344ZM0.851562 7.92344C0.851562 3.9746 4.05273 0.773438 8.00156 0.773438C11.9504 0.773438 15.1516 3.9746 15.1516 7.92344C15.1516 11.8723 11.9504 15.0734 8.00156 15.0734C4.05273 15.0734 0.851562 11.8723 0.851562 7.92344Z"
fill="#1E1E1E"
/>
<path
fill-rule="evenodd"
clip-rule="evenodd"
fillRule="evenodd"
clipRule="evenodd"
d="M11.872 12.1596C12.1649 11.8667 12.6398 11.8667 12.9327 12.1596L16.9327 16.1597C17.2256 16.4526 17.2256 16.9274 16.9327 17.2203C16.6398 17.5132 16.1649 17.5132 15.872 17.2203L11.872 13.2203C11.5791 12.9274 11.5791 12.4525 11.872 12.1596Z"
fill="#1E1E1E"
/>
Expand Down
18 changes: 13 additions & 5 deletions src/components/common/tab/Tab.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import * as Tabs from '@radix-ui/react-tabs';
import { useRecoilState } from 'recoil';
import { tapState } from '@recoil/plan';
import { useRecoilState, useRecoilValue } from 'recoil';
import { isFirstLoadState, tapState } from '@recoil/plan';
import { useEffect } from 'react';

interface TabProps {
lists: string[];
contents: React.ReactNode[];
}

const Tab = ({ lists, contents }: TabProps) => {
const [, setTapState] = useRecoilState(tapState);

const [tap, setTapState] = useRecoilState(tapState);
const isFirstLoad = useRecoilValue(isFirstLoadState);
const handleTabChange = (value: string) => {
const tabIndex = value.replace('tab', '');
setTapState(tabIndex);
};

useEffect(() => {
setTapState(tap);
if (isFirstLoad) {
setTapState('0');
}
}, [tap]);

let isDayTab = false;
let isParticipationTab = false;

Expand All @@ -30,7 +38,7 @@ const Tab = ({ lists, contents }: TabProps) => {
return (
<Tabs.Root
className="flex w-full flex-col"
defaultValue="tab0"
defaultValue={tap ? `tab${tap}` : 'tab0'}
onValueChange={handleTabChange}>
<Tabs.List
className={`${
Expand Down
5 changes: 5 additions & 0 deletions src/recoil/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ export const tapState = atom<string>({
key: 'tapState',
default: '',
});

export const isFirstLoadState = atom({
key: 'isFirstLoadState',
default: true,
});
18 changes: 18 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
import { useGetTrips } from '@hooks/useGetTrips';
import { isFirstLoadState, tapState } from '@recoil/plan';
import { visitDateState } from '@recoil/socket';
import { useSetRecoilState } from 'recoil';

export const getEmoji = (content: string) => {
const emojiMap: { [key: string]: string } = {
깨끗해요: '🧼',
Expand Down Expand Up @@ -56,3 +61,16 @@ export function calculateDayAndDate(startDate: string, endDate: string) {
SmallDayArr,
};
}

export const resetVisitDateAndTab = () => {
const setVisitDate = useSetRecoilState(visitDateState);
const setIsFirstLoad = useSetRecoilState(isFirstLoadState);
const setTapState = useSetRecoilState(tapState);
const { startDate } = useGetTrips();

if (startDate) {
setVisitDate({ visitDate: startDate });
}
setIsFirstLoad(true);
setTapState('0');
};
Loading