|
| 1 | +import { Formik } from 'formik'; |
| 2 | +import React, { Component } from 'react'; |
| 3 | +import Loader from 'components/Loader'; |
| 4 | +import InputFormItem from 'components/FormItems/items/InputFormItem'; |
| 5 | +import SwitchFormItem from 'components/FormItems/items/SwitchFormItem'; |
| 6 | +import RadioFormItem from 'components/FormItems/items/RadioFormItem'; |
| 7 | +import ImagesFormItem from 'components/FormItems/items/ImagesFormItem'; |
| 8 | +import usersFields from 'components/Users/usersFields'; |
| 9 | +import IniValues from 'components/FormItems/iniValues'; |
| 10 | +import FormValidations from 'components/FormItems/formValidations'; |
| 11 | + |
| 12 | +class UsersForm extends Component { |
| 13 | + iniValues = () => { |
| 14 | + return IniValues(usersFields, this.props.record || {}); |
| 15 | + } |
| 16 | + |
| 17 | + formValidations = () => { |
| 18 | + return FormValidations(usersFields, this.props.record || {}); |
| 19 | + } |
| 20 | + |
| 21 | + handleSubmit = () => { |
| 22 | + return null; |
| 23 | + }; |
| 24 | + |
| 25 | + passwordSchema = { |
| 26 | + currentPassword: { type: 'string', label: 'Current Password' }, |
| 27 | + newPassword: { type: 'string', label: 'New Password' }, |
| 28 | + confirmNewPassword: { type: 'string', label: 'Confirm new Password' }, |
| 29 | + }; |
| 30 | + |
| 31 | + renderForm() { |
| 32 | + const { saveLoading } = this.props; |
| 33 | + |
| 34 | + return ( |
| 35 | + <Formik |
| 36 | + onSubmit={this.handleSubmit} |
| 37 | + initialValues={this.iniValues()} |
| 38 | + validationSchema={this.formValidations()} |
| 39 | + render={(form) => { |
| 40 | + return ( |
| 41 | + <form onSubmit={form.handleSubmit}> |
| 42 | + |
| 43 | + <InputFormItem |
| 44 | + name={'firstName'} |
| 45 | + schema={usersFields} |
| 46 | + /> |
| 47 | + |
| 48 | + <InputFormItem |
| 49 | + name={'lastName'} |
| 50 | + schema={usersFields} |
| 51 | + /> |
| 52 | + |
| 53 | + <InputFormItem |
| 54 | + name={'phoneNumber'} |
| 55 | + schema={usersFields} |
| 56 | + /> |
| 57 | + |
| 58 | + <InputFormItem |
| 59 | + name={'email'} |
| 60 | + schema={usersFields} |
| 61 | + /> |
| 62 | + |
| 63 | + <InputFormItem |
| 64 | + name={'currentPassword'} |
| 65 | + password |
| 66 | + schema={this.passwordSchema} |
| 67 | + /> |
| 68 | + |
| 69 | + <InputFormItem |
| 70 | + name={'newPassword'} |
| 71 | + schema={this.passwordSchema} |
| 72 | + password |
| 73 | + /> |
| 74 | + |
| 75 | + <InputFormItem |
| 76 | + name={'confirmNewPassword'} |
| 77 | + schema={this.passwordSchema} |
| 78 | + password |
| 79 | + /> |
| 80 | + |
| 81 | + <> |
| 82 | + <RadioFormItem |
| 83 | + name={'role'} |
| 84 | + schema={usersFields} |
| 85 | + /> |
| 86 | + |
| 87 | + <SwitchFormItem |
| 88 | + name={'disabled'} |
| 89 | + schema={usersFields} |
| 90 | + /> |
| 91 | + </> |
| 92 | + |
| 93 | + <ImagesFormItem |
| 94 | + name={'avatar'} |
| 95 | + schema={usersFields} |
| 96 | + path={'users/avatar'} |
| 97 | + fileProps={{ |
| 98 | + size: undefined, |
| 99 | + formats: undefined, |
| 100 | + }} |
| 101 | + max={undefined} |
| 102 | + /> |
| 103 | + |
| 104 | + <div className="form-buttons"> |
| 105 | + <button |
| 106 | + className="btn btn-primary" |
| 107 | + disabled={saveLoading} |
| 108 | + type="button" |
| 109 | + onClick={form.handleSubmit} |
| 110 | + > |
| 111 | + Save |
| 112 | + </button>{' '}{' '} |
| 113 | + |
| 114 | + <button |
| 115 | + className="btn btn-light" |
| 116 | + type="button" |
| 117 | + disabled={saveLoading} |
| 118 | + onClick={form.handleReset} |
| 119 | + > |
| 120 | + Reset |
| 121 | + </button>{' '}{' '} |
| 122 | + </div> |
| 123 | + </form> |
| 124 | + ); |
| 125 | + }} |
| 126 | + /> |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + render() { |
| 131 | + const { isEditing, findLoading, record } = this.props; |
| 132 | + |
| 133 | + if (findLoading) { |
| 134 | + return <Loader />; |
| 135 | + } |
| 136 | + |
| 137 | + if (isEditing && !record) { |
| 138 | + return <Loader />; |
| 139 | + } |
| 140 | + |
| 141 | + return this.renderForm(); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +export default UsersForm; |
0 commit comments