-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathProfileForm.tsx
More file actions
157 lines (146 loc) · 4.54 KB
/
ProfileForm.tsx
File metadata and controls
157 lines (146 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Field, Form, Formik } from 'formik'
import Link from 'next/link'
import { ReactElement, useEffect, useState } from 'react'
import { PROFILE_ROUTE } from '../../constants'
import { UserInfo } from '../../types'
import { doApiRequest, formatResponse } from '../../utils'
import { OBJECT_NAME_PLURAL, OBJECT_NAME_SINGULAR } from '../../utils/branding'
import { Icon } from '../common'
import {
CheckboxField,
FileField,
FormStyle,
SelectField,
TextField,
} from '../FormComponents'
type Props = {
settings: UserInfo
onUpdate: (info: UserInfo) => void
}
const ProfileForm = ({
settings,
onUpdate = () => undefined,
}: Props): ReactElement => {
const [schools, setSchools] = useState([])
const [majors, setMajors] = useState([])
const [errorMessage, setErrorMessage] = useState<
ReactElement | string | null
>(null)
useEffect(() => {
doApiRequest('/schools/?format=json')
.then((resp) => resp.json())
.then(setSchools)
doApiRequest('/majors/?format=json')
.then((resp) => resp.json())
.then(setMajors)
}, [])
function submit(data, { setSubmitting, setStatus }): Promise<void> {
const infoSubmit = () => {
setErrorMessage(null)
if (data.image !== null) {
delete data.image
}
return doApiRequest('/settings/?format=json', {
method: 'PATCH',
body: data,
}).then((resp) => {
if (resp.ok) {
resp.json().then(onUpdate)
} else {
resp.json().then((resp) => {
setStatus(resp)
setErrorMessage(
<div className="has-text-danger mt-3">
{formatResponse(resp)}
</div>,
)
})
}
})
}
if (data.image && data.image instanceof File) {
const formData = new FormData()
formData.append('image', data.image)
return doApiRequest('/settings/?format=json', {
method: 'PATCH',
body: formData,
})
.then(infoSubmit)
.finally(() => setSubmitting(false))
} else {
return infoSubmit().finally(() => setSubmitting(false))
}
}
const affiliations = [
{ value: 0, label: 'Undergraduate Student' },
{ value: 1, label: 'Masters Student' },
{ value: 2, label: 'Professional Student' },
{ value: 3, label: 'Doctoral Student' },
{ value: 4, label: 'Faculty or Staff Member' },
]
return (
<>
<Formik
initialValues={{ ...settings, image: settings.image_url }}
onSubmit={submit}
enableReinitialize
>
{({ dirty, isSubmitting }) => (
<Form>
<FormStyle isHorizontal>
<Field
name="image"
as={FileField}
label="Profile Picture"
isImage
/>
<Field name="graduation_year" as={TextField} type="number" />
<Field
name="affiliation"
as={SelectField}
choices={affiliations}
valueDeserialize={(val) =>
affiliations.find((item) => item.value === val)
}
/>
<Field name="school" as={SelectField} choices={schools} isMulti />
<Field name="major" as={SelectField} choices={majors} isMulti />
<Field
name="share_bookmarks"
as={CheckboxField}
label={`Share my user information with the ${OBJECT_NAME_PLURAL} that I have bookmarked. By default, this information is not visible to ${OBJECT_NAME_SINGULAR} administrators.`}
/>
<Field
name="show_profile"
as={CheckboxField}
label={
<>
Allow my profile page to be visible to the public. You can
view your profile page{' '}
<Link
href={PROFILE_ROUTE()}
as={PROFILE_ROUTE(settings.username)}
>
<a>here</a>
</Link>
.
</>
}
/>
<button
type="submit"
disabled={!dirty || isSubmitting}
className="button is-success"
>
<Icon alt="save" name={dirty ? 'edit' : 'check-circle'} />
{dirty ? 'Save' : 'Saved!'}
</button>
</FormStyle>
</Form>
)}
</Formik>
{errorMessage}
</>
)
}
export default ProfileForm