diff --git a/app/js/events/components/EventModal.jsx b/app/js/events/components/EventModal.jsx index 8dff758f..796918d8 100644 --- a/app/js/events/components/EventModal.jsx +++ b/app/js/events/components/EventModal.jsx @@ -1,9 +1,12 @@ import React, { Component } from 'react'; import PropTypes from 'prop-types'; -import { Field, Form, reduxForm } from 'redux-form'; -import { Modal, ModalHeader, ModalBody, ModalFooter } from 'reactstrap'; -import Button from 'common/components/Button'; +import { Modal, ModalHeader, ModalBody, ModalFooter, FormFeedback } from 'reactstrap'; import moment from 'moment'; +import { Formik } from 'formik'; + +import Button from 'common/components/Button'; +import FormikDatetime from 'common/components/FormikDatetime'; +import yup from 'utils/yup'; class EventModal extends Component { static propTypes = { @@ -17,8 +20,6 @@ class EventModal extends Component { location: PropTypes.string, image: PropTypes.string, }), - handleSubmit: PropTypes.func.isRequired, - initialize: PropTypes.func.isRequired, isOpen: PropTypes.bool.isRequired, close: PropTypes.func.isRequired, create: PropTypes.func.isRequired, @@ -27,160 +28,229 @@ class EventModal extends Component { } static defaultProps = { - event: null, - } - - componentDidUpdate(prevProps) { - const { - isOpen, - initialize, - event, - } = this.props; - if (isOpen && !prevProps.isOpen) { - initialize(event); - } - } - - submit = (values) => { - const { - event, - create, - update, - close, - } = this.props; - - const newEvent = { - ...values, - startDate: moment(values.startDate).toISOString(), - endDate: moment(values.endDate).toISOString(), - }; - - close(); - - if (event) update(event.id, newEvent); - else create(newEvent); + event: {}, } render() { const { isOpen, - handleSubmit, close, event, committees, + create, + update, } = this.props; - const options = committees.map(committee => ); - const updateOrCreate = event ? 'Update' : 'Create'; - + const committeeNames = committees.map(committee => committee.name); + const updateOrCreate = event.id ? 'Update' : 'Create'; return ( {updateOrCreate} -
- -
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- yup.object() + .shape({ + name: yup.string() + .required('Required') + .max(25, 'Event names can have a maximum of 25 characters'), + startDate: yup.date() + .required('Required') + .isFuture('Start Date must be in the future'), + endDate: yup.date() + .required('Required') + .isFuture('End Date must be in the future') + .isAfter(yup.ref('startDate'), 'End Date must come after Start Date'), + description: yup.string(), + location: yup.string().required('Required'), + image: yup.string().url('Must be a valid URL'), + committeeName: yup.string().required('Required').oneOf(committeeNames), + }) + } + onSubmit={( + values + ) => { + const newEvent = { + ...values, + startDate: moment(values.startDate).toISOString(), + endDate: moment(values.endDate).toISOString(), + image: values.image || null, // If an empty string (''), the API expects 'null' + }; + + close(); + + if (event.id) { + update(event.id, newEvent); + } else { + create(newEvent); + } + }} + render={({ + values, + errors, + touched, + handleChange, + handleBlur, + handleSubmit, + setFieldValue, + setFieldTouched, + isSubmitting, + }) => ( + + +
+ +
+ + {touched.name + && errors.name + && {errors.name}} +
+
+
+ +
+ + {touched.startDate + && errors.startDate + && {errors.startDate}} +
+
+
+ +
+ + {touched.endDate + && errors.endDate + && {errors.endDate}} +
+
+
+ +
+