github.com/wfusion/gofusion@v1.1.14/common/infra/asynq/asynqmon/ui/src/reducers/schedulerEntriesReducer.ts (about) 1 import uniqBy from "lodash.uniqby"; 2 import { 3 LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN, 4 LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR, 5 LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS, 6 LIST_SCHEDULER_ENTRIES_BEGIN, 7 LIST_SCHEDULER_ENTRIES_ERROR, 8 LIST_SCHEDULER_ENTRIES_SUCCESS, 9 SchedulerEntriesActionTypes, 10 } from "../actions/schedulerEntriesActions"; 11 import { SchedulerEnqueueEvent, SchedulerEntry } from "../api"; 12 13 interface SchedulerEntriesState { 14 loading: boolean; 15 data: SchedulerEntry[]; 16 error: string; // error description 17 enqueueEventsByEntryId: { 18 [entryId: string]: { data: SchedulerEnqueueEvent[]; loading: boolean }; 19 }; 20 } 21 22 export function getEnqueueEventsEntry( 23 state: SchedulerEntriesState, 24 entryId: string 25 ): { data: SchedulerEnqueueEvent[]; loading: boolean } { 26 return state.enqueueEventsByEntryId[entryId] || { data: [], loading: false }; 27 } 28 29 const initialState: SchedulerEntriesState = { 30 loading: false, 31 data: [], 32 error: "", 33 enqueueEventsByEntryId: {}, 34 }; 35 36 function schedulerEntriesReducer( 37 state = initialState, 38 action: SchedulerEntriesActionTypes 39 ): SchedulerEntriesState { 40 switch (action.type) { 41 case LIST_SCHEDULER_ENTRIES_BEGIN: 42 return { 43 ...state, 44 loading: true, 45 }; 46 case LIST_SCHEDULER_ENTRIES_SUCCESS: 47 return { 48 ...state, 49 error: "", 50 loading: false, 51 data: action.payload.entries, 52 }; 53 case LIST_SCHEDULER_ENTRIES_ERROR: 54 return { 55 ...state, 56 loading: false, 57 error: action.error, 58 }; 59 case LIST_SCHEDULER_ENQUEUE_EVENTS_BEGIN: { 60 const entry = getEnqueueEventsEntry(state, action.entryId); 61 return { 62 ...state, 63 enqueueEventsByEntryId: { 64 ...state.enqueueEventsByEntryId, 65 [action.entryId]: { 66 ...entry, 67 loading: true, 68 }, 69 }, 70 }; 71 } 72 case LIST_SCHEDULER_ENQUEUE_EVENTS_SUCCESS: { 73 const sortByEnqueuedAt = ( 74 e1: SchedulerEnqueueEvent, 75 e2: SchedulerEnqueueEvent 76 ): number => { 77 return Date.parse(e2.enqueued_at) - Date.parse(e1.enqueued_at); 78 }; 79 const entry = getEnqueueEventsEntry(state, action.entryId); 80 const newData = uniqBy( 81 [...entry.data, ...action.payload.events], 82 "task_id" 83 ).sort(sortByEnqueuedAt); 84 return { 85 ...state, 86 enqueueEventsByEntryId: { 87 ...state.enqueueEventsByEntryId, 88 [action.entryId]: { 89 loading: false, 90 data: newData, 91 }, 92 }, 93 }; 94 } 95 case LIST_SCHEDULER_ENQUEUE_EVENTS_ERROR: { 96 const entry = getEnqueueEventsEntry(state, action.entryId); 97 return { 98 ...state, 99 enqueueEventsByEntryId: { 100 ...state.enqueueEventsByEntryId, 101 [action.entryId]: { 102 ...entry, 103 loading: false, 104 }, 105 }, 106 }; 107 } 108 default: 109 return state; 110 } 111 } 112 113 export default schedulerEntriesReducer;