From 43e81090ef8301b326f9721251bad1fe10ab18f8 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Fri, 4 Dec 2020 02:11:42 +0300 Subject: refactor: use formik in EventForm --- src/containers/BsuFantomSection/EventForm.tsx | 148 ++++++++++++++------------ 1 file changed, 80 insertions(+), 68 deletions(-) (limited to 'src/containers') diff --git a/src/containers/BsuFantomSection/EventForm.tsx b/src/containers/BsuFantomSection/EventForm.tsx index 64d4165..09bb87b 100644 --- a/src/containers/BsuFantomSection/EventForm.tsx +++ b/src/containers/BsuFantomSection/EventForm.tsx @@ -1,5 +1,6 @@ -import React, { useState } from 'react'; +import React from 'react'; import { Grid, TextField, Button } from '@material-ui/core'; +import { Formik, Form, Field } from 'formik'; import { post } from '../../requests'; import { Event } from '../../types'; @@ -7,17 +8,17 @@ interface PropTypes { mutate: () => void; } -const EventForm: React.FC = ({ mutate }) => { - const [name, setName] = useState(''); - const [schedule, setSchedule] = useState('* * * * * *'); - const [attendanceId, setAttendanceId] = useState(''); - const [conferenceId, setConferenceId] = useState(''); +interface Fields { + name: string; + schedule: string; + attendanceId: string; + conferenceId: string; +} - const createHandler = (setter: any) => (event: React.ChangeEvent) => { - setter(event.target.value); - }; +const EventForm: React.FC = ({ mutate }) => { + const handleSubmit = (fields: Fields, { resetForm }: any) => { + const { name, schedule, attendanceId, conferenceId } = fields; - const handleSubmit = () => { if (schedule && conferenceId) { const event: Partial = { type: 'class', @@ -30,68 +31,79 @@ const EventForm: React.FC = ({ mutate }) => { } }; - setName(''); - setAttendanceId(''); - setConferenceId(''); - setSchedule(''); - - return post('/events', event).then(() => mutate()); + return post('/events', event).then(() => { + mutate(); + resetForm(); + }); } } return ( - - - - - - - - - - - - - - - - - + + {({ values, errors, touched, isSubmitting }) => ( +
+ + + + + + + + + + + + + + + + + +
+ )} +
); }; -- cgit v1.2.3