Skip to content

Commit fad0563

Browse files
committed
fix: 투표방지, user team 받아오기
1 parent 95060a3 commit fad0563

File tree

4 files changed

+76
-72
lines changed

4 files changed

+76
-72
lines changed

react-vote/components/Candidate.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Candidate = (candidate: any) => {
99
});
1010
} catch (error) {
1111
console.log(error);
12+
alert('이미 투표하셨습니다!');
1213
}
1314
};
1415

react-vote/components/Team.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Team = (team: any) => {
99
});
1010
} catch (error) {
1111
console.log(error);
12+
alert('이미 투표하셨습니다!');
1213
}
1314
};
1415

react-vote/pages/vote/index.tsx

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ import Link from 'next/link';
33
import axios from 'axios';
44
import Router from 'next/router';
55

6-
import Candidate from '../../components/Candidate';
7-
86
import api from '../../api';
7+
import { useSelector } from 'react-redux';
98
import { getCookie, removeCookie } from '../../util/cookie';
10-
import { Navigate } from 'react-router-dom';
119

1210
const Vote = () => {
1311
interface CandidateProps {
@@ -19,7 +17,7 @@ const Vote = () => {
1917
const [cand, setCand] = useState<CandidateProps[] | null>(null);
2018
const [teams, setTeams] = useState([]);
2119

22-
const fetchCandidates = async () => {
20+
const fetchCandidates = async () => {
2321
try {
2422
const response = await api.get('/candidates/');
2523
setCand(response.data);
@@ -28,7 +26,7 @@ const fetchCandidates = async () => {
2826
}
2927
};
3028

31-
const fetchTeams = async () => {
29+
const fetchTeams = async () => {
3230
try {
3331
const response = await api.get('/teams/');
3432
setTeams(response.data);
@@ -37,25 +35,28 @@ const fetchTeams = async () => {
3735
}
3836
};
3937

40-
useEffect(()=>{
38+
useEffect(() => {
4139
if (getCookie('is_login') === undefined) {
4240
alert('로그인 후 접속');
4341
Router.push('/');
4442
} else {
4543
fetchCandidates();
4644
fetchTeams();
4745
}
48-
},[])
49-
46+
}, []);
5047

5148
const LogOut = () => {
5249
removeCookie('is_login');
5350
Router.push('/');
5451
};
5552

56-
console.log(cand);
53+
const [team, setTeam] = useState('FE');
54+
55+
const data = useSelector((state: any) => state.joinSlice.joinData);
5756

58-
const team = 'FE';
57+
useEffect(() => {
58+
setTeam(data.team);
59+
}, []);
5960

6061
return (
6162
<div className="container">
@@ -70,7 +71,9 @@ const fetchTeams = async () => {
7071
}}
7172
as={`/vote/${team}`}
7273
>
73-
<div className="vote-btn">FE 투표하기</div>
74+
<button className="vote-btn" disabled={team !== 'FE'}>
75+
FE 투표하기
76+
</button>
7477
</Link>
7578
<Link
7679
href={{
@@ -83,7 +86,7 @@ const fetchTeams = async () => {
8386
}}
8487
as={`/result/FE`}
8588
>
86-
<div className="vote-btn">FE 결과보기</div>
89+
<button className="vote-btn">FE 결과보기</button>
8790
</Link>
8891

8992
<Link
@@ -97,7 +100,9 @@ const fetchTeams = async () => {
97100
}}
98101
as={`/vote/BE`}
99102
>
100-
<div className="vote-btn">BE 투표하기</div>
103+
<button className="vote-btn" disabled={team !== 'BE'}>
104+
BE 투표하기
105+
</button>
101106
</Link>
102107
<Link
103108
href={{
@@ -110,7 +115,7 @@ const fetchTeams = async () => {
110115
}}
111116
as={`/result/BE`}
112117
>
113-
<div className="vote-btn">BE 결과보기</div>
118+
<button className="vote-btn">BE 결과보기</button>
114119
</Link>
115120
<Link
116121
href={{
@@ -121,11 +126,11 @@ const fetchTeams = async () => {
121126
}}
122127
as={`/vote/final`}
123128
>
124-
<div className="vote-btn">
129+
<button className="vote-btn">
125130
데모데이
126131
<br />
127132
투표하기
128-
</div>
133+
</button>
129134
</Link>
130135
<Link
131136
href={{
@@ -136,14 +141,14 @@ const fetchTeams = async () => {
136141
}}
137142
as={`/result/final`}
138143
>
139-
<div className="vote-btn">
144+
<button className="vote-btn">
140145
데모데이 <br /> 결과보기
141-
</div>
146+
</button>
142147
</Link>
143148
<button onClick={LogOut}>logOut</button>
144149
<style jsx>{`
145150
.vote-btn {
146-
width: 10em;
151+
width: 12em;
147152
height: 10rem;
148153
padding: 2rem;
149154
border: 1px solid white;

react-vote/reducer/joinUser.tsx

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,55 @@
1-
import { createAsyncThunk,createSlice } from "@reduxjs/toolkit";
2-
import axios from "axios";
3-
import Router from "next/router";
4-
5-
const initialState={
6-
joinData:[],
7-
id:[],
8-
error:"",
9-
loading:false
10-
11-
}
12-
13-
14-
export const signUpDb = createAsyncThunk('post/signup',
15-
async({joinData}:any)=>{
16-
try{
17-
const response = await axios.post(
1+
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2+
import axios from 'axios';
3+
import Router from 'next/router';
4+
5+
const initialState = {
6+
joinData: [],
7+
id: [],
8+
error: '',
9+
loading: false,
10+
};
11+
12+
export const signUpDb = createAsyncThunk(
13+
'post/signup',
14+
async ({ joinData }: any) => {
15+
try {
16+
const response = await axios.post(
1817
`https://www.ceosvote.link/accounts/signup/`,
1918
joinData
20-
);
21-
alert("가입 성공");
22-
Router.push("/");
23-
return response.data;
24-
}
25-
catch(error:any){
26-
console.log(error.code);
27-
alert("다시 입력해주세요");
28-
return error.code;
19+
);
20+
alert('가입 성공');
21+
Router.push('/');
22+
return response.data;
23+
} catch (error: any) {
24+
console.log(error.code);
25+
alert('다시 입력해주세요');
26+
return error.code;
2927
}
30-
}
31-
)
32-
28+
}
29+
);
3330

3431
const joinSlice = createSlice({
35-
name:"joinData",
36-
initialState,
37-
reducers:{},
38-
extraReducers:(builder)=>{
39-
//회원정보 전송
40-
builder.addCase(signUpDb.pending,(state)=>{
41-
state.loading = true;
42-
});
43-
builder.addCase(signUpDb.fulfilled, (state, action) => {
44-
state.loading = false;
45-
state.joinData = action.payload;
46-
state.error = "";
47-
});
48-
builder.addCase(signUpDb.rejected, (state, action) => {
49-
state.loading = false;
50-
state.joinData = [];
51-
//state.error = action.error.message;
52-
});
53-
}
54-
55-
})
56-
57-
export{joinSlice};
58-
export const joinReducer = joinSlice.reducer;
32+
name: 'joinData',
33+
initialState,
34+
reducers: {},
35+
extraReducers: (builder) => {
36+
//회원정보 전송
37+
builder.addCase(signUpDb.pending, (state) => {
38+
state.loading = true;
39+
});
40+
builder.addCase(signUpDb.fulfilled, (state, action) => {
41+
state.loading = false;
42+
state.joinData = action.payload;
43+
state.error = '';
44+
});
45+
builder.addCase(signUpDb.rejected, (state, action) => {
46+
state.loading = false;
47+
state.joinData = [];
48+
//state.error = action.error.message;
49+
});
50+
},
51+
});
52+
53+
export { joinSlice };
54+
55+
export const joinReducer = joinSlice.reducer;

0 commit comments

Comments
 (0)