如果你看看我的额外减速器功能,我已经输入了所有内容。
额外减速功能
export const getMednannyAppointments = createAsyncThunk<
MednannyAppointment[],
string,
{ rejectValue: AppAPIError }
>("mednanny/getAppointments", async (vsnr: string) => {
return await fetchFromAPI<MednannyAppointment[]>(
MEDNANNY_ROUTES.GET_APPOINTMENTS,
"GET",
{
vsnr
}
);
});
切片
type InitialAppointmentState = {
data: MednannyAppointment[];
isFetching: boolean;
error: AppAPIError;
};
const initialAppointmentState: InitialAppointmentState = {
data: null,
isFetching: false,
error: null
};
const appointments = createSlice({
name: "appointments",
initialState: initialAppointmentState,
reducers: {
updateAppointement(state, action: PayloadAction<MednannyAppointment>) {
state.data = state.data.map((appointment) =>
appointment.id === action.payload.id ? action.payload : appointment
);
}
},
extraReducers: (builder) => {
builder.addCase(getMednannyAppointments.fulfilled, (state, action) => {
state.isFetching = false;
state.data = action.payload;
});
builder.addCase(getMednannyAppointments.pending, (state, action) => {
state.isFetching = true;
});
builder.addCase(getMednannyAppointments.rejected, (state, action) => {
state.isFetching = false;
// state.error has a type of WritableDraft<AppApiError> instead of AppApiError
state.error = action.payload;
});
}
});