|
| 1 | +// @flow |
| 2 | + |
| 3 | +import { |
| 4 | + default as React, |
| 5 | + type Node, |
| 6 | + type ComponentType, |
| 7 | + useEffect, |
| 8 | +} from "react"; |
| 9 | +import Grid from "@mui/material/Grid"; |
| 10 | +import TextField from "@mui/material/TextField"; |
| 11 | +import { type Person, type StandardValidations } from "./common"; |
| 12 | +import Tags, { type Tag } from "./Tags"; |
| 13 | + |
| 14 | +/* |
| 15 | + * This component collects the metadata required by Digital Commons Data to make a deposit. |
| 16 | + */ |
| 17 | + |
| 18 | +type DigitalCommonsDataRepoArgs = {| |
| 19 | + // To modify the metadata relating to persons call updatePeople, for all |
| 20 | + // other metadata call handleChange. The form fields are uncontrolled so no |
| 21 | + // current value is passed in from the caller; modified values are simply |
| 22 | + // propagated up and the fields initialise to empty. |
| 23 | + updatePeople: (people: Array<Person>) => void, |
| 24 | + handleChange: (event: { |
| 25 | + target: { name: "subject" | "title" | "description", value: string }, |
| 26 | + }) => void, |
| 27 | + |
| 28 | + // Callers of this component should validate the modified values and use |
| 29 | + // this prop to signal as to whether the corresponding field should be in an |
| 30 | + // error state or not. |
| 31 | + inputValidations: StandardValidations, |
| 32 | + |
| 33 | + // Error states will only be shown when this flag is true, which is to say |
| 34 | + // that the user has attempted to submit the form. This means we don't show |
| 35 | + // any error state whilst they are in the process of inputting the metadata. |
| 36 | + submitAttempt: boolean, |
| 37 | + |
| 38 | + title: string, |
| 39 | + description: string, |
| 40 | + |
| 41 | + tags: Array<Tag>, |
| 42 | + onTagsChange: ({ |
| 43 | + target: { |
| 44 | + value: Array<Tag>, |
| 45 | + }, |
| 46 | + }) => void, |
| 47 | + fetchingTags: boolean, |
| 48 | +|}; |
| 49 | + |
| 50 | +function DigitalCommonsDataRepo({ |
| 51 | + handleChange, |
| 52 | + inputValidations, |
| 53 | + submitAttempt, |
| 54 | + updatePeople, |
| 55 | + title, |
| 56 | + description, |
| 57 | + tags, |
| 58 | + onTagsChange, |
| 59 | + fetchingTags, |
| 60 | +}: DigitalCommonsDataRepoArgs): Node { |
| 61 | + /* |
| 62 | + * DigitalCommonsData doesn't require all of the same information as all of the other |
| 63 | + * repositories, but the RSpace backend is set up to require author, |
| 64 | + * contact, and subject metadata. Rather than weaken the validations for |
| 65 | + * all repositories, for DigitalCommonsData we pass these dummy values which are then |
| 66 | + * ignored when the RSpace backend calls the DigitalCommonsData API. |
| 67 | + */ |
| 68 | + useEffect(() => { |
| 69 | + updatePeople([ |
| 70 | + { |
| 71 | + uniqueName: "DUMMY_VALUE", |
| 72 | + email: "DUMMY_VALUE@example.com", |
| 73 | + type: "Author", |
| 74 | + }, |
| 75 | + { |
| 76 | + uniqueName: "DUMMY_VALUE", |
| 77 | + email: "DUMMY_VALUE@example.com", |
| 78 | + type: "Contact", |
| 79 | + }, |
| 80 | + ]); |
| 81 | + handleChange({ |
| 82 | + target: { |
| 83 | + name: "subject", |
| 84 | + value: "DUMMY_VALUE", |
| 85 | + }, |
| 86 | + }); |
| 87 | + }, []); |
| 88 | + |
| 89 | + return ( |
| 90 | + <Grid container style={{ width: "100%" }}> |
| 91 | + <Grid item xs={12}> |
| 92 | + <TextField |
| 93 | + name="title" |
| 94 | + error={submitAttempt && !inputValidations.title} |
| 95 | + label="Title *" |
| 96 | + onChange={({ target: { value } }) => |
| 97 | + handleChange({ target: { name: "title", value } }) |
| 98 | + } |
| 99 | + margin="normal" |
| 100 | + fullWidth |
| 101 | + value={title} |
| 102 | + /> |
| 103 | + </Grid> |
| 104 | + <Grid item xs={12}> |
| 105 | + <TextField |
| 106 | + error={submitAttempt && !inputValidations.description} |
| 107 | + label="Description *" |
| 108 | + name="description" |
| 109 | + multiline |
| 110 | + maxRows="4" |
| 111 | + onChange={({ target: { value } }) => |
| 112 | + handleChange({ target: { name: "description", value } }) |
| 113 | + } |
| 114 | + margin="normal" |
| 115 | + fullWidth |
| 116 | + value={description} |
| 117 | + /> |
| 118 | + </Grid> |
| 119 | + </Grid> |
| 120 | + ); |
| 121 | +} |
| 122 | + |
| 123 | +export default (DigitalCommonsDataRepo: ComponentType<DigitalCommonsDataRepoArgs>); |
0 commit comments