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

[혜수] - 네비게이션바에 계획 생성 가능 여부 전역상태관리 추가 #335

Merged
merged 1 commit into from
Dec 28, 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
11 changes: 4 additions & 7 deletions src/app/(header)/home/_components/MyPlan.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use client';

import { Dropdown } from '@/components';
import { maxPlan } from '@/constants/plan';
import { planIcons } from '@/constants/planIcons';
import { useScroll } from '@/hooks/useScroll';
import { canMakeNewPlanStore } from '@/stores/canMakeNewPlanStore';
import { GetMyPlansResponse } from '@/types/apis/plan/GetMyPlans';
import { checkThisYear } from '@/utils/checkThisYear';
import classNames from 'classnames';
import Image from 'next/image';
import { useEffect, useState } from 'react';
import { useSetRecoilState } from 'recoil';
import NewPlan from './NewPlan/NewPlan';
import Plan from './Plan/Plan';
import ProgressBar from './ProgressBar/ProgressBar';
Expand All @@ -19,7 +18,6 @@ type MyPlanProps = {
};

export default function MyPlan({ myPlans }: MyPlanProps) {
const maxLength = 4;
const { handleScroll, scrollableRef } = useScroll();
const { data: myPlansData } = myPlans;
const yearList = myPlansData.map((x) => x.year);
Expand All @@ -28,7 +26,7 @@ export default function MyPlan({ myPlans }: MyPlanProps) {
const [yearDataLength, setYearDataLength] = useState(
myPlansData[0].getPlanList.length,
);
const setCanMakeNewPlan = useSetRecoilState(canMakeNewPlanStore);

const PERIOD_OPTIONS = yearList.map((x) => {
return { value: x, name: `${x}년 계획` };
});
Expand All @@ -37,8 +35,7 @@ export default function MyPlan({ myPlans }: MyPlanProps) {
const chosenYearData = myPlansData.find((x) => x.year === year)!;
setYearData(chosenYearData);
setYearDataLength(chosenYearData.getPlanList.length);
setCanMakeNewPlan(!!(maxLength - chosenYearData.getPlanList.length));
}, [year, myPlansData, setCanMakeNewPlan, setYearDataLength]);
}, [year, myPlansData, setYearDataLength]);
return (
<>
<div className={classNames('home__header')}>
Expand Down Expand Up @@ -95,7 +92,7 @@ export default function MyPlan({ myPlans }: MyPlanProps) {
)}
<NewPlan />
<p className={classNames('home__number', 'color-origin-text-300')}>
({yearDataLength}/{maxLength})
({yearDataLength}/{maxPlan})
</p>
</div>
</>
Expand Down
19 changes: 16 additions & 3 deletions src/app/_components/Navigation/Navigation.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use client';

import { getMyPlans } from '@/apis/client/getMyPlans';
import { Icon } from '@/components';
import { ajajaToast } from '@/components/Toaster/customToast';
import { maxPlan } from '@/constants/plan';
import { canMakeNewPlanStore } from '@/stores/canMakeNewPlanStore';
import { checkIsSeason } from '@/utils/checkIsSeason';
import { checkThisYear } from '@/utils/checkThisYear';
import classNames from 'classnames';
import { hasCookie } from 'cookies-next';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useState } from 'react';
import { useRecoilState } from 'recoil';
import { useEffect, useState } from 'react';
import { useRecoilState, useSetRecoilState } from 'recoil';
import './index.scss';

export default function Navigation({ hasAuth }: { hasAuth: boolean }) {
const pathName = usePathname();
const [isLogin, setIsLogin] = useState(hasAuth);
const [canMakeNewPlan] = useRecoilState(canMakeNewPlanStore);

const setCanMakeNewPlan = useSetRecoilState(canMakeNewPlanStore);
if (!hasCookie('auth')) {
setTimeout(() => {
setIsLogin(hasCookie('auth'));
Expand All @@ -29,6 +32,16 @@ export default function Navigation({ hasAuth }: { hasAuth: boolean }) {
}
};

useEffect(() => {
async function isMaxPlan() {
const data = await getMyPlans();
if (data.data[0]?.year === checkThisYear()) {
setCanMakeNewPlan(!!(maxPlan - data.data[0].getPlanList.length));
}
}
isMaxPlan();
}, [setCanMakeNewPlan]);

return (
<div className={classNames('navigation')}>
<Link
Expand Down
1 change: 1 addition & 0 deletions src/constants/plan.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const maxPlan = 4;