54 lines
1.7 KiB
TypeScript

import { createAsyncThunk } from '@reduxjs/toolkit';
import { getLeadsApi, getLeadCountListApi, updateLeadApi } from '@api';
import { LeadItem, LeadCountItem } from '@interfaces';
export const getLeads = createAsyncThunk<
LeadItem[],
{ leadId: string | null; staffid: string }
>('leads/getLeads', async (payload, { rejectWithValue }) => {
try {
return await getLeadsApi(payload.leadId, payload.staffid);
} catch (error: any) {
return rejectWithValue(error.message || 'Failed to load leads');
}
});
export const getLeadCountList = createAsyncThunk<
LeadCountItem[],
string
>('leads/getLeadCountList', async (staffid, { rejectWithValue }) => {
try {
return await getLeadCountListApi(staffid);
} catch (error: any) {
return rejectWithValue(error.message || 'Failed to load lead counts');
}
});
export const updateLeadStatus = createAsyncThunk<
{ leadId: string; statusId: string; statusName: string; color: string; message: string },
{ lead: LeadItem; newStatusId: string; newStatusName: string; newColor: string },
{ rejectValue: string }
>(
'leads/updateStatus',
async (payload, { rejectWithValue }) => {
try {
const { lead, newStatusId } = payload;
const response = await updateLeadApi(lead, newStatusId);
if (response.status) {
return {
leadId: lead.id,
statusId: newStatusId,
statusName: payload.newStatusName,
color: payload.newColor,
message: response.message,
};
} else {
return rejectWithValue(response.message || 'Failed to update status');
}
} catch (error: any) {
return rejectWithValue(error.message || 'Failed to update status');
}
},
);