Add redux actions and reducer for scheduler entries

This commit is contained in:
Ken Hibino
2020-12-02 19:44:50 -08:00
parent fbbc414bdf
commit 3e5b145883
4 changed files with 119 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
import { Dispatch } from "@reduxjs/toolkit";
import { listSchedulerEntries, ListSchedulerEntriesResponse } from "../api";
// List of scheduler-entry related action types.
export const LIST_SCHEDULER_ENTRIES_BEGIN = "LIST_SCHEDULER_ENTRIES_BEGIN";
export const LIST_SCHEDULER_ENTRIES_SUCCESS = "LIST_SCHEDULER_ENTRIES_SUCCESS";
export const LIST_SCHEDULER_ENTRIES_ERROR = "LIST_SCHEDULER_ENTRIES_ERROR";
interface ListSchedulerEntriesBeginAction {
type: typeof LIST_SCHEDULER_ENTRIES_BEGIN;
}
interface ListSchedulerEntriesSuccessAction {
type: typeof LIST_SCHEDULER_ENTRIES_SUCCESS;
payload: ListSchedulerEntriesResponse;
}
interface ListSchedulerEntriesErrorAction {
type: typeof LIST_SCHEDULER_ENTRIES_ERROR;
error: string; // error description
}
// Union of all scheduler-entry related actions.
export type SchedulerEntriesActionTypes =
| ListSchedulerEntriesBeginAction
| ListSchedulerEntriesSuccessAction
| ListSchedulerEntriesErrorAction;
export function listSchedulerEntriesAsync() {
return async (dispatch: Dispatch<SchedulerEntriesActionTypes>) => {
dispatch({ type: LIST_SCHEDULER_ENTRIES_BEGIN });
try {
const response = await listSchedulerEntries();
dispatch({
type: LIST_SCHEDULER_ENTRIES_SUCCESS,
payload: response,
});
} catch (error) {
console.error(error);
dispatch({
type: LIST_SCHEDULER_ENTRIES_ERROR,
error: "Could not retrieve scheduler entries",
});
}
};
}