Newer
Older
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
import NavBar from "@/components/navigation/NavBar";
import { User } from "@/types/user";
import { PhotoIcon, UserCircleIcon } from "@heroicons/react/24/outline";
import Router from "next/router";
import { FormEvent, useEffect, useState } from "react";
const Profile = () => {
const [user, setUser] = useState<undefined | User>(undefined)
const fetchUser = async () => {
const endpoint = `${process.env.NEXT_PUBLIC_USER_SERVICE_URL}api/user/${sessionStorage.getItem('username')}`
const response = await fetch(endpoint)
return await response.json()
}
useEffect(() => {
if(!user){
fetchUser().then(res => {
const {_id, username, email, profile, firstName, lastName } = res
setUser({id: _id, email, username, profile, firstName, lastName})
})
}
})
const handleItemChange = (item: string, value: string) => {
if(user){
const updated = {...user, ...{[item]: value}}
setUser(updated)
}
}
const handleSubmit = async (event: FormEvent) => {
const JSONdata = JSON.stringify({
username: user?.username,
email: user?.email,
profile: user?.profile,
firstName: user?.firstName,
lastName: user?.lastName
})
const endpoint = `${process.env.NEXT_PUBLIC_USER_SERVICE_URL}api/updateuser`
const options = {
method: 'PUT',
headers: {
'Authorization': `Bearer ${sessionStorage.getItem("token")}`,
'Content-Type': 'application/json',
},
body: JSONdata,
}
const response = await fetch(endpoint, options)
const result = await response.json()
}
return (
<div className="w-full h-screen flex flex-col">
<div className="w-full">
<NavBar user={user} />
</div>
<form className="max-w-4xl mx-auto pt-10 p-3" onSubmit={(e) => handleSubmit(e)}>
<div className="h-16"></div>
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
<div className="border-b border-gray-900/10 pb-12">
<h2 className="text-base font-semibold leading-7 text-gray-900">Profile</h2>
<p className="mt-1 text-sm leading-6 text-gray-600">
This information will be displayed publicly so be careful what you share.
</p>
<div className="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div className="sm:col-span-3">
<label htmlFor="first-name" className="block text-sm font-medium leading-6 text-gray-900">
First name
</label>
<div className="mt-2">
<input
type="text"
name="first-name"
id="first-name"
autoComplete="given-name"
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
value={user?.firstName}
onChange={(e) => handleItemChange("firstName", e.target.value)}
/>
</div>
</div>
<div className="sm:col-span-3">
<label htmlFor="last-name" className="block text-sm font-medium leading-6 text-gray-900">
Last name
</label>
<div className="mt-2">
<input
type="text"
name="last-name"
id="last-name"
autoComplete="family-name"
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
value={user?.lastName}
onChange={(e) => handleItemChange("lastName", e.target.value)}
/>
</div>
</div>
<div className="sm:col-span-4">
<label htmlFor="username" className="block text-sm font-medium leading-6 text-gray-900">
Username
</label>
<div className="mt-2">
<div className="flex bg-gray-100 rounded-md shadow-sm ring-1 ring-inset ring-gray-300 focus-within:ring-2 focus-within:ring-inset focus-within:ring-indigo-600 sm:max-w-md">
<span className="flex select-none items-center pl-3 text-gray-500 sm:text-sm">dialy.com/</span>
<input
type="text"
name="username"
id="username"
autoComplete="username"
className="block flex-1 border-0 bg-transparent py-1.5 pl-0 text-gray-900 placeholder:text-gray-400 focus:ring-0 sm:text-sm sm:leading-6"
placeholder="janesmith"
value={user?.username}
onChange={(e) => handleItemChange("username", e.target.value)}
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/>
</div>
</div>
</div>
<div className="col-span-full">
<label htmlFor="photo" className="block text-sm font-medium leading-6 text-gray-900">
Photo
</label>
<div className="mt-2 flex items-center gap-x-3">
<UserCircleIcon className="h-12 w-12 text-gray-300" aria-hidden="true" />
<button
type="button"
className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
>
Change
</button>
</div>
</div>
</div>
</div>
<div className="border-b border-gray-900/10 pb-12">
<h2 className="text-base font-semibold leading-7 text-gray-900">Personal Information</h2>
<p className="mt-1 text-sm leading-6 text-gray-600">This won't be shared with anyone.</p>
<div className="mt-10 grid grid-cols-1 gap-x-6 gap-y-8 sm:grid-cols-6">
<div className="sm:col-span-4">
<label htmlFor="email" className="block text-sm font-medium leading-6 text-gray-900">
Email address
</label>
<div className="mt-2">
<input
id="email"
name="email"
type="email"
autoComplete="email"
className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
value={user?.email}
onChange={(e) => handleItemChange("email", e.target.value)}
/>
</div>
</div>
</div>
</div>
</div>
<div className="mt-6 flex items-center justify-end gap-x-6">
<button type="button" className="text-sm font-semibold leading-6 text-gray-900" onClick={() => {Router.push("/feed")}}>
Cancel
</button>
<button
type="submit"
className="rounded-md bg-c-pink px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-c-green focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
>
Save
</button>
</div>
</form>
</div>
)
}
export default Profile;