diff --git a/src/api/requests.tsx b/src/api/requests.tsx index 0f78e1e..d000fcb 100644 --- a/src/api/requests.tsx +++ b/src/api/requests.tsx @@ -83,24 +83,36 @@ export const loginUser = async (id: string, password: string) => { } }; -export const logoutUser = async (refresh: any) => { +export const logoutUser = async (access: any) => { const response = await fetch(process.env.API_URL + '/api/accounts/logout/', { method: 'POST', headers: { 'Content-Type': 'application/json', - Authorization: `Bearer ${refresh}`, + Authorization: `Bearer ${access}`, }, }); - console.log(response); if (response.ok) { - const responseData = await response.json(); - return { success: true, data: responseData }; + return { success: true }; } else { return { success: false }; } }; -export const refreshAccessToken = async (refresh: any) => { +export const getIsVoted = async (access : any, userid : any) =>{ + const response = await fetch(process.env.API_URL + `/api/polls/vote-history/${userid}/`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${access}`, + }, + }); + if (response.ok) { + const data = await response.json(); + return data; + } +}; + +export const refreshAccessToken = async (refresh : any) =>{ const data = { refresh: refresh, }; diff --git a/src/app/login/page.tsx b/src/app/login/page.tsx index 0b10a43..19d87f7 100644 --- a/src/app/login/page.tsx +++ b/src/app/login/page.tsx @@ -39,7 +39,8 @@ export default function page() { id : response.data.user.id, part : response.data.user.part, team : response.data.user.team, - userName : response.data.user.username + userName : response.data.user.username, + userId : response.data.user.userid }); alert("로그인에 성공하였습니다."); diff --git a/src/app/vote/demo/page.tsx b/src/app/vote/demo/page.tsx index 5e31b8b..028f062 100644 --- a/src/app/vote/demo/page.tsx +++ b/src/app/vote/demo/page.tsx @@ -1,12 +1,30 @@ 'use client'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import Line from '@/components/common/Line'; import Header from '@/components/common/Header'; import SelectMenuResult from '@/components/vote/SelectMenuResult'; import Order from '@/components/common/Order'; +import { userInfoState } from '@/atom/states'; +import { useRecoilState } from 'recoil'; +import getAccessToken from '@/util/getAccessToken'; +import { useCookies } from 'react-cookie'; +import { getIsVoted } from '@/api/requests'; function page() { + const [cookies,setCookie,removeCookie] = useCookies(); + const [userInfo, setUserInfo] = useRecoilState(userInfoState); + const [isVoted , setIsVoted] = useState(false); + + useEffect(() => { + const checkIsVoted = async () => { + let accessToken = await getAccessToken(cookies,setCookie); + const response = await getIsVoted(accessToken,userInfo.userId); + setIsVoted(response.has_voted_demo); + }; + + checkIsVoted(); + }, []); return ( @@ -16,6 +34,8 @@ function page() { content="데모데이 투표" href="/vote/demo/day" resultHref="/vote/demo/day/result" + isVoted = {isVoted} + isShow = {true} /> ); diff --git a/src/app/vote/part/page.tsx b/src/app/vote/part/page.tsx index 865a910..31c35b8 100644 --- a/src/app/vote/part/page.tsx +++ b/src/app/vote/part/page.tsx @@ -1,12 +1,30 @@ 'use client'; -import React from 'react'; +import React, { useEffect, useState } from 'react'; import styled from 'styled-components'; import Line from '@/components/common/Line'; import Header from '@/components/common/Header'; import SelectMenuResult from '@/components/vote/SelectMenuResult'; import Order from '@/components/common/Order'; +import { userInfoState } from '@/atom/states'; +import { useRecoilState } from 'recoil'; +import getAccessToken from '@/util/getAccessToken'; +import { useCookies } from 'react-cookie'; +import { getIsVoted } from '@/api/requests'; function page() { + const [cookies,setCookie,removeCookie] = useCookies(); + const [userInfo, setUserInfo] = useRecoilState(userInfoState); + const [isVoted , setIsVoted] = useState(false); + useEffect(() => { + const checkIsVoted = async () => { + let accessToken = await getAccessToken(cookies,setCookie); + const response = await getIsVoted(accessToken,userInfo.userId); + setIsVoted(response.has_voted_part); + console.log(userInfo.part); + }; + + checkIsVoted(); + }, []); return ( @@ -17,11 +35,15 @@ function page() { content="FRONT-END br 파트장 투표" href="/vote/part/fe" resultHref="/vote/part/fe/result" + isVoted = {isVoted} + isShow = {userInfo.part === 2} /> diff --git a/src/atom/states.tsx b/src/atom/states.tsx index 047618a..25d10cb 100644 --- a/src/atom/states.tsx +++ b/src/atom/states.tsx @@ -10,6 +10,7 @@ const userInfoState = atom({ part : 0, team : 0, userName : '', + userId : '', }, effects_UNSTABLE: [persistAtom], }) diff --git a/src/components/common/Modal.tsx b/src/components/common/Modal.tsx index 402ef65..e097da2 100644 --- a/src/components/common/Modal.tsx +++ b/src/components/common/Modal.tsx @@ -4,16 +4,19 @@ import Link from 'next/link'; import { useRouter } from 'next/navigation'; import {useCookies} from 'react-cookie' import { logoutUser } from '@/api/requests'; +import getAccessToken from '@/util/getAccessToken'; function Modal({ clickModal }: any) { const router = useRouter(); const [cookies, setCookie, removeCookie] = useCookies(); const logoutHandler = async () => { - const response = await logoutUser(cookies.refresh); - console.log(response); - // removeCookie("refresh"); - // removeCookie("access"); - // router.push("/main"); + let accessToken = await getAccessToken(cookies,setCookie); + const response = await logoutUser(accessToken); + if(response.success){ + removeCookie("refresh"); + removeCookie("access"); + router.push("/main"); + } }; return ( diff --git a/src/components/vote/SelectMenuResult.tsx b/src/components/vote/SelectMenuResult.tsx index f561ab1..dd1912b 100644 --- a/src/components/vote/SelectMenuResult.tsx +++ b/src/components/vote/SelectMenuResult.tsx @@ -8,12 +8,17 @@ function SelectMenuResult({ content, href, resultHref, + isVoted, + isShow }: { content: string; href: string; resultHref: string; + isVoted : boolean; + isShow : boolean; }) { const lines = content.split('br'); + console.log(isShow); return ( <> @@ -26,9 +31,15 @@ function SelectMenuResult({ ))} - - + {isShow && !isVoted && } + {isShow && isVoted && } + + {!isShow && + + 본인의 파트만 투표 가능 + + } ); @@ -44,12 +55,12 @@ const VoteForm = styled.div` align-items: center; justify-content: space-between; margin-top: 50px; + position : relative; `; const VoteName = styled.div` font-size: 19px; font-weight: bold; margin-left: 40px; - margin-bottom: 40px; `; const CheckWrapper = styled.div` display: flex; @@ -57,3 +68,22 @@ const CheckWrapper = styled.div` justify-content: center; align-items: center; `; + +const DisableBox = styled.div` + position : absolute; + width: 300px; + height: 150px; + z-index : 10; + top : 0; + background-color: #ffe27c; + opacity : 0.8; + display: flex; + align-items: center; + justify-content: center; +` + +const WarnWrapper = styled.div` + text-align: center; + font-weight: bold; + font-size : 20px; +` \ No newline at end of file