Skip to content

Commit

Permalink
feat: selectedDate에 따라 store의 BlockList 데이터 갱신(#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoonncho committed Mar 2, 2023
1 parent 0aef3f9 commit ad40f97
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/containers/Home/BlockList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import NoData from '@/components/NoData'
import useSelectedDateState from '@/store/selectedDate'
import { dayBlockAPI } from '@/api'
import useHttpRequest from '@/hooks/useHttpRequest'
import useBlockListStore from '@/store/blocks'
import { BASE_URL } from '@/constants/urls'
import DiaryButton from './DiaryButton'

Expand Down Expand Up @@ -34,6 +35,8 @@ const AddButton = ({

const BlockList = () => {
const selectedDate = useSelectedDateState((state) => state.date)
const storedBlocks = useBlockListStore((state) => state.blockList) // store에 저장된 블럭
const setStoredBlocks = useBlockListStore((state) => state.setBlockList)

const [dayBlocks, fetchDayBlocks, isLoading] = useHttpRequest(() =>
dayBlockAPI.getDayBlocks({ date: selectedDate }).then(({ data }) => data),
Expand All @@ -54,9 +57,12 @@ const BlockList = () => {
}

useEffect(() => {
if (selectedDate) {
fetchDayBlocks()
}
if (!selectedDate) return
fetchDayBlocks(undefined, {
onSuccess: (data) => {
setStoredBlocks(data)
},
})
}, [selectedDate])

const onVisibility = () => {
Expand All @@ -74,7 +80,7 @@ const BlockList = () => {
})

if (!dayBlocks || isLoading) return null
const { totalBlock, totalTask, blocks = [] } = dayBlocks
const { totalBlock, totalTask, blocks = [] } = storedBlocks

return (
<>
Expand Down

0 comments on commit ad40f97

Please sign in to comment.