Skip to content
This repository was archived by the owner on May 5, 2025. It is now read-only.

Commit 92801f6

Browse files
committed
update: add service function for update recipient
1 parent c6a6f98 commit 92801f6

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/features/recipients/recipientService.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
2641
export const findRecipientById = async (id: number): Promise<string | null> => {
2742
const { data, error } = await supabase
2843
.from('recipients')

src/features/recipients/recipientSlice.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff 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+
5064
const 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

0 commit comments

Comments
 (0)