Skip to content

Commit

Permalink
feat:add update info
Browse files Browse the repository at this point in the history
  • Loading branch information
jundm committed Mar 30, 2022
1 parent 7d0e0e0 commit 249ef0d
Show file tree
Hide file tree
Showing 6 changed files with 290 additions and 14 deletions.
10 changes: 6 additions & 4 deletions frontend/components/HeaderBig/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ const HeaderBig = (props: HeaderProps) => {
{accountUserName ? (
<>
<HeaderLoginDiv>
<WrapperUser>
<UserNameDiv>{accountUserName}</UserNameDiv>
<ProfileDiv>{Safari ? "😍" : <Heart />}</ProfileDiv>
</WrapperUser>
<Link href="/accounts/userinfo">
<WrapperUser>
<UserNameDiv>{accountUserName}</UserNameDiv>
<ProfileDiv>{Safari ? "😍" : <Heart />}</ProfileDiv>
</WrapperUser>
</Link>
<Logout onClick={onLogout}>logout</Logout>
</HeaderLoginDiv>
</>
Expand Down
2 changes: 2 additions & 0 deletions frontend/components/HeaderSmall/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ const HeaderSmall = (props: HeaderProps) => {
</Link>

{accountUserName ? (
<Link href="/accounts/userinfo">
<MenuLi>
{accountUserName.slice(0, 10)}
<ProfileDiv>{Safari ? "😍" : <Heart />}</ProfileDiv>
</MenuLi>
</Link>
) : (
<>
<Link href="/accounts/login">
Expand Down
5 changes: 3 additions & 2 deletions frontend/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Cookies from "universal-cookie";
import { detect } from "detect-browser";
import axios from "axios";
import { useAppSelector, useAppDispatch } from "@utils/Toolkit/hook";
import { name, userName } from "@utils/Toolkit/Slice/userSlice";
import { email, name, userName } from "@utils/Toolkit/Slice/userSlice";
import { useRouter } from "next/router";
import { parseJwt, setVerifyToken } from "@utils/Cookies/TokenManager";
import Footer from "@components/Footer";
Expand Down Expand Up @@ -42,7 +42,8 @@ function App({ Component, pageProps }: AppProps) {
.get(process.env.NEXT_PUBLIC_ENV_BASE_URL + "users/me/")
.then((res) => {
dispatch(userName(res.data.username));
dispatch(name(res.data.username));
dispatch(name(res.data.name));
dispatch(email(res.data.email));
})
.catch((e) => console.error(e.message));
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/pages/accounts/login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ function Login() {
password,
})
.then((res) => {
router.push("/");
const access = res.data.access;
const refresh = res.data.refresh;
setAccessToken(access);
setRefreshToken(refresh);
router.push("/");
if (rememberUserEmail === true) {
dispatch(globalEmail(email));
}
})
.catch((e) => console.warn(e.message));
.catch((e) => alert("이메일 혹은 비밀번호가 잘못 되었습니다"));
setIsLoading(false);
form.resetFields(["email", "password"]);
};
Expand Down
276 changes: 271 additions & 5 deletions frontend/pages/accounts/userinfo/index.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,275 @@
import React from "react";
import React, { useEffect, useState } from "react";
import axios from "axios";
import {
Form,
Input,
InputNumber,
Cascader,
Select,
Row,
Col,
Checkbox,
Button,
AutoComplete,
Card,
} from "antd";
import { useRouter } from "next/router";
import Cookies from "universal-cookie";
import { useSelector } from "react-redux";
import { userName } from "@utils/Toolkit/Slice/userSlice";
import { GetServerSideProps } from "next";

interface UserInfoProps {}
//TODO 추가예정=[validator, add form, email인증]
interface SignUpProps {}

function UserInfo({}: UserInfoProps) {
return <div>UserInfo</div>;
}
const loginWidth = 500;
const { Option } = Select;

const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 6 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 16 },
},
};
const tailFormItemLayout = {
wrapperCol: {
xs: {
span: 24,
offset: 0,
},
sm: {
span: 16,
offset: 6,
},
},
};

function UserInfo(this: any, {}: SignUpProps) {
const router = useRouter();
const cookies = new Cookies();
const accountUser = useSelector(userName);
const accountUserName = accountUser.payload.auth.username;
const accountName = accountUser.payload.auth.name;
const accountEmail = accountUser.payload.auth.email;
//*@param: email async-validator message remove {https://github.com/yiminghe/async-validator/issues/92}
const warn = console.warn;
console.warn = (...args: any[]) => {
if (typeof args[0] === "string" && args[0].startsWith("async-validator:"))
return;
warn(...args);
};
useEffect(() => {
if (!cookies.get("accessToken") && !cookies.get("refreshToken")) {
router.back();
}
});

const [form] = Form.useForm();
const [isLoading, setIsLoading] = useState(false);
const [inputs, setInputs] = useState({
name: accountName,
username: accountUserName,
email: accountEmail,
old_password: "",
password: "",
re_password: "",
});
const { name, username, email, old_password, password, re_password } = inputs;

const onChange = (e: any) => {
const { name, value } = e.target;
setInputs({
...inputs,
[name]: value,
});
};

const onFinish = async (value: any) => {
setIsLoading(true);
axios
.post(process.env.NEXT_PUBLIC_ENV_BASE_URL + "users/set_password/", {
new_password: password,
re_new_password: re_password,
current_password: old_password,
})
.then((res) => {
form.resetFields(["old_password", "password", "re_password"]);
router.push("/");
alert("정보가 수정되었습니다!");
})
.catch((e) => {
alert(e.message);
});
setIsLoading(false);
};

return (
<div className="" style={{ margin: "auto" }}>
<Card
title="Update Info"
bordered={true}
style={{ width: `${loginWidth + 50}px`, margin: "0 auto" }}
>
<Form
{...formItemLayout}
form={form}
name="register"
onFinish={onFinish}
initialValues={{
email: accountEmail,
name: accountName,
username: accountUserName,
}}
scrollToFirstError
>
<Form.Item
name="email"
label="이메일"
tooltip="중복체크 기능이 아직 없습니다. 본인 메일로 가입해주세요."
rules={[
{
type: "email",
message: "이메일 형식으로 입력하세요!",
},
{
required: true,
message: "이메일을 입력하세요!",
},
]}
>
<Input name="email" onChange={onChange} value={email} disabled />
</Form.Item>

<Form.Item
name="old_password"
label="기존 비밀번호"
rules={[
{
required: true,
message: "비밀번호를 입력하세요!",
},
]}
hasFeedback
>
<Input.Password
name="old_password"
onChange={onChange}
value={old_password}
/>
</Form.Item>
<Form.Item
name="password"
label="비밀번호"
rules={[
{
required: true,
message: "비밀번호를 입력하세요!",
},
]}
hasFeedback
>
<Input.Password
name="password"
onChange={onChange}
value={password}
/>
</Form.Item>

<Form.Item
name="re_password"
label="비밀번호 확인"
dependencies={["password"]}
hasFeedback
rules={[
{
required: true,
message: "비밀번호를 확인 하세요!",
},
({ getFieldValue }) => ({
validator(_, value) {
if (!value || getFieldValue("password") === value) {
return Promise.resolve();
}
return Promise.reject(
new Error(
"The two passwords that you entered do not match!"
)
);
},
}),
]}
>
<Input.Password
name="re_password"
onChange={onChange}
value={re_password}
/>
</Form.Item>

<Form.Item
name="name"
label="실명"
tooltip="사용자의 이름을 기입합니다."
rules={[
{
required: true,
message: "실명을 입력하세요!",
whitespace: false,
},
]}
>
<Input name="name" onChange={onChange} value={name} disabled />
</Form.Item>
<Form.Item
name="username"
label="닉네임"
tooltip="닉네임을 사용 합니다(중복 불가)"
rules={[
{
required: true,
message: "닉네임을 입력하세요!",
whitespace: true,
},
]}
>
<Input
name="username"
onChange={onChange}
value={username}
disabled
/>
</Form.Item>

<Form.Item {...tailFormItemLayout}>
<Button type="primary" htmlType="submit" disabled={isLoading}>
{isLoading ? "정보수정중.." : "정보수정"}
</Button>
</Form.Item>
</Form>
</Card>
</div>
);
}
export const getServerSideProps: GetServerSideProps = async (context) => {
const cookieReq = context.req ? context.req.headers.cookie : null;
const cookies = new Cookies(cookieReq);
const refreshToken = cookies.get("refreshToken");
const resolvedUrl = context.resolvedUrl;
if (refreshToken) {
return {
props: {},
};
} else {
return {
redirect: {
destination: resolvedUrl.slice(0, -7),
permanent: false,
},
};
}
};
export default UserInfo;
7 changes: 6 additions & 1 deletion frontend/utils/Toolkit/Slice/userSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import { HYDRATE } from "next-redux-wrapper";
interface AuthState {
username: string;
name: string;
email: string;
}
const initialState: AuthState = {
username: "",
name: "",
email: "",
};

export const userSlice = createSlice({
Expand All @@ -24,6 +26,9 @@ export const userSlice = createSlice({
name: (state, { payload }) => {
state.name = payload;
},
email: (state, { payload }) => {
state.email = payload;
},
},
extraReducers: {
[HYDRATE]: (state, action) => {
Expand All @@ -34,5 +39,5 @@ export const userSlice = createSlice({
},
},
});
export const { userName, name } = userSlice.actions;
export const { userName, name, email } = userSlice.actions;
export default userSlice.reducer;

0 comments on commit 249ef0d

Please sign in to comment.