송용우

Update BJIDForm

1 +import React from 'react';
2 +import styled from 'styled-components';
3 +import Button from '../common/Button';
4 +import palette from '../../lib/styles/palette';
5 +const BJIDFormBlock = styled.div`
6 + width: 100%;
7 + border-top: 1px solid ${palette.gray[2]};
8 + padding-top: 2rem;
9 + h4 {
10 + color: ${palette.gray[8]};
11 + margin-top: 0;
12 + margin-bottom: 0.5rem;
13 + }
14 +`;
15 +
16 +const BJIDForm = ({ onChange, onBJIDSubmit, profile }) => {
17 + return (
18 + <BJIDFormBlock>
19 + <h4>백준 아이디</h4>
20 + <input
21 + name="userBJID"
22 + onChange={onChange}
23 + value={profile.userBJID}
24 + placeholder="백준 아이디"
25 + />
26 + <button type="submit" onSubmit={onBJIDSubmit}>
27 + 등록
28 + </button>
29 + </BJIDFormBlock>
30 + );
31 +};
32 +export default BJIDForm;
1 +import React from 'react';
2 +import styled from 'styled-components';
3 +import Button from '../common/Button';
4 +import palette from '../../lib/styles/palette';
5 +import BJIDForm from './BJIDForm';
6 +
7 +const SettingFormBlock = styled.div`
8 + h3 {
9 + margin: 0;
10 + color: ${palette.gray[8]};
11 + margin-bottom: 1rem;
12 + }
13 + background: ${palette.gray[2]};
14 + margin: 0 auto;
15 + display: flex;
16 + flex-direction: column;
17 +`;
18 +const StyledInput = styled.input`
19 + font-size: 1rem;
20 + border: none;
21 + border-bottom: 1px solid ${palette.gray[5]};
22 + padding-bottom: 0.5rem;
23 + outline: none;
24 + &:focus {
25 + color: $oc-teal-7;
26 + border-bottom: 1px solid ${palette.gray[7]};
27 + }
28 + & + & {
29 + margin-top: 1rem;
30 + }
31 +`;
32 +const SectionContainer = styled.div`
33 + display: flex;
34 +`;
35 +
36 +const SettingForm = ({ onChange, onBJIDSubmit, profile }) => {
37 + console.log(profile);
38 + return (
39 + <SettingFormBlock>
40 + <SectionContainer>
41 + <h3>{profile.username}</h3>
42 + <p>입력</p>
43 + </SectionContainer>
44 +
45 + <SectionContainer>
46 + <BJIDForm
47 + profile={profile}
48 + onChange={onChange}
49 + onBJIDSubmit={onBJIDSubmit}
50 + />
51 + </SectionContainer>
52 +
53 + <SectionContainer>
54 + <h3>친구</h3>
55 + <StyledInput name="BJID" placeholder="친구 아이디" />
56 + <Button>추가</Button>
57 + </SectionContainer>
58 + <h3>친구 리스트</h3>
59 + </SettingFormBlock>
60 + );
61 +};
62 +
63 +export default SettingForm;
1 +import React, { useEffect, useState } from 'react';
2 +import { useDispatch, useSelector } from 'react-redux';
3 +import { withRouter } from 'react-router-dom';
4 +import { changeField, setBJID } from '../../modules/profile';
5 +import SettingForm from '../../components/setting/SettingForm';
6 +const SettingContainer = ({ history }) => {
7 + const dispatch = useDispatch();
8 + const [error, setError] = useState(null);
9 + const { user, profile } = useSelector(({ user, profile }) => ({
10 + user: user.user,
11 + profile: profile,
12 + }));
13 +
14 + const onChange = (e) => {
15 + const { value, name } = e.target;
16 + dispatch(
17 + changeField({
18 + key: name,
19 + value: value,
20 + }),
21 + );
22 + };
23 +
24 + const onBJIDSubmit = (e) => {
25 + e.preventDefault();
26 + let username = profile.username;
27 + let userBJID = profile.userBJID;
28 + dispatch(setBJID({ username, userBJID }));
29 + };
30 +
31 + useEffect(() => {
32 + //Do Init Form
33 + console.log(profile);
34 + }, [dispatch]);
35 +
36 + return (
37 + <SettingForm
38 + type="setting"
39 + onChange={onChange}
40 + onBJIDSubmit={onBJIDSubmit}
41 + profile={profile}
42 + ></SettingForm>
43 + );
44 +};
45 +
46 +export default withRouter(SettingContainer);
1 +import client from './client';
2 +
3 +export const setBJID = ({ username, BJID }) =>
4 + client.post('api/profile/setprofile', { username, BJID });
...@@ -5,19 +5,19 @@ import createRequestSaga, { ...@@ -5,19 +5,19 @@ import createRequestSaga, {
5 createRequestActionTypes, 5 createRequestActionTypes,
6 } from '../lib/createRequestSaga'; 6 } from '../lib/createRequestSaga';
7 import * as authAPI from '../lib/api/auth'; 7 import * as authAPI from '../lib/api/auth';
8 -const CHAGE_FIELD = 'auth/CHANGE_FIELD'; 8 +const CHANGE_FIELD = 'auth/CHANGE_FIELD';
9 const INITIALIZE_FORM = 'auth/INITIALIZE_FORM'; 9 const INITIALIZE_FORM = 'auth/INITIALIZE_FORM';
10 10
11 -const REGISTER = 'auth/REGISTER'; 11 +const [REGISTER, REGISTER_SUCCESS, REGISTER_FAILURE] = createRequestActionTypes(
12 -const REGISTER_SUCCESS = 'auth/REGISTER_SUCCESS'; 12 + 'auth/REGISTER',
13 -const REGISTER_FAILURE = 'auth/REGISTER_FAILURE'; 13 +);
14 14
15 -const LOGIN = 'auth/LOGIN'; 15 +const [LOGIN, LOGIN_SUCCESS, LOGIN_FAILURE] = createRequestActionTypes(
16 -const LOGIN_SUCCESS = 'auth/LOGIN_SUCCESS'; 16 + 'auth/REGISTER',
17 -const LOGIN_FAILURE = 'auth/LOGIN_FAILURE'; 17 +);
18 18
19 export const changeField = createAction( 19 export const changeField = createAction(
20 - CHAGE_FIELD, 20 + CHANGE_FIELD,
21 ({ form, key, value }) => ({ 21 ({ form, key, value }) => ({
22 form, 22 form,
23 key, 23 key,
...@@ -59,7 +59,7 @@ export function* authSaga() { ...@@ -59,7 +59,7 @@ export function* authSaga() {
59 59
60 const auth = handleActions( 60 const auth = handleActions(
61 { 61 {
62 - [CHAGE_FIELD]: (state, { payload: { form, key, value } }) => 62 + [CHANGE_FIELD]: (state, { payload: { form, key, value } }) =>
63 produce(state, (draft) => { 63 produce(state, (draft) => {
64 draft[form][key] = value; 64 draft[form][key] = value;
65 }), 65 }),
......
...@@ -3,15 +3,17 @@ import { all } from 'redux-saga/effects'; ...@@ -3,15 +3,17 @@ import { all } from 'redux-saga/effects';
3 import auth, { authSaga } from './auth'; 3 import auth, { authSaga } from './auth';
4 import loading from './loading'; 4 import loading from './loading';
5 import user, { userSaga } from './user'; 5 import user, { userSaga } from './user';
6 +import profile, { profileSaga } from './profile';
6 7
7 const rootReducer = combineReducers({ 8 const rootReducer = combineReducers({
8 auth, 9 auth,
9 loading, 10 loading,
10 user, 11 user,
12 + profile,
11 }); 13 });
12 14
13 export function* rootSaga() { 15 export function* rootSaga() {
14 - yield all([authSaga(), userSaga()]); 16 + yield all([authSaga(), userSaga(), profileSaga()]);
15 } 17 }
16 18
17 export default rootReducer; 19 export default rootReducer;
......
1 +import { createAction, handleActions } from 'redux-actions';
2 +import createRequestSaga, {
3 + createRequestActionTypes,
4 +} from '../lib/createRequestSaga';
5 +import produce from 'immer';
6 +import * as profileAPI from '../lib/api/profile';
7 +import { takeLatest } from 'redux-saga/effects';
8 +
9 +const INITIALIZE = 'profile/INITIALIZE';
10 +const CHANGE_FIELD = 'profile/CHANGE_FIELD';
11 +
12 +const [SET_BJID, SET_BJID_SUCCESS, SET_BJID_FAILURE] = createRequestActionTypes(
13 + 'profile/SET_BJID',
14 +);
15 +
16 +export const setBJID = createAction(SET_BJID, ({ username, BJID }) => ({
17 + username,
18 + BJID,
19 +}));
20 +
21 +export const changeField = createAction(CHANGE_FIELD, ({ key, value }) => ({
22 + key,
23 + value,
24 +}));
25 +
26 +const initialState = {
27 + username: '',
28 + userBJID: '',
29 + solvedBJ: '',
30 + friendList: [],
31 + BJIDError: '',
32 +};
33 +
34 +const setBJIDSaga = createRequestSaga(SET_BJID, profileAPI.setBJID);
35 +export function* profileSaga() {
36 + yield takeLatest(SET_BJID, setBJIDSaga);
37 +}
38 +
39 +export default handleActions(
40 + {
41 + [INITIALIZE]: (state) => initialState,
42 + [CHANGE_FIELD]: (state, { payload: { key, value } }) =>
43 + produce(state, (draft) => {
44 + draft[key] = value;
45 + }),
46 +
47 + [SET_BJID_SUCCESS]: (state, { payload: BJID }) => ({
48 + ...state,
49 + BJID,
50 + BJIDError: null,
51 + }),
52 + [SET_BJID_FAILURE]: (state, { payload: error }) => ({
53 + ...state,
54 + BJIDError: error,
55 + }),
56 + },
57 + initialState,
58 +);
1 import React from 'react'; 1 import React from 'react';
2 import HeaderContainer from '../containers/common/HeaderContainer'; 2 import HeaderContainer from '../containers/common/HeaderContainer';
3 -import Button from '../components/common/Button'; 3 +import SettingForm from '../components/setting/SettingForm';
4 +import SettingContainer from '../containers/setting/SettingContainer';
4 5
5 const SettingPage = () => { 6 const SettingPage = () => {
6 return ( 7 return (
7 <div> 8 <div>
8 <HeaderContainer /> 9 <HeaderContainer />
9 - <Button>setting</Button> 10 + <SettingContainer></SettingContainer>
10 </div> 11 </div>
11 ); 12 );
12 }; 13 };
......