program.js 1.73 KiB
import pick from "lodash/pick";
import { createAsyncAction, errorResult, successResult } from "pullstate";
import fetch from "unfetch";
import { ProgramStore } from "stores";
export const loadProgram = createAsyncAction(
async () => {
try {
const resp = await fetch(`${process.env.REACT_APP_API_BASE_URL}/program`);
const mappings = await resp.json();
return successResult(mappings);
} catch (err) {
return errorResult([], err.toString());
}
},
{
postActionHook: ({ result }) => {
if (!result.error) {
const entries = result.payload
.map(
/**
*
* @param {any} entry
* @returns {CF2021.ProgramScheduleEntry}
*/
(entry) => {
return {
...pick(entry, [
"id",
"number",
"title",
"description",
"proposer",
]),
expectedStartAt: new Date(entry.expectedStartAt),
expectedFinishAt: entry.expectedFinishAt
? new Date(entry.expectedFinishAt)
: undefined,
};
}
)
.sort((a, b) => a.expectedStartAt - b.expectedStartAt);
const currentEntry = result.payload.find((entry) => entry.is_live);
ProgramStore.update((state) => {
state.schedule = entries;
if (currentEntry) {
state.current = state.schedule.find(
(scheduleEntry) => scheduleEntry.id === currentEntry.id
);
} else {
// TODO: for testing only
state.current = state.schedule[1];
}
});
}
},
}
);