송용우

Update SYNCBJ

......@@ -13,19 +13,20 @@ const BJIDFormBlock = styled.div`
}
`;
const BJIDForm = ({ onChange, onBJIDSubmit, profile }) => {
const BJIDForm = ({ onChange, onBJIDSubmit, profile, onSyncBJIDSubmit }) => {
return (
<BJIDFormBlock>
<h4>백준 아이디</h4>
<form onSubmit={onBJIDSubmit}>
<input
name="userBJID"
onChange={onChange}
value={profile.userBJID}
placeholder="백준 아이디"
/>
<button type="submit" onSubmit={onBJIDSubmit}>
등록
</button>
<button type="submit">등록</button>
</form>
<button onClick={onSyncBJIDSubmit}>동기화</button>
</BJIDFormBlock>
);
};
......
......@@ -33,8 +33,7 @@ const SectionContainer = styled.div`
display: flex;
`;
const SettingForm = ({ onChange, onBJIDSubmit, profile }) => {
console.log(profile);
const SettingForm = ({ onChange, onBJIDSubmit, profile, onSyncBJIDSubmit }) => {
return (
<SettingFormBlock>
<SectionContainer>
......@@ -47,6 +46,7 @@ const SettingForm = ({ onChange, onBJIDSubmit, profile }) => {
profile={profile}
onChange={onChange}
onBJIDSubmit={onBJIDSubmit}
onSyncBJIDSubmit={onSyncBJIDSubmit}
/>
</SectionContainer>
......
import React, { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { withRouter } from 'react-router-dom';
import { changeField, setBJID } from '../../modules/profile';
import {
changeField,
setBJID,
getPROFILE,
syncBJID,
} from '../../modules/profile';
import SettingForm from '../../components/setting/SettingForm';
import { sync } from '../../../node_modules/fast-glob/index';
const SettingContainer = ({ history }) => {
const dispatch = useDispatch();
const [error, setError] = useState(null);
......@@ -21,16 +27,25 @@ const SettingContainer = ({ history }) => {
);
};
const onSyncBJIDSubmit = (e) => {
e.preventDefault();
let username = profile.username;
dispatch(syncBJID({ username }));
};
const onBJIDSubmit = (e) => {
e.preventDefault();
let username = profile.username;
let userBJID = profile.userBJID;
dispatch(setBJID({ username, userBJID }));
};
useEffect(() => {
console.log('1');
let username = JSON.parse(user).username;
dispatch(getPROFILE({ username }));
//Do Init Form
console.log(profile);
}, [dispatch]);
return (
......@@ -38,6 +53,7 @@ const SettingContainer = ({ history }) => {
type="setting"
onChange={onChange}
onBJIDSubmit={onBJIDSubmit}
onSyncBJIDSubmit={onSyncBJIDSubmit}
profile={profile}
></SettingForm>
);
......
import client from './client';
export const setBJID = ({ username, BJID }) =>
client.post('api/profile/setprofile', { username, BJID });
export const setBJID = ({ username, userBJID }) =>
client.post('api/profile/setprofile', {
username: username,
userBJID: userBJID,
});
export const getPROFILE = ({ username }) =>
client.post('api/profile/getprofile', { username });
export const syncBJ = ({ username }) =>
client.patch('api/profile/syncBJ', { username });
......
......@@ -8,14 +8,27 @@ import { takeLatest } from 'redux-saga/effects';
const INITIALIZE = 'profile/INITIALIZE';
const CHANGE_FIELD = 'profile/CHANGE_FIELD';
const [SET_BJID, SET_BJID_SUCCESS, SET_BJID_FAILURE] = createRequestActionTypes(
'profile/SET_BJID',
);
const [
GET_PROFILE,
GET_PROFILE_SUCCESS,
GET_PROFILE_FAILURE,
] = createRequestActionTypes('profile/GET_PROFILE');
const [
SYNC_BJID,
SYNC_BJID_SUCCESS,
SYNC_BJID_FAILURE,
] = createRequestActionTypes('profile/SYNC_BJID');
export const setBJID = createAction(SET_BJID, ({ username, BJID }) => ({
export const syncBJID = createAction(SYNC_BJID, ({ username }) => ({
username,
}));
export const setBJID = createAction(SET_BJID, ({ username, userBJID }) => ({
username,
BJID,
userBJID,
}));
export const changeField = createAction(CHANGE_FIELD, ({ key, value }) => ({
......@@ -23,17 +36,23 @@ export const changeField = createAction(CHANGE_FIELD, ({ key, value }) => ({
value,
}));
export const getPROFILE = createAction(GET_PROFILE, ({ username }) => ({
username,
}));
const initialState = {
username: '',
userBJID: '',
solvedBJ: '',
friendList: [],
BJIDError: '',
profileError: '',
};
const getPROFILESaga = createRequestSaga(GET_PROFILE, profileAPI.getPROFILE);
const setBJIDSaga = createRequestSaga(SET_BJID, profileAPI.setBJID);
const syncBJIDSaga = createRequestSaga(SYNC_BJID, profileAPI.syncBJ);
export function* profileSaga() {
yield takeLatest(SET_BJID, setBJIDSaga);
yield takeLatest(GET_PROFILE, getPROFILESaga);
yield takeLatest(SYNC_BJID, syncBJIDSaga);
}
export default handleActions(
......@@ -43,15 +62,39 @@ export default handleActions(
produce(state, (draft) => {
draft[key] = value;
}),
[GET_PROFILE_SUCCESS]: (
state,
{ payload: { username, userBJID, solvedBJ, friendList } },
) => ({
...state,
username: username,
userBJID: userBJID,
solvedBJ: solvedBJ,
friendList: friendList,
profileError: null,
}),
[GET_PROFILE_FAILURE]: (state, { payload: error }) => ({
...state,
profileError: error,
}),
[SET_BJID_SUCCESS]: (state, { payload: BJID }) => ({
[SET_BJID_SUCCESS]: (state, { payload: { userBJID } }) => ({
...state,
BJID,
BJIDError: null,
userBJID: userBJID,
profileError: null,
}),
[SET_BJID_FAILURE]: (state, { payload: error }) => ({
...state,
BJIDError: error,
profileError: error,
}),
[SYNC_BJID_SUCCESS]: (state, { payload: { solvedBJ } }) => ({
...state,
solvedBJ,
profileError: null,
}),
[SYNC_BJID_FAILURE]: (state, { payload: error }) => ({
...state,
profileError: error,
}),
},
initialState,
......
......@@ -19,7 +19,7 @@
## API Table
| group | description | method | URL | Detail | Auth |
| ------- | --------------------------- | ------ | ------------------------ | -------- | --------- |
| ------- | --------------------------- | --------- | ------------------------ | -------- | --------- |
| user | 유저 등록 | POST | api/user | 바로가기 | JWT Token |
| user | 유저 삭제 | DELETE | api/user:id | 바로가기 | JWT Token |
| user | 특정 유저 조회 | GET | api/user:id | 바로가기 | None |
......@@ -29,8 +29,10 @@
| profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | 바로가기 | None |
| profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None |
| profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN |
| profile | 유저 정보 받아오기 | POST | api/profile/getprofile | 바로가기 | JWT |
| profile | 추천 문제 조회 | GET | api/profile/recommend:id | 바로가기 | None |
| notify | 슬랙 메시지 전송 요청 | POST | api/notify/slack | 바로가기 | Jwt Token |
| notify | 슬랙 메시지 전송 요청 | POST | api/notify/ |
| slack | 바로가기 | Jwt Token |
| auth | 로그인 | POST | api/auth/login | 바로가기 | None |
| auth | 로그아웃 | POST | api/auth/logout | 바로가기 | JWT Token |
| auth | 회원가입 | POST | api/auth/register | 바로가기 | None |
......
......@@ -88,7 +88,6 @@ exports.login = async (ctx) => {
GET api/auth/check
*/
exports.check = async (ctx) => {
console.log(ctx.state);
const { user } = ctx.state;
if (!user) {
ctx.status = 401;
......
......@@ -7,4 +7,5 @@ profile.get("/solvednum:id");
profile.get("/recommendps:id");
profile.patch("/syncBJ", profileCtrl.syncBJ);
profile.post("/setprofile", profileCtrl.setProfile);
profile.post("/getprofile", profileCtrl.getProfile);
module.exports = profile;
......
......@@ -13,6 +13,24 @@ exports.checkObjectId = (ctx, next) => {
}
return next();
};
/*POST /api/profile/getprofile
{
username: "username"
}
*/
exports.getProfile = async (ctx) => {
try {
const { username } = ctx.request.body;
const profile = await Profile.findByUsername(username);
if (!profile) {
ctx.status = 401;
return;
}
ctx.body = profile;
} catch (e) {
ctx.throw(500, e);
}
};
/*
POST /api/proflie/setprofile
{
......
......@@ -3,14 +3,11 @@ const User = require("../models/user");
const jwtMiddleware = async (ctx, next) => {
const token = ctx.cookies.get("access_token");
console.log("1");
console.log(token);
if (!token) {
console.log("1");
return next();
}
try {
console.log("1");
const decoded = jwt.verify(token, process.env.JWT_SECRET);
ctx.state.user = {
_id: decoded._id,
......@@ -25,7 +22,7 @@ const jwtMiddleware = async (ctx, next) => {
httpOnly: true,
});
}
console.log(decoded);
return next();
} catch (e) {
return next();
......