Skip to content

Commit

Permalink
[feat] 추첨 이벤트 zustand 스토어 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
lybell-art committed Aug 19, 2024
1 parent fef556e commit 6a6716b
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
58 changes: 58 additions & 0 deletions src/mainPage/shared/drawEvent/store.js
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions src/mainPage/shared/realtimeEvent/store.js
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 6a6716b

Please sign in to comment.