Skip to content

Commit

Permalink
Refactor: useGetCalendarData
Browse files Browse the repository at this point in the history
  • Loading branch information
cuconveniencestore authored and hookor committed May 24, 2024
1 parent 7973eeb commit dc1ef30
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/components/coffee/MyCalendar.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import dayjs from 'dayjs';
import { useState } from 'react';
import { useRecoilState } from 'recoil';
import ReactCalendar, { OnArgs, TileArgs } from 'react-calendar';

import { COFFEE_CALENDAR_TEXTS } from '@/constants/coffee';
import { validateDay } from '@/utils/validateDay';
import { useGetCalendarData } from 'hooks/coffee/useGetCalendarData';
import { CalendarData } from '@/types/types';
import { activeMonthState } from '@/atoms/atoms';

import { Blur, InputByteCheck, SumTitle } from '@/styles/styles';
import { Align, Flex } from '@/styles/layout';
import { css, cx } from 'styled-system/css';
import { styled } from 'styled-system/jsx';
import { useRecoilState } from 'recoil';
import { activeMonthState } from '@/atoms/atoms';

const { title, legend } = COFFEE_CALENDAR_TEXTS;

Expand Down Expand Up @@ -135,6 +135,7 @@ const MyCoffeeCalendar = css`
}
& .react-calendar__month-view__days__day {
line-height: 23px;
color: var(--colors-main-dark);
}
Expand All @@ -161,6 +162,8 @@ const MyCoffeeCalendar = css`
& abbr {
font-weight: 600;
width: 30px;
text-align: center;
line-height: 23px;
border-radius: 50%;
color: #fff;
background-color: var(--colors-main);
Expand Down
22 changes: 12 additions & 10 deletions src/hooks/coffee/useGetCalendarData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';

import { getCoffeeCaledar } from '@/api/coffee';
import { CalendarData } from '@/types/types';

export const useGetCalendarData = (signedIn: string, activeMonth: string) => {
const [coffeeData, setCoffeeData] = useState<(CalendarData | null)[]>();
const getCalendarData = async (date: string) => {
const res = await getCoffeeCaledar(date);
res && setCoffeeData(res.days);
};
const { data } = useQuery({
queryKey: ['coffeeCalendar', activeMonth],
queryFn: async () => {
const data = await getCoffeeCaledar(activeMonth);
return data.days;
},
enabled: !!signedIn
});

const coffeeData = data as CalendarData[];

const healthy = coffeeData?.filter(
item => item && Number(item.caffeineSum) <= 200
Expand All @@ -22,9 +28,5 @@ export const useGetCalendarData = (signedIn: string, activeMonth: string) => {
item => item && Number(item.caffeineSum) > 401
);

useEffect(() => {
signedIn && getCalendarData(activeMonth);
}, [activeMonth, signedIn]);

return { healthy, recommended, excessive };
};
8 changes: 5 additions & 3 deletions src/hooks/coffee/useGetMyCoffeeSum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import { getCoffeeIntake } from '@/api/coffee';
import { activeMonthState } from '@/atoms/atoms';
import { COFFEE_TEXTS } from '@/constants/coffee';
import {
InvalidateQueryFilters,
useQuery,
Expand All @@ -9,7 +6,12 @@ import {
import { useEffect } from 'react';
import { useRecoilValue } from 'recoil';

import { getCoffeeIntake } from '@/api/coffee';
import { activeMonthState } from '@/atoms/atoms';
import { COFFEE_TEXTS } from '@/constants/coffee';

const { week } = COFFEE_TEXTS;

export const useGetMyCoffeeSum = (signedIn: string | null) => {
const queryClient = useQueryClient();
const activeMonth = useRecoilValue(activeMonthState);
Expand Down

0 comments on commit dc1ef30

Please sign in to comment.