2020-12-27 05:43:51 +08:00
|
|
|
import uniqBy from "lodash.uniqby";
|
2020-12-03 11:44:50 +08:00
|
|
|
import {
|
2020-12-27 02:29:43 +08:00
|
|
|
LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN,
|
|
|
|
LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR,
|
|
|
|
LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS,
|
2020-12-03 11:44:50 +08:00
|
|
|
LIST_SCHEDULER_ENTRIES_BEGIN,
|
|
|
|
LIST_SCHEDULER_ENTRIES_ERROR,
|
|
|
|
LIST_SCHEDULER_ENTRIES_SUCCESS,
|
|
|
|
SchedulerEntriesActionTypes,
|
|
|
|
} from "../actions/schedulerEntriesActions";
|
2020-12-27 02:29:43 +08:00
|
|
|
import { SchedulerEnqueueEvent, SchedulerEntry } from "../api";
|
2020-12-03 11:44:50 +08:00
|
|
|
|
|
|
|
interface SchedulerEntriesState {
|
|
|
|
loading: boolean;
|
|
|
|
data: SchedulerEntry[];
|
|
|
|
error: string; // error description
|
2020-12-27 02:29:43 +08:00
|
|
|
enqueueEventsByEntryId: {
|
|
|
|
[entryId: string]: { data: SchedulerEnqueueEvent[]; loading: boolean };
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-27 05:43:51 +08:00
|
|
|
export function getEnqueueEventsEntry(
|
2020-12-27 02:29:43 +08:00
|
|
|
state: SchedulerEntriesState,
|
|
|
|
entryId: string
|
|
|
|
): { data: SchedulerEnqueueEvent[]; loading: boolean } {
|
|
|
|
return state.enqueueEventsByEntryId[entryId] || { data: [], loading: false };
|
2020-12-03 11:44:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const initialState: SchedulerEntriesState = {
|
|
|
|
loading: false,
|
|
|
|
data: [],
|
|
|
|
error: "",
|
2020-12-27 02:29:43 +08:00
|
|
|
enqueueEventsByEntryId: {},
|
2020-12-03 11:44:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function schedulerEntriesReducer(
|
|
|
|
state = initialState,
|
|
|
|
action: SchedulerEntriesActionTypes
|
|
|
|
): SchedulerEntriesState {
|
|
|
|
switch (action.type) {
|
|
|
|
case LIST_SCHEDULER_ENTRIES_BEGIN:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: true,
|
|
|
|
};
|
|
|
|
case LIST_SCHEDULER_ENTRIES_SUCCESS:
|
|
|
|
return {
|
2020-12-27 02:29:43 +08:00
|
|
|
...state,
|
2020-12-03 11:44:50 +08:00
|
|
|
error: "",
|
|
|
|
loading: false,
|
|
|
|
data: action.payload.entries,
|
|
|
|
};
|
|
|
|
case LIST_SCHEDULER_ENTRIES_ERROR:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loading: false,
|
|
|
|
error: action.error,
|
|
|
|
};
|
2020-12-27 02:29:43 +08:00
|
|
|
case LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN: {
|
|
|
|
const entry = getEnqueueEventsEntry(state, action.entryId);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
enqueueEventsByEntryId: {
|
|
|
|
...state.enqueueEventsByEntryId,
|
|
|
|
[action.entryId]: {
|
|
|
|
...entry,
|
|
|
|
loading: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS: {
|
2020-12-27 05:43:51 +08:00
|
|
|
const sortByEnqueuedAt = (
|
|
|
|
e1: SchedulerEnqueueEvent,
|
|
|
|
e2: SchedulerEnqueueEvent
|
|
|
|
): number => {
|
|
|
|
return Date.parse(e2.enqueued_at) - Date.parse(e1.enqueued_at);
|
|
|
|
};
|
2020-12-27 02:29:43 +08:00
|
|
|
const entry = getEnqueueEventsEntry(state, action.entryId);
|
2020-12-27 05:43:51 +08:00
|
|
|
const newData = uniqBy(
|
|
|
|
[...entry.data, ...action.payload.events],
|
|
|
|
"task_id"
|
|
|
|
).sort(sortByEnqueuedAt);
|
2020-12-27 02:29:43 +08:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
enqueueEventsByEntryId: {
|
|
|
|
...state.enqueueEventsByEntryId,
|
|
|
|
[action.entryId]: {
|
|
|
|
loading: false,
|
2020-12-27 05:43:51 +08:00
|
|
|
data: newData,
|
2020-12-27 02:29:43 +08:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR: {
|
|
|
|
const entry = getEnqueueEventsEntry(state, action.entryId);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
enqueueEventsByEntryId: {
|
|
|
|
...state.enqueueEventsByEntryId,
|
|
|
|
[action.entryId]: {
|
|
|
|
...entry,
|
|
|
|
loading: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2020-12-03 11:44:50 +08:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default schedulerEntriesReducer;
|