From 6a6716b93fdd4737a0d72d31dc538215575c6c4e Mon Sep 17 00:00:00 2001 From: lybell-art Date: Mon, 19 Aug 2024 18:48:09 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20=EC=B6=94=EC=B2=A8=20=EC=9D=B4?= =?UTF-8?q?=EB=B2=A4=ED=8A=B8=20zustand=20=EC=8A=A4=ED=86=A0=EC=96=B4=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/mainPage/shared/drawEvent/store.js | 58 ++++++++++++++++++++++ src/mainPage/shared/realtimeEvent/store.js | 4 +- 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 src/mainPage/shared/drawEvent/store.js diff --git a/src/mainPage/shared/drawEvent/store.js b/src/mainPage/shared/drawEvent/store.js new file mode 100644 index 00000000..3886b1f2 --- /dev/null +++ b/src/mainPage/shared/drawEvent/store.js @@ -0,0 +1,58 @@ +import { create } from "zustand"; +import { fetchServer, HTTPError, ServerCloseError } from "@common/dataFetch/fetchServer.js"; +import { getQuery, getQuerySuspense } from "@common/dataFetch/getQuery.js"; +import { getServerPresiseTime } from "@common/utils.js"; +import { EVENT_DRAW_ID, EVENT_START_DATE } from "@common/constants.js"; + +function getJoinDataEvent() +{ + return fetchServer(`/api/v1/event/draw/${EVENT_DRAW_ID}/participation`) + .then(({ dates }) => { + let newJoinedList = [false, false, false, false, false]; + dates.forEach((date) => { + const day = getDayDifference(EVENT_START_DATE, new Date(date)); + newJoinedList[day] = true; + }); + return newJoinedList; + }) + .catch(() => [false, false, false, false, false]); +} + +const drawEventStore = create( (set, get)=>({ + joinStatus : [false, false, false, false, false], + openBaseDate : new Date("9999-12-31"), + currentJoined: false, + getJoinData: ()=>{ + async function promiseFn() { + try { + const [serverTime, joinStatus] = Promise.all([ + getQuery("server-time", getServerPresiseTime), + getJoinDataEvent() + ]); + const currentDay = getDayDifference(EVENT_START_DATE, serverTime); + let currentJoined = false; + if(currentDay >= 0 && currentDay < joinStatus.length) { + currentJoined = joinStatus[currentDay]; + } + set({joinStatus, openBaseDate: serverTime, currentJoined }); + return joinStatus; + } + catch { + set({joinStatus: [false, false, false, false, false], openBaseDate: new Date("9999-12-31")}); + return [false, false, false, false, false]; + } + } + return getQuerySuspense("draw-info-data", promiseFn, [set]); + }, + setCurrentJoin: (value)=>{ + set({currentJoined: value}); + }, + getJoinStatus: (index)=>{ + return get().joinStatus[index]; + }, + getOpenStatus: (index)=>{ + return get().openBaseDate >= EVENT_START_DATE.getTime() + index * DAY_MILLISEC; + } +}) ); + +export default drawEventStore; \ No newline at end of file diff --git a/src/mainPage/shared/realtimeEvent/store.js b/src/mainPage/shared/realtimeEvent/store.js index 3373178c..f78dd4dd 100644 --- a/src/mainPage/shared/realtimeEvent/store.js +++ b/src/mainPage/shared/realtimeEvent/store.js @@ -1,7 +1,7 @@ import { create } from "zustand"; import * as Status from "./constants.js"; import { fetchServer, HTTPError, ServerCloseError } from "@common/dataFetch/fetchServer.js"; -import { getQuerySuspense } from "@common/dataFetch/getQuery.js"; +import { getQuery, getQuerySuspense } from "@common/dataFetch/getQuery.js"; import { getServerPresiseTime } from "@common/utils.js"; import { EVENT_FCFS_ID } from "@common/constants.js"; @@ -55,7 +55,7 @@ const fcfsStore = create((set) => ({ const promiseFn = async function () { // get server time and event info const [serverTime, eventInfo] = await Promise.all([ - getServerPresiseTime(), + getQuery("server-time", getServerPresiseTime), getFcfsEventInfo(), ]); const currentServerTime = serverTime;