Skip to content

Commit

Permalink
feat: Add vote daily limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Najeong-Kim committed Aug 23, 2024
1 parent b3cf169 commit 19c3392
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/features/discovery-detail/lib/formatDate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const formatDate = (date: Date) => {
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${year}-${month}-${day}`;
}
21 changes: 21 additions & 0 deletions src/features/discovery-detail/lib/vote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { formatDate } from "./formatDate";

export const getVoteData = () => {
const voteData = localStorage.getItem('vote_data');
return voteData ? JSON.parse(voteData) : {};
}

export const saveVoteData = (voteData: Record<string, string>[]) => {
localStorage.setItem('vote_data', JSON.stringify(voteData));
}

export const canVote = (resortId: string) => {
const voteData = getVoteData();
const todayDate = formatDate(new Date());

if (voteData[resortId] === todayDate) {
return false;
}

return true;
}
11 changes: 10 additions & 1 deletion src/features/discovery-detail/ui/vote-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
DialogTitle,
DialogTrigger,
} from '@/shared/ui/dialog';
import { formatDate } from '../lib/formatDate';
import { getVoteText } from '../lib/getVoteText';
import { canVote, getVoteData, saveVoteData } from '../lib/vote';

interface VoteDialogProps {
id: number;
Expand All @@ -29,14 +31,21 @@ const VoteDialog = ({ id, trigger }: VoteDialogProps) => {
const { mutateAsync } = usePostVote(id.toString());

const handleVote = useCallback(async () => {
if (!canVote(id.toString())) {
toast.error('하루에 한 번만 투표할 수 있어요');
return;
}
try {
await mutateAsync({ isLike: isGood });
} catch (error) {
console.log(error);
} finally {
const voteData = getVoteData();
voteData[id.toString()] = formatDate(new Date());
saveVoteData(voteData);
toast.success('고마워요! 투표의 결과가 반영되었어요');
}
}, [isGood, mutateAsync]);
}, [id, isGood, mutateAsync]);

return (
<Dialog>
Expand Down
11 changes: 10 additions & 1 deletion src/views/discovery-detail/ui/discovery-detail-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { DiscoveryContentTabList } from '@/widgets/discovery-detail/model/consta
import DiscoverySummary from '@/widgets/discovery-detail/ui/discovery-summary';
import { Header } from '@/widgets/header/ui';
import { WebcamMap, WebcamSlopList } from '@/widgets/webcam/ui';
import { formatDate } from '@/features/discovery-detail/lib/formatDate';
import { getVoteText } from '@/features/discovery-detail/lib/getVoteText';
import { canVote, getVoteData, saveVoteData } from '@/features/discovery-detail/lib/vote';
import AppDownloadDialog from '@/features/discovery-detail/ui/app-download-dialog';
import useMapPinch from '@/features/slop/hooks/useMapPinch';
import calculateWebcamPosition from '@/features/slop/lib/calculateWebcamPosition';
Expand Down Expand Up @@ -55,14 +57,21 @@ const DiscoveryDetailPage = ({ params }: { params: { resortId: string } }) => {
};

const handleVote = useCallback(async () => {
if (!canVote(params?.resortId)) {
toast.error('하루에 한 번만 투표할 수 있어요');
return;
}
try {
await mutateAsync({ isLike: isGood });
} catch (error) {
console.log(error);
} finally {
const voteData = getVoteData();
voteData[params?.resortId] = formatDate(new Date());
saveVoteData(voteData);
toast.success('고마워요! 투표의 결과가 반영되었어요');
}
}, [isGood, mutateAsync]);
}, [isGood, mutateAsync, params?.resortId]);

if (!discovery) return;

Expand Down

0 comments on commit 19c3392

Please sign in to comment.