1
- import { apiLogout } from '~constants/api'
1
+ import { API , GET_PATHS , apiLogout } from '~constants/api'
2
2
import { api } from '~constants/mochi'
3
- import { Button , Separator , Typography , useLoginWidget } from '@mochi-ui/core'
3
+ import {
4
+ ActionBar ,
5
+ ActionBarActionGroup ,
6
+ ActionBarBody ,
7
+ ActionBarCancelButton ,
8
+ ActionBarConfirmButton ,
9
+ ActionBarContent ,
10
+ ActionBarDescription ,
11
+ ActionBarIcon ,
12
+ Button ,
13
+ Separator ,
14
+ Typography ,
15
+ toast ,
16
+ useLoginWidget ,
17
+ } from '@mochi-ui/core'
4
18
import { useRouter } from 'next/router'
5
- import React from 'react'
19
+ import React , { useEffect } from 'react'
6
20
import { FormProvider , useForm } from 'react-hook-form'
7
21
import { ROUTES } from '~constants/routes'
8
- import { platformGroupList , targetGroupList } from '~constants/settings'
9
- import { GeneralFormValue } from './types'
22
+ import { useFetchGeneralSettings } from '~hooks/settings/useFetchGeneralSettings'
23
+ import {
24
+ RequestDefaultMessageSetting ,
25
+ RequestTxLimitSetting ,
26
+ RequestUpdateGeneralSettingsPayloadRequest ,
27
+ ResponseGeneralSettingData ,
28
+ ResponseUserGeneralSettingResponse ,
29
+ } from '~types/mochi-schema'
10
30
import { MoneySource } from './MoneySource'
11
31
import { ReceiverPlatform } from './ReceiverPlatform'
12
32
import { TokenPriority } from './TokenPriority'
@@ -16,65 +36,94 @@ import { TransactionPrivacy } from './TransactionPrivacy'
16
36
import { SocialAccountsPrivacy } from './SocialAccountsPrivacy'
17
37
import { WalletsPrivacy } from './WalletsPrivacy'
18
38
39
+ const SETTINGS_GENERAL_FORM_ID = 'settings-general-form'
40
+
19
41
export const GeneralPage = ( ) => {
20
- const form = useForm < GeneralFormValue > ( {
42
+ const form = useForm < ResponseGeneralSettingData > ( {
21
43
mode : 'all' ,
22
- defaultValues : {
23
- defaultMoneySource : 'mochi' ,
24
- defaultReceiverPlatform : 'discord' ,
25
- defaultTokenPriority : [ { id : '941f0fb1-00da-49dc-a538-5e81fc874cb4' } ] ,
26
- transactionPrivacy : {
27
- general_target_group : targetGroupList [ 0 ] . key ,
28
- general_platform_group : platformGroupList [ 0 ] . key ,
29
- custom_settings : [ ] ,
30
- } ,
31
- socialAccountsPrivacy : {
32
- general_target_group : targetGroupList [ 0 ] . key ,
33
- general_platform_group : platformGroupList [ 0 ] . key ,
34
- custom_settings : [ ] ,
35
- } ,
36
- walletsPrivacy : {
37
- general_target_group : targetGroupList [ 0 ] . key ,
38
- general_platform_group : platformGroupList [ 0 ] . key ,
39
- custom_settings : [ ] ,
40
- } ,
41
- } ,
42
44
} )
43
- const { handleSubmit, control, watch } = form
45
+ const {
46
+ handleSubmit,
47
+ reset,
48
+ formState : { isSubmitting, isDirty } ,
49
+ } = form
44
50
45
- const { logout } = useLoginWidget ( )
51
+ const { profile , logout } = useLoginWidget ( )
46
52
const { replace } = useRouter ( )
53
+ const { data : settings } = useFetchGeneralSettings ( profile ?. id )
47
54
48
- const onSubmit = ( data : any ) => {
49
- alert ( JSON . stringify ( data ) )
55
+ const onUpdateSettings = ( data : ResponseGeneralSettingData ) => {
56
+ if ( ! profile ?. id ) return
57
+ const payload : RequestUpdateGeneralSettingsPayloadRequest = {
58
+ payment : {
59
+ default_token : data . payment ?. default_token_id || '' ,
60
+ default_money_source : {
61
+ platform : data . payment ?. default_money_source ?. platform || '' ,
62
+ platform_identifier :
63
+ data . payment ?. default_money_source ?. platform_identifier || '' ,
64
+ } ,
65
+ default_receiver_platform :
66
+ data . payment ?. default_receiver_platform || '' ,
67
+ token_priorities :
68
+ data . payment ?. prioritized_token ?. flatMap ( ( each ) => each . id || [ ] ) ||
69
+ [ ] ,
70
+ default_message_enable : data . payment ?. default_message_enable || true ,
71
+ default_message_settings : data . payment
72
+ ?. default_message_settings as RequestDefaultMessageSetting [ ] ,
73
+ tx_limit_enable : data . payment ?. tx_limit_enable || true ,
74
+ tx_limit_settings : data . payment
75
+ ?. tx_limit_settings as RequestTxLimitSetting [ ] ,
76
+ } ,
77
+ privacy : {
78
+ ...data . privacy ,
79
+ } ,
80
+ }
81
+ return API . MOCHI . put ( payload , GET_PATHS . UPDATE_GENERAL_SETTINGS ( profile . id ) )
82
+ . json ( ( r : ResponseUserGeneralSettingResponse ) => {
83
+ reset ( r . data )
84
+ } )
85
+ . catch ( ( e ) => {
86
+ const err = JSON . parse ( e . message )
87
+ toast ( {
88
+ description : err . msg ,
89
+ scheme : 'danger' ,
90
+ } )
91
+ } )
50
92
}
51
93
52
- return (
53
- < >
54
- < FormProvider { ... form } >
55
- < form onSubmit = { handleSubmit ( onSubmit ) } />
56
- < div className = "space-y-4" >
57
- < Typography level = "h6" > Payment setting </ Typography >
94
+ useEffect ( ( ) => {
95
+ if ( settings ) {
96
+ console . log ( { settings } )
97
+ reset ( settings )
98
+ }
99
+ } , [ reset , settings ] )
58
100
59
- < div className = "flex gap-4" >
60
- < MoneySource control = { control } />
61
- < ReceiverPlatform control = { control } />
62
- </ div >
101
+ return (
102
+ < FormProvider { ...form } >
103
+ < form
104
+ id = { SETTINGS_GENERAL_FORM_ID }
105
+ onSubmit = { handleSubmit ( onUpdateSettings ) }
106
+ />
107
+ < div className = "space-y-4" >
108
+ < Typography level = "h6" > Payment setting</ Typography >
63
109
64
- < TokenPriority { ...{ control, watch } } />
65
- < DefaultMessage { ...{ control, watch } } />
66
- < TransactionLimit { ...{ control, watch } } />
110
+ < div className = "flex gap-4" >
111
+ < MoneySource />
112
+ < ReceiverPlatform />
113
+ </ div >
67
114
68
- < Separator className = "w-full max-w-md" />
115
+ < TokenPriority />
116
+ < DefaultMessage />
117
+ < TransactionLimit />
69
118
70
- < Typography level = "h6" > Privacy </ Typography >
119
+ < Separator className = "w-full max-w-md" / >
71
120
72
- < TransactionPrivacy { ...{ control, watch } } />
73
- < SocialAccountsPrivacy { ...{ control, watch } } />
74
- < WalletsPrivacy { ...{ control, watch } } />
75
- </ div >
76
- </ FormProvider >
121
+ < Typography level = "h6" > Privacy</ Typography >
77
122
123
+ < TransactionPrivacy />
124
+ < SocialAccountsPrivacy />
125
+ < WalletsPrivacy />
126
+ </ div >
78
127
< div className = "mt-8" >
79
128
< Button
80
129
variant = "outline"
@@ -91,6 +140,41 @@ export const GeneralPage = () => {
91
140
Log out
92
141
</ Button >
93
142
</ div >
94
- </ >
143
+ < div className = "sticky bottom-0" >
144
+ < ActionBar open = { isDirty } >
145
+ < ActionBarContent
146
+ scheme = "success"
147
+ outline
148
+ shadow
149
+ onOpenAutoFocus = { ( e ) => e . preventDefault ( ) }
150
+ anchorClassName = "left-0 right-0 -mb-8"
151
+ >
152
+ < ActionBarIcon />
153
+ < ActionBarBody >
154
+ < ActionBarDescription >
155
+ Do you want to save these changes?
156
+ </ ActionBarDescription >
157
+ </ ActionBarBody >
158
+ < ActionBarActionGroup >
159
+ < ActionBarCancelButton
160
+ disabled = { isSubmitting }
161
+ variant = "link"
162
+ onClick = { ( ) => reset ( ) }
163
+ >
164
+ Reset
165
+ </ ActionBarCancelButton >
166
+ < ActionBarConfirmButton
167
+ loading = { isSubmitting }
168
+ type = "submit"
169
+ form = { SETTINGS_GENERAL_FORM_ID }
170
+ className = "min-w-[130px]"
171
+ >
172
+ Save changes
173
+ </ ActionBarConfirmButton >
174
+ </ ActionBarActionGroup >
175
+ </ ActionBarContent >
176
+ </ ActionBar >
177
+ </ div >
178
+ </ FormProvider >
95
179
)
96
180
}
0 commit comments