import {createSlice} from "@reduxjs/toolkit"; export type Job = { id: string // Unique ID description: string // Description of job finished: string logs: Array<{ // All logs added to the job severity?: string, // | title: string, // | description: string, // | }> oId: string // Associated org outputMsg: string // Final Job message product: string // institutes/providesr started: string // date? status: string // Current processing state title: string // Title shown on list updated: string // Date? viewedBy: Array invokedBy: string // What user invoked? invokerEmail: string // Email of invoker. }; type InitialJobState = { values: { [jobId: string]: Job } } const initialState: InitialJobState = { values: {}, }; export const JobsSlice = createSlice({ name: "jobs", initialState, reducers: { setJobs: (state, action) => { state.values = action.payload; }, addJob: (state, action) => { const {job, jobId} = action.payload; console.log("j", job); const newJobs = {...state.values}; newJobs[jobId] = job; state.values = Object.fromEntries(Object.entries(newJobs).sort(([, a], [, b]) => b.started > a.started ? 1 : -1)); }, setMarkRead: (state, action) => { console.log("Mark read", action.payload.data); state.values[action.payload.jobId].viewedBy.push(action.payload.userId); }, setJobStatus: (state, action) => { state.values[action.payload.jobId].status = action.payload.status; state.values[action.payload.jobId].updated = (new Date()).toISOString(); }, updateJob: (state, action) => { state.values[action.payload.jobId] = action.payload.data; }, }, }); export const {setJobs, addJob, setMarkRead, setJobStatus, updateJob} = JobsSlice.actions; export default JobsSlice.reducer;