|
| 1 | +/** |
| 2 | + * Edit Role Modal |
| 3 | + * Popup form to enter 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 { validator, validateExists, validateMin, composeValidators } from "./utils/validator"; |
| 17 | + |
| 18 | +const Error = ({ name }) => { |
| 19 | + const { |
| 20 | + meta: { dirty, error }, |
| 21 | + } = useField(name, { subscription: { dirty: true, error: true } }); |
| 22 | + return dirty && error ? <span styleName="error">{error}</span> : null; |
| 23 | +}; |
| 24 | + |
| 25 | +function EditRoleModal({ open, onClose, submitForm, role }) { |
| 26 | + const [startMonthVisible, setStartMonthVisible] = useState(false); |
| 27 | + |
| 28 | + return ( |
| 29 | + <Form |
| 30 | + onSubmit={submitForm} |
| 31 | + mutators={{ |
| 32 | + clearField: ([fieldName], state, { changeValue }) => { |
| 33 | + changeValue(state, fieldName, () => undefined); |
| 34 | + }, |
| 35 | + }} |
| 36 | + validate={validator} |
| 37 | + > |
| 38 | + {({ |
| 39 | + handleSubmit, |
| 40 | + hasValidationErrors, |
| 41 | + form: { |
| 42 | + mutators: { clearField }, |
| 43 | + getState, |
| 44 | + }, |
| 45 | + }) => { |
| 46 | + return ( |
| 47 | + <BaseCreateModal |
| 48 | + open={open} |
| 49 | + onClose={onClose} |
| 50 | + title="Edit Role" |
| 51 | + subtitle="Please provide your team details before submitting a request." |
| 52 | + buttons={ |
| 53 | + <Button |
| 54 | + type="primary" |
| 55 | + size="medium" |
| 56 | + onClick={handleSubmit} |
| 57 | + disabled={hasValidationErrors} |
| 58 | + > |
| 59 | + Submit |
| 60 | + </Button> |
| 61 | + } |
| 62 | + disableFocusTrap |
| 63 | + > |
| 64 | + <div styleName="modal-body"> |
| 65 | + <table styleName="table"> |
| 66 | + <tr> |
| 67 | + <th># of resources</th> |
| 68 | + <th>Duration (weeks)</th> |
| 69 | + <th>Start month</th> |
| 70 | + </tr> |
| 71 | + <tr styleName="role-row"> |
| 72 | + <td> |
| 73 | + <Field |
| 74 | + validate={composeValidators(validateExists, validateMin(1))} |
| 75 | + name="numberOfResources" |
| 76 | + initialValue={role.numberOfResources} |
| 77 | + > |
| 78 | + {({ input, meta }) => ( |
| 79 | + <NumberInput |
| 80 | + name={input.name} |
| 81 | + value={input.value} |
| 82 | + onChange={input.onChange} |
| 83 | + onBlur={input.onBlur} |
| 84 | + onFocus={input.onFocus} |
| 85 | + min="1" |
| 86 | + error={meta.touched && meta.error} |
| 87 | + /> |
| 88 | + )} |
| 89 | + </Field> |
| 90 | + <Error name="numberOfResources" /> |
| 91 | + </td> |
| 92 | + <td> |
| 93 | + <Field |
| 94 | + validate={composeValidators(validateExists, validateMin(4))} |
| 95 | + name="durationWeeks" |
| 96 | + initialValue={role.durationWeeks} |
| 97 | + > |
| 98 | + {({ input, meta }) => ( |
| 99 | + <NumberInput |
| 100 | + name={input.name} |
| 101 | + value={input.value} |
| 102 | + onChange={input.onChange} |
| 103 | + onBlur={input.onBlur} |
| 104 | + onFocus={input.onFocus} |
| 105 | + min="4" |
| 106 | + error={meta.touched && meta.error} |
| 107 | + /> |
| 108 | + )} |
| 109 | + </Field> |
| 110 | + <Error name="durationWeeks" /> |
| 111 | + </td> |
| 112 | + <td> |
| 113 | + {startMonthVisible ? ( |
| 114 | + <> |
| 115 | + <Field |
| 116 | + name="startMonth" |
| 117 | + initialValue={Date.now()} |
| 118 | + > |
| 119 | + {(props) => ( |
| 120 | + <MonthPicker |
| 121 | + name={props.input.name} |
| 122 | + value={props.input.value} |
| 123 | + onChange={props.input.onChange} |
| 124 | + onBlur={props.input.onBlur} |
| 125 | + onFocus={props.input.onFocus} |
| 126 | + /> |
| 127 | + )} |
| 128 | + </Field> |
| 129 | + <Error name="startMonth" /> |
| 130 | + </> |
| 131 | + ) : ( |
| 132 | + <div styleName="flex-container"> |
| 133 | + <button |
| 134 | + styleName="toggle-button" |
| 135 | + onClick={() => |
| 136 | + setStartMonthVisible(true) |
| 137 | + } |
| 138 | + > |
| 139 | + Add Start Month |
| 140 | + </button> |
| 141 | + <InformationTooltip |
| 142 | + iconSize="14px" |
| 143 | + text="Requested start month for this position." |
| 144 | + /> |
| 145 | + </div> |
| 146 | + )} |
| 147 | + </td> |
| 148 | + </tr> |
| 149 | + </table> |
| 150 | + </div> |
| 151 | + </BaseCreateModal> |
| 152 | + ); |
| 153 | + }} |
| 154 | + </Form> |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +EditRoleModal.propTypes = { |
| 159 | + open: PT.bool, |
| 160 | + onClose: PT.func, |
| 161 | + submitForm: PT.func, |
| 162 | + role: PT.object, |
| 163 | +}; |
| 164 | + |
| 165 | +export default EditRoleModal; |
0 commit comments