59 lines
1.5 KiB
TypeScript
59 lines
1.5 KiB
TypeScript
import { createAsyncThunk } from '@reduxjs/toolkit';
|
|
import {
|
|
DeliveryAction,
|
|
RespondToOfferResponse,
|
|
PendingOfferResponse,
|
|
} from '@interfaces';
|
|
import { changeOrderStatus, getPendingOffer, respondToOffer } from '@api';
|
|
|
|
/**
|
|
* Respond to a delivery offer (ACCEPT / REJECT).
|
|
*/
|
|
export const respondToOfferThunk = createAsyncThunk<
|
|
RespondToOfferResponse,
|
|
{ deliveryId: string; action: DeliveryAction }
|
|
>(
|
|
'delivery/respondToOffer',
|
|
async ({ deliveryId, action }, { rejectWithValue }) => {
|
|
try {
|
|
return await respondToOffer(deliveryId, action);
|
|
} catch (error: any) {
|
|
return rejectWithValue(
|
|
error.message || 'Failed to respond to delivery offer',
|
|
);
|
|
}
|
|
},
|
|
);
|
|
|
|
/**
|
|
* Check for any pending delivery offer (e.g. on app launch / reconnect).
|
|
*/
|
|
export const checkPendingOfferThunk = createAsyncThunk<PendingOfferResponse>(
|
|
'delivery/checkPendingOffer',
|
|
async (_, { rejectWithValue }) => {
|
|
try {
|
|
return await getPendingOffer();
|
|
} catch (error: any) {
|
|
return rejectWithValue(error.message || 'Failed to check pending offers');
|
|
}
|
|
},
|
|
);
|
|
|
|
export const changeOrderStatusThunk = createAsyncThunk(
|
|
'delivery/changeOrderStatus',
|
|
async (
|
|
{
|
|
deliveryId,
|
|
action,
|
|
proofKey,
|
|
}: { deliveryId: string; action: string; proofKey?: string },
|
|
{ rejectWithValue },
|
|
) => {
|
|
try {
|
|
return await changeOrderStatus(deliveryId, action, proofKey);
|
|
} catch (error: any) {
|
|
return rejectWithValue(error.message || 'Failed to change order status');
|
|
}
|
|
},
|
|
);
|