Skip to content

Commit

Permalink
feat: 목표 생성, 목표 상세보기, 목표 삭제 후 현재 페이지 변경 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
deepbig committed Jan 12, 2024
1 parent 8d1618e commit 1e12ad2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 38 deletions.
24 changes: 13 additions & 11 deletions src/app/goal/detail/saved/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@ import { useSearchParams } from 'next/navigation';
import { ContentBody, SavedFooterButton, SavedHeader, Sticker } from '@/features/goal/components';
import DetailLayout from '@/features/goal/components/detail/DetailLayout';
import { useGetGoal } from '@/hooks/reactQuery/goal';
import { usePrefetchGoals } from '@/hooks/reactQuery/goal/useGetGoals';

// TODO: 추후 GoalDetailPage와 합칠 예정
const GoalSavedPage = () => {
const id = useSearchParams().get('id');
const { data: goal } = useGetGoal({ goalId: Number(id) });
const id = Number(useSearchParams().get('id'));
const { data: goal } = useGetGoal({ goalId: id });
usePrefetchGoals();

return (
<DetailLayout
header={<SavedHeader />}
sticker={goal && <Sticker stickerUrl={goal.stickerUrl} />}
body={
goal && (
goal && (
<DetailLayout
header={<SavedHeader id={id} />}
sticker={<Sticker stickerUrl={goal.stickerUrl} />}
body={
<ContentBody title={goal.title} date={goal.deadline} tag={goal.tagInfo.tagContent} more={goal.description} />
)
}
footer={<SavedFooterButton />}
/>
}
footer={<SavedFooterButton id={id} />}
/>
)
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const DeleteGoalButtomSheet = ({ open, onClose, goalId }: DeleteGoalButto
useEffect(() => {
if (isSuccess) {
onClose();
router.back();
router.push('/home');
}
}, [isSuccess, isError, onClose, router]);

Expand Down
2 changes: 1 addition & 1 deletion src/features/goal/components/detail/DetailHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const DetailHeader = ({ goalId }: DetailHeaderProps) => {

return (
<>
<Link href={{ pathname: '/home' }}>
<Link href={`/home?id=${goalId}`}>
<CloseIcon />
</Link>

Expand Down
8 changes: 6 additions & 2 deletions src/features/goal/components/detail/SavedFooterButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import Link from 'next/link';

import { Button } from '@/components';

export const SavedFooterButton = () => {
interface SavedFooterButtonProps {
id: number;
}

export const SavedFooterButton = ({ id }: SavedFooterButtonProps) => {
return (
<Link href={{ pathname: '/home' }}>
<Link href={`/home?id=${id}`}>
<Button>홈으로 가기</Button>
</Link>
);
Expand Down
8 changes: 6 additions & 2 deletions src/features/goal/components/detail/SavedHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import BackIcon from '@/assets/icons/goal/back-icon.svg';
import CloseIcon from '@/assets/icons/goal/close-icon.svg';
import { Typography } from '@/components/atoms';

export const SavedHeader = () => {
interface SavedHeaderProps {
id: number;
}

export const SavedHeader = ({ id }: SavedHeaderProps) => {
return (
<>
<Link href={{ pathname: '/home' }}>
<Link href={`/home?id=${id}`}>
<BackIcon />
</Link>
<Typography type="header1">목표 저장 완료!</Typography>
Expand Down
20 changes: 15 additions & 5 deletions src/features/home/components/lifeMap/LifeMap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { useEffect, useRef, useState } from 'react';
import Link from 'next/link';
import { useSearchParams } from 'next/navigation';
import { useOverlay } from '@toss/use-overlay';
import { SwiperSlide } from 'swiper/react';

Expand Down Expand Up @@ -33,12 +34,12 @@ export const LifeMap = () => {

const [position, setPosition] = useState<number | null>(null);
const [currentPage, setCurrentPage] = useState<number | null>(null);
const paramId = Number(useSearchParams().get('id'));

useEffect(() => {
if (goalsData?.goals) {
findCurrentPosition(goalsData?.goals);
findCurrentPosition(goalsData.goals);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [goalsData]);

const findCurrentPosition = (goals: Array<GoalProps>) => {
Expand All @@ -59,7 +60,16 @@ export const LifeMap = () => {
position = currentPosition + 1;
}
setPosition(position);
setCurrentPage(Math.floor(position / GOAL_COUNT_PER_PAGE));

let page = Math.floor(position / GOAL_COUNT_PER_PAGE);
// check if query params contains id value
if (paramId) {
const index = goals.findIndex(({ id: goalId }) => goalId === paramId);
if (index > -1) {
page = Math.floor((index + 1) / GOAL_COUNT_PER_PAGE);
}
}
setCurrentPage(page);
};

const handleOpenShareBottomSheet = () => {
Expand Down Expand Up @@ -100,15 +110,15 @@ export const LifeMap = () => {
<StarBg />
<div className="h-[520px]">
<div className="absolute inset-x-0">
<MapSwiper currentPosition={position}>
<MapSwiper currentPage={currentPage}>
{participatedGoalsArray?.map((goals, page) => (
<SwiperSlide key={`swiper-goal-${page}`}>
{!(page % 2) ? (
<MapCardPositioner type="A" goals={goals} isFirst={page === 0} isLast={page === LAST_PAGE - 1} />
) : (
<MapCardPositioner type="B" goals={goals} isLast={page === LAST_PAGE - 1} />
)}
{position && currentPage === page && (
{position && Math.floor(position / GOAL_COUNT_PER_PAGE) === page && (
<CurrentPositionCover currentPosition={position % TOTAL_CURRENT_POSITIONS} />
)}
</SwiperSlide>
Expand Down
20 changes: 5 additions & 15 deletions src/features/home/components/mapSwiper/MapSwiper.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { type PropsWithChildren, useEffect, useState } from 'react';
import { type PropsWithChildren } from 'react';
import { Pagination } from 'swiper/modules';
import { Swiper } from 'swiper/react';

import { GOAL_COUNT_PER_PAGE } from '@/features/home/constants';

import { CustomPagination } from './CustomPagination';

import 'swiper/css';
Expand All @@ -16,21 +14,13 @@ const settings = {
};

interface MapSwiperProps extends PropsWithChildren {
currentPosition: number | null;
currentPage: number | null;
}

export const MapSwiper = ({ currentPosition, children }: MapSwiperProps) => {
const [initialSlide, setInitialSlide] = useState<number | null>(null);

useEffect(() => {
if (currentPosition) {
setInitialSlide(Math.floor(currentPosition / GOAL_COUNT_PER_PAGE));
}
}, [currentPosition]);

export const MapSwiper = ({ currentPage, children }: MapSwiperProps) => {
return (
initialSlide !== null && (
<Swiper {...settings} initialSlide={initialSlide} className="h-full">
currentPage !== null && (
<Swiper {...settings} initialSlide={currentPage} className="h-full">
{children}
<CustomPagination />
</Swiper>
Expand Down
10 changes: 9 additions & 1 deletion src/hooks/reactQuery/goal/useGetGoals.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQuery } from '@tanstack/react-query';
import { useQuery, useQueryClient } from '@tanstack/react-query';

import { api } from '@/apis';

Expand All @@ -21,3 +21,11 @@ export const useGetGoals = () => {
queryFn: () => api.get<GoalResponse>('/life-map'),
});
};

export const usePrefetchGoals = (): void => {
const queryClient = useQueryClient();
queryClient.prefetchQuery({
queryKey: ['goals'],
queryFn: () => api.get<GoalResponse>('/life-map'),
});
};

0 comments on commit 1e12ad2

Please sign in to comment.