1
1
'use client'
2
2
3
- import { useState } from 'react'
3
+ import { useEffect , useState } from 'react'
4
4
import Checkbox from '@/components/layouts/Forms/Checkbox'
5
5
import MarginClamp from '@/components/layouts/Layouts/MarginClamp'
6
6
import DropZone from '@/components/Modals/DropZone'
@@ -9,6 +9,9 @@ import { Group, GroupContainer, MarginGutter } from '@mav/ui/components/layouts'
9
9
import { InputField , Textarea } from '@mav/ui/components/fields'
10
10
import { SelectField } from '@/components/layouts/Forms'
11
11
import { furrySpeciesOptions , pronounOptions } from '@/utils/constants'
12
+ import { Button } from '@mav/ui/components/buttons/Button'
13
+ import { updateCharacter } from '@/utils/api'
14
+ import { redirect } from 'next/navigation'
12
15
13
16
export default function EditCharacter ( { character } : { character : Character } ) {
14
17
const [ avatarUrl , setAvatarUrl ] = useState < string > ( character . avatarUrl )
@@ -20,6 +23,49 @@ export default function EditCharacter({ character }: { character: Character }) {
20
23
const [ species , setSpecies ] = useState < string > ( character . species ?? '' )
21
24
const [ bio , setBio ] = useState < string > ( character . attributes . bio ?? '' )
22
25
26
+ const [ isDirty , setIsDirty ] = useState ( false )
27
+ const [ isSaving , setIsSaving ] = useState ( false )
28
+
29
+ useEffect ( ( ) => {
30
+ const changed =
31
+ displayName !== ( character . name ?? "" ) ||
32
+ nickname !== ( character . nickname ?? "" ) ||
33
+ isMainCharacter !== character . mainCharacter ||
34
+ characterUrl !== ( character . slug ?? "" ) ||
35
+ pronouns !== ( character . attributes . pronouns ?? "" ) ||
36
+ species !== ( character . species ?? "" ) ||
37
+ bio !== ( character . attributes . bio ?? "" )
38
+ setIsDirty ( changed )
39
+ } , [ displayName , nickname , isMainCharacter , characterUrl , pronouns , species , bio ] )
40
+
41
+ const handleSave = async ( ) => {
42
+ setIsSaving ( true )
43
+ try {
44
+ const attributes = {
45
+ pronouns,
46
+ bio,
47
+ gender : character . attributes . gender ?? "" ,
48
+ preferences : character . attributes . preferences ?? { likes : "" , dislikes : "" } ,
49
+ custom_fields : character . attributes . custom_fields ?? [ ] ,
50
+ }
51
+ await updateCharacter ( character . id , {
52
+ name : displayName ,
53
+ nickname,
54
+ mainCharacter : isMainCharacter ,
55
+ slug : characterUrl ,
56
+ attributes,
57
+ species,
58
+ avatarUrl
59
+ } )
60
+ setIsDirty ( false )
61
+ redirect ( '/studio/characters' )
62
+ } catch ( error ) {
63
+ console . error ( "Failed to save profile settings" , error )
64
+ } finally {
65
+ setIsSaving ( false )
66
+ }
67
+ }
68
+
23
69
return (
24
70
< MarginGutter screenSize = "xl" className = "px-6 py-5 *:mt-6 *:gap-6 first:*:mt-0" >
25
71
< GroupContainer >
@@ -82,7 +128,33 @@ export default function EditCharacter({ character }: { character: Character }) {
82
128
/>
83
129
</ div >
84
130
</ Group >
131
+ < Group title = 'Reference sheets' description = "Reference sheets will appear in the list if you have this character linked and be shown on the public profile. Learn more" >
132
+ < div className = 'flex flex-row gap-x-2' >
133
+ < Button
134
+ variant = "primary"
135
+ onClick = { ( ) => { } }
136
+ >
137
+ Add Reference Sheet
138
+ </ Button >
139
+ < Button
140
+ variant = "secondary"
141
+ onClick = { ( ) => { } }
142
+ >
143
+ Manage Reference Sheet
144
+ </ Button >
145
+ </ div >
146
+ </ Group >
85
147
</ GroupContainer >
148
+ { isDirty && (
149
+ < div className = "flex justify-end mt-4" >
150
+ < Button
151
+ onClick = { handleSave }
152
+ disabled = { isSaving }
153
+ >
154
+ { isSaving ? "Saving..." : "Save Changes" }
155
+ </ Button >
156
+ </ div >
157
+ ) }
86
158
</ MarginGutter >
87
159
)
88
160
}
0 commit comments