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> => {
2323 if ( error ) throw error ;
2424} ;
2525
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+
2641export const findRecipientById = async ( id : number ) : Promise < string | null > => {
2742 const { data, error } = await supabase
2843 . from ( 'recipients' )
Original file line number Diff line number Diff line change @@ -47,6 +47,20 @@ export const deleteRecipient = createAsyncThunk(
4747 } ,
4848) ;
4949
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+
5064const recipientSlice = createSlice ( {
5165 name : 'recipients' ,
5266 initialState,
@@ -81,7 +95,27 @@ const recipientSlice = createSlice({
8195 ( recipient ) => recipient . id !== action . payload ,
8296 ) ;
8397 } ,
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+ } ) ;
85119 } ,
86120} ) ;
87121
You can’t perform that action at this time.
0 commit comments