@@ -22,20 +22,19 @@ import {
2222} from '@/models/notifications'
2323import { RootState , AppDispatch } from '@/store/store'
2424import { connect } from 'react-redux'
25- import { updateNotificationSettings } from '@/store/userSlice'
25+ import { updateNotificationSettings , setNotificationSettingsDraft } from '@/store/userSlice'
2626
2727type NotificationSettingProps = {
28- initialType : NotificationType
29- initialOptions : NotificationTriggerOptions
28+ draftType : NotificationType
29+ draftOptions : NotificationTriggerOptions
3030
31+ setNotificationSettingsDraft : ( provider : NotificationType , triggers : NotificationTriggerOptions ) => void
3132 updateNotificationSettings : ( type : NotificationType , options : NotificationTriggerOptions ) => Promise < any >
3233}
3334
3435interface NotificationSettingState {
3536 saved : boolean
36- type : NotificationType
3737 error : string
38- options : NotificationTriggerOptions
3938}
4039
4140class NotificationSettingsImpl extends React . Component <
@@ -48,8 +47,6 @@ class NotificationSettingsImpl extends React.Component<
4847 this . state = {
4948 saved : true ,
5049 error : '' ,
51- type : props . initialType ,
52- options : props . initialOptions ,
5350 }
5451 }
5552
@@ -60,9 +57,9 @@ class NotificationSettingsImpl extends React.Component<
6057 const provider = option as NotificationProvider
6158 const type = getDefaultTypeForProvider ( provider )
6259
60+ this . props . setNotificationSettingsDraft ( type , this . props . draftOptions )
6361 this . setState ( {
6462 saved : false ,
65- type,
6663 } )
6764 }
6865
@@ -71,56 +68,65 @@ class NotificationSettingsImpl extends React.Component<
7168 option : SelectValue < string , false > ,
7269 ) => {
7370 const method = option as WebhookMethod
74- const type = this . state . type as NotificationTypeWebhook
71+ const type = this . props . draftType as NotificationTypeWebhook
7572
76- this . setState ( {
77- saved : false ,
78- type : {
73+ this . props . setNotificationSettingsDraft (
74+ {
7975 ...type ,
8076 method,
8177 } ,
78+ this . props . draftOptions
79+ )
80+ this . setState ( {
81+ saved : false ,
8282 } )
8383 }
8484
8585 private onURLChanged = ( e : React . ChangeEvent < HTMLInputElement > ) => {
8686 const url = e . target . value
8787
88- const type = this . state . type as
88+ const type = this . props . draftType as
8989 | NotificationTypeWebhook
9090 | NotificationTypeGotify
9191
92- this . setState ( {
93- saved : false ,
94- type : {
92+ this . props . setNotificationSettingsDraft (
93+ {
9594 ...type ,
9695 url,
9796 } ,
97+ this . props . draftOptions
98+ )
99+ this . setState ( {
100+ saved : false ,
98101 } )
99102 }
100103
101104 private onGotifyTokenChanged = ( e : React . ChangeEvent < HTMLInputElement > ) => {
102105 const token = e . target . value
103106
104- const type = this . state . type as NotificationTypeGotify
107+ const type = this . props . draftType as NotificationTypeGotify
105108
106- this . setState ( {
107- saved : false ,
108- type : {
109+ this . props . setNotificationSettingsDraft (
110+ {
109111 ...type ,
110112 token,
111113 } ,
114+ this . props . draftOptions
115+ )
116+ this . setState ( {
117+ saved : false ,
112118 } )
113119 }
114120
115121 private onTriggersChanged = ( options : NotificationTriggerOptions ) => {
122+ this . props . setNotificationSettingsDraft ( this . props . draftType , options )
116123 this . setState ( {
117124 saved : false ,
118- options,
119125 } )
120126 }
121127
122128 private onSaveClicked = async ( ) => {
123- const { type, options } = this . state
129+ const { draftType : type , draftOptions : options } = this . props
124130 if ( type . provider === 'webhook' ) {
125131 const isValidURL = type . url . match ( / ^ h t t p s ? : \/ \/ [ ^ \s / $ . ? # ] .[ ^ \s ] * $ / i)
126132 if ( ! isValidURL ) {
@@ -152,7 +158,8 @@ class NotificationSettingsImpl extends React.Component<
152158 }
153159
154160 render ( ) : React . ReactNode {
155- const { error, type, options, saved } = this . state
161+ const { error, saved } = this . state
162+ const { draftType : type , draftOptions : options } = this . props
156163
157164 const placeholderURL =
158165 type . provider === 'webhook'
@@ -254,15 +261,15 @@ class NotificationSettingsImpl extends React.Component<
254261}
255262
256263const mapStateToProps = ( state : RootState ) => {
257- const userNotifications = state . user . profile . notifications
258-
259264 return {
260- initialType : userNotifications . provider ,
261- initialOptions : userNotifications . triggers ,
265+ draftType : state . user . draftNotificationSettings . provider ,
266+ draftOptions : state . user . draftNotificationSettings . triggers ,
262267 }
263268}
264269
265270const mapDispatchToProps = ( dispatch : AppDispatch ) => ( {
271+ setNotificationSettingsDraft : ( provider : NotificationType , triggers : NotificationTriggerOptions ) =>
272+ dispatch ( setNotificationSettingsDraft ( { provider, triggers } ) ) ,
266273 updateNotificationSettings : ( type : NotificationType , options : NotificationTriggerOptions ) =>
267274 dispatch ( updateNotificationSettings ( { type, options } ) ) ,
268275} )
0 commit comments