Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions src/components/FormikWrapper/TextField.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,44 @@
import React, { useCallback } from 'react'
import { Input, Form } from 'antd'
import { useField } from 'formik'
import React, { useCallback } from "react";
import { Input, Form } from "antd";
import { useField } from "formik";

export const TextField = (props: {
label?: React.ReactNode
name: string
placeholder?: string
type?: 'text' | 'password'
label?: React.ReactNode;
name: string;
placeholder?: string;
type?: "text" | "password";
value?: string;
}) => {
const [field, meta] = useField({ name: props.name })
const [field, meta] = useField({ name: props.name });
const renderInput = useCallback(() => {
switch (props.type) {
case 'text':
case "text":
return (
<Input {...field} id={props.name} placeholder={props.placeholder} />
)
case 'password':
);
case "password":
return (
<Input.Password
{...field}
id={props.name}
placeholder={props.placeholder}
/>
)
);
default:
return (
<Input {...field} id={props.name} placeholder={props.placeholder} />
)
);
}
}, [props.type, props.name, props.placeholder, field])
}, [props.type, props.name, props.placeholder, field]);
return (
<Form.Item
label={props.label}
hasFeedback
validateStatus={meta.error && 'error'}
validateStatus={meta.error && "error"}
help={meta.error}
htmlFor={props.name}
>
{renderInput()}
</Form.Item>
)
}
);
};
64 changes: 63 additions & 1 deletion src/modules/Auth/Login/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,69 @@
import React from "react";
import { Formik } from "formik";
import Modal from "antd/lib/modal/Modal";
import { Form, TextField } from "../../../components/FormikWrapper";
import { Space, Button, Typography } from "antd";
import * as yup from "yup";
import { Link } from "react-router-dom";

const LoginPage = () => {
return <div>login page</div>;
const validationSchema = React.useMemo(() => {
return yup.object({
email: yup.string().required(),
password: yup.string().required(),
});
}, []);

const handleSubmit = React.useCallback(() => {}, []);

return (
<>
<Formik
initialValues={{
email: "",
password: "",
}}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
<Modal
closable={false}
title={<Typography.Title level={2}>Login</Typography.Title>}
visible={true}
// onCancel={props.onToggleModal}
footer={
<Space style={{ display: "flex", justifyContent: "space-between" }}>
<Typography>
Dont have account ?{" "}
<Link to="/auth/register">register now</Link>{" "}
</Typography>
<Button
type="primary"
htmlType="submit"
// loading={projectSync.isMutating}
>
Login
</Button>
</Space>
}
>
<Form>
<TextField
name="email"
label="Email"
placeholder="example@mail.com"
/>
<TextField
name="password"
label="Password"
type="password"
placeholder="*********"
/>
</Form>
</Modal>
</Formik>
</>
);
};

export default LoginPage;
70 changes: 69 additions & 1 deletion src/modules/Auth/Register/RegisterPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,75 @@
import React from "react";
import { Formik } from "formik";
import Modal from "antd/lib/modal/Modal";
import { Typography, Space, Button } from "antd";
import { Link } from "react-router-dom";
import { Form, TextField } from "../../../components/FormikWrapper";
import * as yup from "yup";

const RegisterPage = () => {
return <div>register page</div>;
const validationSchema = React.useMemo(() => {
return yup.object({
email: yup.string().required(),
password: yup.string().required(),
password_confirmation: yup.string().required(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

validation buat password_confirmation ini bisa diimprove dengan membuatnya harus sama seperti password. Coba kamu lihat referensi ini https://til.hashrocket.com/posts/vahuw4phan-check-the-password-confirmation-with-yup

Suggested change
password_confirmation: yup.string().required(),
password_confirmation: yup.string()
.oneOf([yup.ref('password'), null], 'Passwords must match'),

});
}, []);

const handleSubmit = React.useCallback(() => {}, []);

return (
<>
<Formik
initialValues={{
email: "",
password: "",
}}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
<Modal
closable={false}
title={<Typography.Title level={2}>Register</Typography.Title>}
visible={true}
// onCancel={props.onToggleModal}
footer={
<Space style={{ display: "flex", justifyContent: "space-between" }}>
<Typography>
Already have account ? <Link to="/auth/login">login now</Link>
</Typography>
<Button
type="primary"
htmlType="submit"
// loading={projectSync.isMutating}
>
Register
</Button>
</Space>
}
>
<Form>
<TextField
name="email"
label="Email"
placeholder="example@mail.com"
/>
<TextField
name="password"
label="Password"
type="password"
placeholder="*********"
/>
<TextField
name="password_confirmation"
label="Password Confirmation"
type="password"
placeholder="*********"
/>
</Form>
</Modal>
</Formik>
</>
);
};

export default RegisterPage;
52 changes: 32 additions & 20 deletions src/modules/LoggedIn/Project/ProjectForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import React from "react";
import { Button, Modal, Divider, Space } from "antd";
import { useProjectSync, ProjectRequest } from "./useProjectSync";
import { useProjectSync, ProjectRequest, Project } from "./useProjectSync";
import { Formik } from "formik";
import * as yup from "yup";
import { Form, TextField } from "../../../components/FormikWrapper";

const ProjectForm = () => {
type FormProps = {
visible: boolean;
onToggleModal: () => void;
project?: Project;
};

const ProjectForm = (props: FormProps) => {
const projectSync = useProjectSync();
const [visible, setVisible] = React.useState<boolean>(false);
const [editedProject, setEditedProject] = React.useState<Project>();

React.useEffect(() => {
console.log("RENDERED!");

setEditedProject(props.project);
return () => {
console.log("UNMOUNTED");
};
}, [props.project]);

const validationSchema = React.useMemo(() => {
return yup.object({
Expand All @@ -16,29 +31,30 @@ const ProjectForm = () => {
});
}, []);

const toggleModal = React.useCallback(() => {
setVisible(prev => !prev);
}, []);

const handleSubmit = React.useCallback(
async (projectRequest: ProjectRequest) => {
await projectSync.createProject(projectRequest);
toggleModal();
if (props.project)
await projectSync.updateProject(projectRequest, props.project._id);
else await projectSync.createProject(projectRequest);
props.onToggleModal();
},
[projectSync, toggleModal]
[projectSync, props]
);

return (
<>
<Formik
initialValues={{ name: "", description: "" }}
initialValues={{
name: editedProject?.name || "",
description: editedProject?.description || "",
}}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
<Modal
title="Create Project"
visible={visible}
onCancel={toggleModal}
title={props.project ? "Update Project" : "Create Project"}
visible={props.visible}
onCancel={props.onToggleModal}
footer={null}
>
<Form>
Expand All @@ -50,22 +66,18 @@ const ProjectForm = () => {
/>
<Divider />
<Space>
<Button onClick={toggleModal}>Cancel</Button>
<Button onClick={props.onToggleModal}>Cancel</Button>
<Button
type="primary"
htmlType="submit"
loading={projectSync.isMutating}
>
Create Project
{props.project ? "Update Project" : "Create Project"}
</Button>
</Space>
</Form>
</Modal>
</Formik>

<Button type="primary" onClick={toggleModal}>
Create New Project
</Button>
</>
);
};
Expand Down
25 changes: 21 additions & 4 deletions src/modules/LoggedIn/Project/ProjectList.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import React from "react";
import { Card, Row, Col } from "antd";
import { useProjectSync } from "./useProjectSync";
import { useProjectSync, Project } from "./useProjectSync";
import { Popconfirm } from "antd";
import { DeleteOutlined } from "@ant-design/icons";
import { DeleteOutlined, EditOutlined } from "@ant-design/icons";

const ProjectList = (props: {
onSelect: (project: Project) => void;
onToggleModal: () => void;
}) => {

const handleEditClick = React.useCallback(
(project: Project) => {
props.onSelect(project);
props.onToggleModal();
},
[props]
);

const ProjectList = () => {
const projectSync = useProjectSync();

const confirm = React.useCallback(
Expand Down Expand Up @@ -32,7 +44,7 @@ const ProjectList = () => {
</Col>
</>
) : (
projectSync.projects.map(project => {
projectSync.projects.map((project: Project) => {
return (
<Col span={6} key={project._id}>
<Card
Expand All @@ -51,6 +63,11 @@ const ProjectList = () => {
}}
/>
</Popconfirm>,
<EditOutlined
onClick={() => {
handleEditClick(project);
}}
/>,
]}
>
<Card.Meta
Expand Down
Loading