|
| 1 | +import React from 'react'; |
| 2 | +import {t} from 'i18next'; |
| 3 | +import classnames from 'classnames'; |
| 4 | +import chrono from 'chrono-node'; |
| 5 | +import PropTypes from 'prop-types'; |
| 6 | +import ImmutablePropTypes from 'react-immutable-proptypes'; |
| 7 | +import {Field} from 'redux-form/immutable'; |
| 8 | + |
| 9 | +import FormDate from '../records/FormDate'; |
| 10 | + |
| 11 | +import AssignmentCreatorTextField from './AssignmentCreatorTextField'; |
| 12 | +import AssignmentCreatorSelectField from './AssignmentCreatorSelectField'; |
| 13 | + |
| 14 | +function parseDate(value) { |
| 15 | + const [parsedResponse] = chrono.parse(value); |
| 16 | + let parsedDate; |
| 17 | + if (parsedResponse) { |
| 18 | + parsedDate = parsedResponse.start.date(); |
| 19 | + } |
| 20 | + return new FormDate({ |
| 21 | + string: value, |
| 22 | + parsedDate, |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +function formatDate(value) { |
| 27 | + if (value) { |
| 28 | + return value.string; |
| 29 | + } |
| 30 | + return ''; |
| 31 | +} |
| 32 | + |
| 33 | +export default function AssignmentCreatorForm({ |
| 34 | + courses, |
| 35 | + handleSubmit, |
| 36 | + parsedDate, |
| 37 | + onAssignAssignment, |
| 38 | + onDraftAssignment, |
| 39 | + onCloseAssignmentCreator, |
| 40 | +}) { |
| 41 | + return ( |
| 42 | + <form> |
| 43 | + <div> |
| 44 | + <Field |
| 45 | + component={AssignmentCreatorSelectField} |
| 46 | + name="course" |
| 47 | + > |
| 48 | + <option value="">{t('assignment-creator.select-class')}</option> |
| 49 | + { |
| 50 | + courses.map(course => ( |
| 51 | + <option |
| 52 | + key={course.id} |
| 53 | + value={course.id} |
| 54 | + > |
| 55 | + {course.name} |
| 56 | + </option> |
| 57 | + )).valueSeq() |
| 58 | + } |
| 59 | + </Field> |
| 60 | + </div> |
| 61 | + <div> |
| 62 | + <Field |
| 63 | + component={AssignmentCreatorTextField} |
| 64 | + format={formatDate} |
| 65 | + name="date" |
| 66 | + parse={parseDate} |
| 67 | + placeholder={t('assignment-creator.input-placeholder')} |
| 68 | + type="text" |
| 69 | + valueLabel={parsedDate} |
| 70 | + /> |
| 71 | + </div> |
| 72 | + <button |
| 73 | + className={classnames( |
| 74 | + 'assignment-creator__button', |
| 75 | + 'assignment-creator__button_reject', |
| 76 | + )} |
| 77 | + type="button" |
| 78 | + onClick={onCloseAssignmentCreator} |
| 79 | + > |
| 80 | + {t('assignment-creator.cancel-button')} |
| 81 | + </button> |
| 82 | + <button |
| 83 | + className={classnames( |
| 84 | + 'assignment-creator__button', |
| 85 | + 'assignment-creator__button_confirm', |
| 86 | + )} |
| 87 | + type="button" |
| 88 | + onClick={handleSubmit(onDraftAssignment)} |
| 89 | + > |
| 90 | + {t('assignment-creator.draft-button')} |
| 91 | + </button> |
| 92 | + <button |
| 93 | + className={classnames( |
| 94 | + 'assignment-creator__button', |
| 95 | + 'assignment-creator__button_confirm', |
| 96 | + )} |
| 97 | + type="button" |
| 98 | + onClick={handleSubmit(onAssignAssignment)} |
| 99 | + > |
| 100 | + {t('assignment-creator.assign-button')} |
| 101 | + </button> |
| 102 | + </form> |
| 103 | + ); |
| 104 | +} |
| 105 | + |
| 106 | +AssignmentCreatorForm.propTypes = { |
| 107 | + courses: ImmutablePropTypes.iterable.isRequired, |
| 108 | + handleSubmit: PropTypes.func.isRequired, |
| 109 | + parsedDate: PropTypes.instanceOf(Date), |
| 110 | + onAssignAssignment: PropTypes.func.isRequired, |
| 111 | + onCloseAssignmentCreator: PropTypes.func.isRequired, |
| 112 | + onDraftAssignment: PropTypes.func.isRequired, |
| 113 | +}; |
| 114 | + |
| 115 | +AssignmentCreatorForm.defaultProps = { |
| 116 | + parsedDate: null, |
| 117 | +}; |
0 commit comments