This repository was archived by the owner on May 5, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,21 @@ export const deleteRecipient = async (id: string): Promise<void> => {
23
23
if ( error ) throw error ;
24
24
} ;
25
25
26
+ export const updateRecipient = async (
27
+ id : string ,
28
+ updates : Partial < CreateRecipientDTO > ,
29
+ ) : Promise < Recipient > => {
30
+ const { data, error } = await supabase
31
+ . from ( 'recipients' )
32
+ . update ( updates )
33
+ . eq ( 'id' , id )
34
+ . single ( ) ;
35
+
36
+ if ( error ) throw error ;
37
+
38
+ return data as Recipient ;
39
+ } ;
40
+
26
41
export const findRecipientById = async ( id : number ) : Promise < string | null > => {
27
42
const { data, error } = await supabase
28
43
. from ( 'recipients' )
Original file line number Diff line number Diff line change @@ -47,6 +47,20 @@ export const deleteRecipient = createAsyncThunk(
47
47
} ,
48
48
) ;
49
49
50
+ export const updateRecipient = createAsyncThunk (
51
+ 'recipients/updateRecipient' ,
52
+ async (
53
+ { id, updates } : { id : string ; updates : Partial < CreateRecipientDTO > } ,
54
+ { rejectWithValue } ,
55
+ ) => {
56
+ try {
57
+ return await recipientService . updateRecipient ( id , updates ) ;
58
+ } catch ( error ) {
59
+ return rejectWithValue ( ( error as Error ) . message ) ;
60
+ }
61
+ } ,
62
+ ) ;
63
+
50
64
const recipientSlice = createSlice ( {
51
65
name : 'recipients' ,
52
66
initialState,
@@ -81,7 +95,27 @@ const recipientSlice = createSlice({
81
95
( recipient ) => recipient . id !== action . payload ,
82
96
) ;
83
97
} ,
84
- ) ;
98
+ )
99
+ . addCase ( updateRecipient . pending , ( state ) => {
100
+ state . loading = true ;
101
+ state . error = null ;
102
+ } )
103
+ . addCase (
104
+ updateRecipient . fulfilled ,
105
+ ( state , action : PayloadAction < Recipient > ) => {
106
+ state . loading = false ;
107
+ const index = state . recipients . findIndex (
108
+ ( recipient ) => recipient . id === action . payload . id ,
109
+ ) ;
110
+ if ( index !== - 1 ) {
111
+ state . recipients [ index ] = action . payload ;
112
+ }
113
+ } ,
114
+ )
115
+ . addCase ( updateRecipient . rejected , ( state , action ) => {
116
+ state . loading = false ;
117
+ state . error = action . payload as string ;
118
+ } ) ;
85
119
} ,
86
120
} ) ;
87
121
You can’t perform that action at this time.
0 commit comments