Skip to content

Commit 2a49096

Browse files
authored
Merge pull request #666 from mapswipe/refactor-new-project-basic-info
refactor(manager-dashboard): create reusable BasicProjectInformationForm component
2 parents 6062309 + f7ec0cf commit 2a49096

File tree

2 files changed

+210
-170
lines changed

2 files changed

+210
-170
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
import React from 'react';
2+
import { EntriesAsList, ObjectError, SetBaseValueArg } from '@togglecorp/toggle-form';
3+
import TextInput from '#components/TextInput';
4+
import { generateProjectName, PartialProjectFormType } from '#views/NewProject/utils';
5+
import { labelSelector, valueSelector } from '#utils/common';
6+
import NumberInput from '#components/NumberInput';
7+
import TextArea from '#components/TextArea';
8+
import ImageInput from '#components/ImageInput';
9+
import SelectInput from '#components/SelectInput';
10+
import useProjectOptions from '#views/NewProject/useProjectOptions';
11+
import styles from '#views/NewProject/styles.css';
12+
13+
export interface Props<T extends PartialProjectFormType> {
14+
className?: string;
15+
submissionPending: boolean;
16+
value: T;
17+
setValue: (value: SetBaseValueArg<T>, doNotReset?: boolean) => void;
18+
setFieldValue: (...entries: EntriesAsList<T>) => void;
19+
error: ObjectError<T> | undefined;
20+
}
21+
22+
function BasicProjectInfoForm(props: Props<PartialProjectFormType>) {
23+
const {
24+
submissionPending,
25+
value,
26+
setValue,
27+
setFieldValue,
28+
error,
29+
} = props;
30+
31+
const {
32+
teamOptions,
33+
tutorialOptions,
34+
teamsPending,
35+
tutorialsPending,
36+
organisationOptions,
37+
organisationsPending,
38+
} = useProjectOptions(value?.projectType);
39+
40+
React.useEffect(() => {
41+
setFieldValue(tutorialOptions?.[0]?.value, 'tutorialId');
42+
}, [setFieldValue, value?.projectType, tutorialOptions]);
43+
44+
const setFieldValueAndGenerateName = React.useCallback(
45+
(...entries: EntriesAsList<PartialProjectFormType>) => {
46+
// NOTE: we need to use setFieldValue to set error on change
47+
setFieldValue(...entries);
48+
49+
setValue((oldValue) => {
50+
const name = generateProjectName(
51+
oldValue.projectTopic,
52+
oldValue.projectNumber,
53+
oldValue.projectRegion,
54+
oldValue.requestingOrganisation,
55+
);
56+
return {
57+
...oldValue,
58+
name,
59+
};
60+
}, true);
61+
},
62+
[setFieldValue, setValue],
63+
);
64+
return (
65+
66+
<>
67+
<div className={styles.inputGroup}>
68+
<TextInput
69+
name={'projectTopic' as const}
70+
value={value?.projectTopic}
71+
onChange={setFieldValueAndGenerateName}
72+
error={error?.projectTopic}
73+
label="Project Topic"
74+
hint="Enter the topic of your project (50 char max)."
75+
disabled={submissionPending}
76+
autoFocus
77+
/>
78+
<TextInput
79+
name={'projectRegion' as const}
80+
value={value?.projectRegion}
81+
onChange={setFieldValueAndGenerateName}
82+
label="Project Region"
83+
hint="Enter name of your project Region (50 chars max)"
84+
error={error?.projectRegion}
85+
disabled={submissionPending}
86+
/>
87+
</div>
88+
<div className={styles.inputGroup}>
89+
<NumberInput
90+
name={'projectNumber' as const}
91+
value={value?.projectNumber}
92+
onChange={setFieldValueAndGenerateName}
93+
label="Project Number"
94+
hint="Is this project part of a bigger campaign with multiple projects?"
95+
error={error?.projectNumber}
96+
disabled={submissionPending}
97+
/>
98+
<SelectInput
99+
name={'requestingOrganisation' as const}
100+
value={value?.requestingOrganisation}
101+
options={organisationOptions}
102+
onChange={setFieldValueAndGenerateName}
103+
error={error?.requestingOrganisation}
104+
label="Requesting Organisation"
105+
hint="Which group, institution or community is requesting this project?"
106+
disabled={submissionPending || organisationsPending}
107+
keySelector={valueSelector}
108+
labelSelector={labelSelector}
109+
/>
110+
</div>
111+
<TextArea
112+
name={'name' as const}
113+
value={value?.name}
114+
label="Name"
115+
hint="We will generate you project name based on your inputs above."
116+
readOnly
117+
placeholder="[Project Topic] - [Project Region] ([Task Number]) [Requesting Organisation]"
118+
// error={error?.name}
119+
disabled={submissionPending}
120+
/>
121+
<div className={styles.inputGroup}>
122+
<SelectInput
123+
name={'visibility' as const}
124+
value={value?.visibility}
125+
onChange={setFieldValue}
126+
keySelector={valueSelector}
127+
labelSelector={labelSelector}
128+
options={teamOptions}
129+
label="Visibility"
130+
hint="Choose either 'public' or select the team for which this project should be displayed"
131+
error={error?.visibility}
132+
disabled={submissionPending || teamsPending}
133+
/>
134+
<TextInput
135+
name={'lookFor' as const}
136+
value={value?.lookFor}
137+
onChange={setFieldValue}
138+
error={error?.lookFor}
139+
label="Look For"
140+
hint="What should the users look for (e.g. buildings, cars, trees)? (25 chars max)"
141+
disabled={submissionPending}
142+
/>
143+
</div>
144+
<TextArea
145+
name={'projectDetails' as const}
146+
value={value?.projectDetails}
147+
onChange={setFieldValue}
148+
error={error?.projectDetails}
149+
label="Project Details"
150+
hint="Enter the description for your project. (markdown syntax is supported)."
151+
disabled={submissionPending}
152+
rows={4}
153+
/>
154+
<div className={styles.inputGroup}>
155+
<ImageInput
156+
name={'projectImage' as const}
157+
value={value?.projectImage}
158+
onChange={setFieldValue}
159+
label="Upload Project Image (Image)"
160+
hint="Make sure you have the rights to use the image. It should end with .jpg or .png."
161+
showPreview
162+
error={error?.projectImage}
163+
disabled={submissionPending}
164+
/>
165+
<div className={styles.verticalInputGroup}>
166+
<SelectInput
167+
label="Tutorial"
168+
hint="Choose which tutorial should be used for this project. Make sure that this aligns with what you are looking for."
169+
name={'tutorialId' as const}
170+
value={value?.tutorialId}
171+
onChange={setFieldValue}
172+
options={tutorialOptions}
173+
error={error?.tutorialId}
174+
keySelector={valueSelector}
175+
labelSelector={labelSelector}
176+
disabled={submissionPending || tutorialsPending}
177+
/>
178+
<NumberInput
179+
name={'verificationNumber' as const}
180+
value={value?.verificationNumber}
181+
onChange={setFieldValue}
182+
label="Verification Number"
183+
hint="How many people do you want to see every tile before you consider it finished? (default is 3 - more is recommended for harder tasks, but this will also make project take longer)"
184+
error={error?.verificationNumber}
185+
disabled={submissionPending}
186+
/>
187+
<NumberInput
188+
name={'groupSize' as const}
189+
value={value?.groupSize}
190+
onChange={setFieldValue}
191+
label="Group Size"
192+
hint="How big should a mapping session be? Group size refers to the number of tasks per mapping session."
193+
error={error?.groupSize}
194+
disabled={submissionPending}
195+
/>
196+
</div>
197+
</div>
198+
</>
199+
);
200+
}
201+
202+
export default BasicProjectInfoForm;

0 commit comments

Comments
 (0)