Django 프로젝트02 - 리액트와 SPA 방식으로 인스타그램 만들기(7)
2020년08월20일에듀캐스트 장고&리액트 강의를 듣고 정리하는 글이다.
Ant Design Form을 활용한 회원가입 폼 만들기
import React, { useState, useEffect } from "react";
import { Form, Input, Button, notification } from "antd";
import { SmileOutlined, FrownOutlined } from "@ant-design/icons";
import { useHistory } from "react-router-dom";
import Axios from "axios";
export default function Signup() {
const history = useHistory();
const [fieldErrors, setFieldErrors] = useState({});
const onFinish = (values) => {
async function fn() {
const { username, password } = values;
setFieldErrors({});
const data = { username, password };
try {
await Axios.post("http://localhost:8000/accounts/signup/", data);
notification.open({
message: "회원가입 성공",
description: "로그인 페이지로 이동합니다.",
icon: <SmileOutlined style=/>,
});
history.push("/accounts/login")
} catch (error) {
if (error.response) {
notification.open({
message: "회원가입 실패",
description: "아이디/암호를 확인해주세요.",
icon: <FrownOutlined style=/>,
});
const { data: fieldsErrorMessages } = error.response;
// python: mydict.items()
setFieldErrors(
Object.entries(fieldsErrorMessages).reduce(
(acc, [fieldName, errors]) => {
/// errors : ["m1", "m2"].join(" ") => "m1 m2"
acc[fieldName] = {
validateStatus: "error",
help: errors.join(" "),
};
return acc;
},
{}
)
);
}
}
}
fn();
};
return (
<Form
{...layout}
onFinish={onFinish}
// onFinishFailed={onFinishFailed}
>
<Form.Item
label="Username"
name="username"
rules={[
{ required: true, message: "Please input your username!" },
{ min: 5, message: "5글자 입력해주세요." },
]}
hasFeedback
{...fieldErrors.username}
>
<Input />
</Form.Item>
<Form.Item
label="Password"
name="password"
rules={[{ required: true, message: "Please input your password!" }]}
{...fieldErrors.password}
>
<Input.Password />
</Form.Item>
<Form.Item {...tailLayout}>
<Button type="primary" htmlType="submit">
Submit
</Button>
</Form.Item>
</Form>
);
}
const layout = {
labelCol: { span: 8 },
wrapperCol: { span: 16 },
};
const tailLayout = {
wrapperCol: { offset: 8, span: 16 },
};