|
| 1 | +/** |
| 2 | + * Edit Role Form |
| 3 | + * form for eidting details about current role |
| 4 | + */ |
| 5 | +import React, { useEffect, useState } from "react"; |
| 6 | +import PT from "prop-types"; |
| 7 | +import { Form, Field, useField } from "react-final-form"; |
| 8 | +import FormField from "components/FormField"; |
| 9 | +import BaseCreateModal from "../BaseCreateModal"; |
| 10 | +import Button from "components/Button"; |
| 11 | +import MonthPicker from "components/MonthPicker"; |
| 12 | +import InformationTooltip from "components/InformationTooltip"; |
| 13 | +import IconCrossLight from "../../../../assets/images/icon-cross-light.svg"; |
| 14 | +import "./styles.module.scss"; |
| 15 | +import NumberInput from "components/NumberInput"; |
| 16 | +import { |
| 17 | + validator, |
| 18 | + validateExists, |
| 19 | + validateMin, |
| 20 | + composeValidators, |
| 21 | +} from "./utils/validator"; |
| 22 | + |
| 23 | +const Error = ({ name }) => { |
| 24 | + const { |
| 25 | + meta: { dirty, error }, |
| 26 | + } = useField(name, { subscription: { dirty: true, error: true } }); |
| 27 | + return dirty && error ? <span styleName="error">{error}</span> : null; |
| 28 | +}; |
| 29 | + |
| 30 | +function EditRoleForm({ submitForm, role }) { |
| 31 | + const [startMonthVisible, setStartMonthVisible] = useState(false); |
| 32 | + |
| 33 | + return ( |
| 34 | + <Form |
| 35 | + onSubmit={submitForm} |
| 36 | + mutators={{ |
| 37 | + clearField: ([fieldName], state, { changeValue }) => { |
| 38 | + changeValue(state, fieldName, () => undefined); |
| 39 | + }, |
| 40 | + }} |
| 41 | + validate={validator} |
| 42 | + > |
| 43 | + {({ |
| 44 | + handleSubmit, |
| 45 | + hasValidationErrors, |
| 46 | + form: { |
| 47 | + mutators: { clearField }, |
| 48 | + getState, |
| 49 | + }, |
| 50 | + }) => { |
| 51 | + return ( |
| 52 | + <div styleName="modal-body"> |
| 53 | + <table styleName="table"> |
| 54 | + <tr> |
| 55 | + <th># of resources</th> |
| 56 | + <th>Duration (weeks)</th> |
| 57 | + <th>Start month</th> |
| 58 | + </tr> |
| 59 | + <tr styleName="role-row"> |
| 60 | + <td> |
| 61 | + <Field |
| 62 | + validate={composeValidators(validateExists, validateMin(1))} |
| 63 | + name="numberOfResources" |
| 64 | + initialValue={role.numberOfResources} |
| 65 | + > |
| 66 | + {({ input, meta }) => ( |
| 67 | + <NumberInput |
| 68 | + name={input.name} |
| 69 | + value={input.value} |
| 70 | + onChange={input.onChange} |
| 71 | + onBlur={input.onBlur} |
| 72 | + onFocus={input.onFocus} |
| 73 | + min="1" |
| 74 | + error={meta.touched && meta.error} |
| 75 | + /> |
| 76 | + )} |
| 77 | + </Field> |
| 78 | + <Error name="numberOfResources" /> |
| 79 | + </td> |
| 80 | + <td> |
| 81 | + <Field |
| 82 | + validate={composeValidators(validateExists, validateMin(4))} |
| 83 | + name="durationWeeks" |
| 84 | + initialValue={role.durationWeeks} |
| 85 | + > |
| 86 | + {({ input, meta }) => ( |
| 87 | + <NumberInput |
| 88 | + name={input.name} |
| 89 | + value={input.value} |
| 90 | + onChange={input.onChange} |
| 91 | + onBlur={input.onBlur} |
| 92 | + onFocus={input.onFocus} |
| 93 | + min="4" |
| 94 | + error={meta.touched && meta.error} |
| 95 | + /> |
| 96 | + )} |
| 97 | + </Field> |
| 98 | + <Error name="durationWeeks" /> |
| 99 | + </td> |
| 100 | + <td> |
| 101 | + {startMonthVisible ? ( |
| 102 | + <> |
| 103 | + <Field name="startMonth" initialValue={Date.now()}> |
| 104 | + {(props) => ( |
| 105 | + <MonthPicker |
| 106 | + name={props.input.name} |
| 107 | + value={props.input.value} |
| 108 | + onChange={props.input.onChange} |
| 109 | + onBlur={props.input.onBlur} |
| 110 | + onFocus={props.input.onFocus} |
| 111 | + /> |
| 112 | + )} |
| 113 | + </Field> |
| 114 | + <Error name="startMonth" /> |
| 115 | + </> |
| 116 | + ) : ( |
| 117 | + <div styleName="flex-container"> |
| 118 | + <button |
| 119 | + styleName="toggle-button" |
| 120 | + onClick={() => setStartMonthVisible(true)} |
| 121 | + > |
| 122 | + Add Start Month |
| 123 | + </button> |
| 124 | + <InformationTooltip |
| 125 | + iconSize="14px" |
| 126 | + text="Requested start month for this position." |
| 127 | + /> |
| 128 | + </div> |
| 129 | + )} |
| 130 | + </td> |
| 131 | + </tr> |
| 132 | + </table> |
| 133 | + <Button |
| 134 | + type="primary" |
| 135 | + size="medium" |
| 136 | + onClick={handleSubmit} |
| 137 | + disabled={hasValidationErrors} |
| 138 | + > |
| 139 | + Save |
| 140 | + </Button> |
| 141 | + </div> |
| 142 | + ); |
| 143 | + }} |
| 144 | + </Form> |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +EditRoleForm.propTypes = { |
| 149 | + submitForm: PT.func, |
| 150 | + role: PT.object, |
| 151 | +}; |
| 152 | + |
| 153 | +export default EditRoleForm; |
0 commit comments