Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 0963f45

Browse files
form redux logic moved to form not in modal, drawer
1 parent 7c36bb8 commit 0963f45

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/forms/todo/drawer.form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default function DrawerForm() {
3434
Add new todo
3535
</Button>
3636
<Drawer
37-
title="Add new todo"
37+
title="Todo"
3838
width={500}
3939
placement="right"
4040
closable={false}

src/forms/todo/modal.form.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,22 @@ import { editTodo } from '../../store/actions/api.actions';
77

88
export default function ModalForm() {
99
const [visible, setVisible] = useState(false);
10-
const [formData, setFormData] = useState({});
1110
const dispatch = useDispatch();
1211
const { editTodoData } = useSelector((state) => state.api);
1312

1413
useEffect(() => {
1514
if (!isEmpty(editTodoData)) {
1615
setVisible(true);
17-
setFormData(editTodoData);
1816
}
1917
}, [editTodoData]);
2018

2119
const showForm = () => {
2220
dispatch(editTodo());
2321
setVisible(true);
24-
setFormData({});
2522
};
2623

2724
const onClose = () => {
25+
dispatch(editTodo());
2826
setVisible(false);
2927
};
3028

@@ -34,16 +32,12 @@ export default function ModalForm() {
3432
Add new todo
3533
</Button>
3634
<Modal
37-
title="Add new todo"
35+
title="Todo"
3836
visible={visible}
3937
onCancel={onClose}
4038
footer={null}
4139
>
42-
<TodoForm
43-
onClose={onClose}
44-
editMode={!isEmpty(formData)}
45-
editableTodoData={formData}
46-
/>
40+
<TodoForm onClose={onClose} />
4741
</Modal>
4842
</div>
4943
);

src/forms/todo/todo.form.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useState, useEffect } from 'react';
2-
import { useDispatch } from 'react-redux';
2+
import { useDispatch, useSelector } from 'react-redux';
33
import { message, Col, Row } from 'antd';
44
import { Form, Input, DatePicker, Select } from 'formik-antd';
55
import { Formik } from 'formik';
@@ -23,29 +23,38 @@ const TodoSchema = Yup.object({
2323

2424
const TOMORROW = moment().add(1, 'days');
2525

26-
function TodoForm({ onClose, editMode = false, editableTodoData = {} }) {
26+
function TodoForm({ onClose }) {
2727
const dispatch = useDispatch();
28+
const { editTodoData } = useSelector((state) => state.api);
2829
const [dueDate, setDueDate] = useState();
2930
const [todoLabel, setTodoLabel] = useState();
30-
31-
const formInitialValues = {
32-
title: editMode ? editableTodoData.title : undefined,
33-
description: editMode ? editableTodoData.description : undefined,
34-
dueDate: editMode ? moment(editableTodoData.dueDate) : TOMORROW,
35-
label: editMode ? editableTodoData.label : 1,
36-
};
31+
const [editMode, setEditMode] = useState(false);
32+
const [formInitialValues, setFormInitialValues] = useState({});
3733

3834
useEffect(() => {
39-
if (!isEmpty(editableTodoData)) {
40-
setDueDate(moment(editableTodoData.dueDate));
35+
if (!isEmpty(editTodoData)) {
36+
setEditMode(true);
37+
setDueDate(moment(editTodoData.dueDate));
38+
} else {
39+
setEditMode(false);
4140
}
42-
}, [editableTodoData]);
41+
}, [editTodoData]);
42+
43+
useEffect(() => {
44+
const formInitialValues = {
45+
title: editMode ? editTodoData.title : undefined,
46+
description: editMode ? editTodoData.description : undefined,
47+
dueDate: editMode ? moment(editTodoData.dueDate) : TOMORROW,
48+
label: editMode ? editTodoData.label : 1,
49+
};
50+
setFormInitialValues(formInitialValues);
51+
}, [editMode]);
4352

4453
function handleSubmit(values, { setErrors, resetForm, setSubmitting }) {
4554
let url = `todos`;
4655

4756
if (editMode) {
48-
url = `${url}/${editableTodoData._id}`;
57+
url = `${url}/${editTodoData._id}`;
4958
}
5059

5160
const CREDENTIALS = {
@@ -183,8 +192,6 @@ function TodoForm({ onClose, editMode = false, editableTodoData = {} }) {
183192

184193
TodoForm.propTypes = {
185194
onClose: PropTypes.func.isRequired,
186-
// editMode: PropTypes.bool.isRequired,
187-
// editableTodoData: PropTypes.object.isRequired,
188195
};
189196

190197
export { TodoForm };

0 commit comments

Comments
 (0)