Skip to content

Commit cc7afc2

Browse files
author
Roshan Jossy
committed
add edit and view mode for bio field
1 parent e304ca3 commit cc7afc2

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

src/components/UserDetails/Bio.tsx

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { useState } from 'react'
2+
import { GoPencil, GrPencil } from '@react-icons/all-files/go/GoPencil'
3+
import { GoCheck } from '@react-icons/all-files/go/GoCheck'
24
import { graphql, useFragment, useMutation } from 'react-relay'
35
import { Bio_user$key } from '../../queries/__generated__/Bio_user.graphql'
46

@@ -18,6 +20,7 @@ const Bio = ({ user }: BioProps) => {
1820
)
1921

2022
const [bio, setBio] = useState(data.bio)
23+
const [editMode, setEditMode] = useState(false)
2124

2225
const [commitMutation, isMutationInFlight] = useMutation(
2326
graphql`
@@ -29,8 +32,10 @@ const Bio = ({ user }: BioProps) => {
2932
}
3033
`
3134
)
32-
const onChange = (evt: any) => setBio(evt.target.value)
35+
const onChange = (evt: React.ChangeEvent<HTMLInputElement>) =>
36+
setBio(evt.target.value)
3337
const onSubmit = () => {
38+
setEditMode(false)
3439
commitMutation({
3540
variables: {
3641
input: { id: data.id, bio: bio },
@@ -39,13 +44,30 @@ const Bio = ({ user }: BioProps) => {
3944
}
4045

4146
return (
42-
<input
43-
onChange={onChange}
44-
onBlur={onSubmit}
45-
className="dark:text-gray-300"
46-
type="text"
47-
defaultValue={data.bio}
48-
/>
47+
<div>
48+
{editMode ? (
49+
<div className="flex justify-between">
50+
<input
51+
onChange={onChange}
52+
onBlur={onSubmit}
53+
className="dark:text-gray-300 dark:bg-dark-600"
54+
type="text"
55+
disabled={isMutationInFlight}
56+
defaultValue={data.bio}
57+
/>
58+
<button onClick={() => setEditMode(false)}>
59+
<GoCheck />
60+
</button>
61+
</div>
62+
) : (
63+
<div className="flex justify-between">
64+
<span>{data.bio}</span>
65+
<button onClick={() => setEditMode(true)}>
66+
<GoPencil />
67+
</button>
68+
</div>
69+
)}
70+
</div>
4971
)
5072
}
5173

0 commit comments

Comments
 (0)