|
| 1 | +import React, { useState } from 'react'; |
| 2 | + |
| 3 | +import { bool, func, number, shape, string } from 'prop-types'; |
| 4 | + |
| 5 | +import OrganizationSelector from '../containers/OrganizationSelector'; |
| 6 | +import UserSelector from '../containers/UserSelector'; |
| 7 | + |
| 8 | +const onNumericChange = (currentValue, setter) => (e) => { |
| 9 | + const parsed = parseInt(e.target.value, 10); |
| 10 | + isNaN(parsed) ? setter(currentValue) : setter(parsed); |
| 11 | +}; |
| 12 | + |
| 13 | +const PdfForm = ({ onSubmit, defaultValues = {} }) => { |
| 14 | + const [organizationNameMatch, setOrganizationNameMatch] = useState( |
| 15 | + defaultValues.organization && { |
| 16 | + label: defaultValues.organization.name, |
| 17 | + value: defaultValues.organization.uuid, |
| 18 | + } |
| 19 | + ); |
| 20 | + const [organization, setOrganization] = useState( |
| 21 | + defaultValues.organization && defaultValues.organization.uuid |
| 22 | + ); |
| 23 | + const [userNameMatch, setUserNameMatch] = useState( |
| 24 | + defaultValues.user && { |
| 25 | + label: defaultValues.user.name, |
| 26 | + value: defaultValues.user.uuid, |
| 27 | + } |
| 28 | + ); |
| 29 | + const [user, setUser] = useState( |
| 30 | + defaultValues.user && defaultValues.user.uuid |
| 31 | + ); |
| 32 | + const [year, setYear] = useState(defaultValues.year); |
| 33 | + const [url, setUrl] = useState(defaultValues.url); |
| 34 | + const [currentPage, setCurrentPage] = useState(defaultValues.currentPage); |
| 35 | + const [done, setDone] = useState(defaultValues.done); |
| 36 | + |
| 37 | + const handleSubmit = (e) => { |
| 38 | + e.preventDefault(); |
| 39 | + onSubmit({ |
| 40 | + organization, |
| 41 | + user, |
| 42 | + url, |
| 43 | + done: !!done, |
| 44 | + year, |
| 45 | + current_page: currentPage, |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + const onYearChange = onNumericChange(year, setYear); |
| 50 | + const onCurrentPageChange = onNumericChange(currentPage, setCurrentPage); |
| 51 | + |
| 52 | + return ( |
| 53 | + <form onSubmit={handleSubmit}> |
| 54 | + <div className="form-group"> |
| 55 | + <label htmlFor="organization">Organization</label> |
| 56 | + <OrganizationSelector |
| 57 | + value={organizationNameMatch} |
| 58 | + setValue={setOrganizationNameMatch} |
| 59 | + onOrgSelected={setOrganization} |
| 60 | + /> |
| 61 | + </div> |
| 62 | + |
| 63 | + <div className="form-group"> |
| 64 | + <label htmlFor="user">User</label> |
| 65 | + <UserSelector |
| 66 | + value={userNameMatch} |
| 67 | + setValue={setUserNameMatch} |
| 68 | + onUserSelected={setUser} |
| 69 | + /> |
| 70 | + </div> |
| 71 | + |
| 72 | + <div className="form-group"> |
| 73 | + <label htmlFor="year">Year</label> |
| 74 | + <input |
| 75 | + value={year} |
| 76 | + onChange={onYearChange} |
| 77 | + name="year" |
| 78 | + type="number" |
| 79 | + min={1900} |
| 80 | + max={new Date().getUTCFullYear() + 1} |
| 81 | + /> |
| 82 | + </div> |
| 83 | + |
| 84 | + <div className="form-group"> |
| 85 | + <label htmlFor="url">Url</label> |
| 86 | + <input |
| 87 | + value={url} |
| 88 | + onChange={(e) => setUrl(e.target.value)} |
| 89 | + name="url" |
| 90 | + type="url" |
| 91 | + /> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div className="form-group"> |
| 95 | + <label htmlFor="currentPage">Current Page</label> |
| 96 | + <input |
| 97 | + type="number" |
| 98 | + min={1} |
| 99 | + value={currentPage} |
| 100 | + name="currentPage" |
| 101 | + onChange={onCurrentPageChange} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div className="form-group"> |
| 106 | + <label htmlFor="done">Done?</label> |
| 107 | + <input |
| 108 | + type="checkbox" |
| 109 | + checked={!!done} |
| 110 | + name="done" |
| 111 | + onChange={(e) => setDone(e.target.checked)} |
| 112 | + /> |
| 113 | + </div> |
| 114 | + |
| 115 | + <button type="submit">{defaultValues.uuid ? 'Update' : 'Add'} PDF</button> |
| 116 | + </form> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +PdfForm.propTypes = { |
| 121 | + onSubmit: func.isRequired, |
| 122 | + defaultValues: shape({ |
| 123 | + organization: shape({ |
| 124 | + uuid: string, |
| 125 | + name: string, |
| 126 | + }), |
| 127 | + user: shape({ |
| 128 | + uuid: string, |
| 129 | + name: string, |
| 130 | + }), |
| 131 | + url: string, |
| 132 | + done: bool, |
| 133 | + year: number, |
| 134 | + currentPage: number, |
| 135 | + }), |
| 136 | +}; |
| 137 | + |
| 138 | +PdfForm.defaultProps = { |
| 139 | + defaultValues: { |
| 140 | + url: '', |
| 141 | + done: false, |
| 142 | + year: new Date().getUTCFullYear(), |
| 143 | + currentPage: 1, |
| 144 | + }, |
| 145 | +}; |
| 146 | + |
| 147 | +export default PdfForm; |
0 commit comments