UNPKG

@adaptabletools/adaptable

Version:

Powerful AG Grid extension which provides advanced, cutting-edge functionality to meet all DataGrid requirements

150 lines (149 loc) 6.12 kB
import { EMPTY_ARRAY } from '../../Utilities/Constants/GeneralConstants'; import AdaptableHelper from '../../Utilities/Helpers/AdaptableHelper'; import { ensureExportStateSchedulesUuids, normalizeScheduledReportSchedule, replaceScheduledReportScheduleInList, } from '../../Utilities/Helpers/Scheduling/ScheduledReportHelper'; import { changeIsSuspendInList } from './utils'; export const REPORT_SELECT = 'REPORT_SELECT'; export const REPORT_ADD = 'REPORT_ADD'; export const REPORT_EDIT = 'REPORT_EDIT'; export const REPORT_DELETE = 'REPORT_DELETE'; export const EXPORT_READY = 'EXPORT_READY'; export const EXPORT_ENSURE_SCHEDULE_UUIDS = 'EXPORT_ENSURE_SCHEDULE_UUIDS'; export const FORMAT_SELECT = 'FORMAT_SELECT'; export const SCHEDULED_REPORT_ADD = 'SCHEDULED_REPORT_ADD'; export const SCHEDULED_REPORT_EDIT = 'SCHEDULED_REPORT_EDIT'; export const SCHEDULED_REPORT_DELETE = 'SCHEDULED_REPORT_DELETE'; export const SCHEDULED_REPORT_SUSPEND = 'SCHEDULED_REPORT_SUSPEND'; export const SCHEDULED_REPORT_UNSUSPEND = 'SCHEDULED_REPORT_UNSUSPEND'; export const SCHEDULED_REPORT_JOB_RUN = 'SCHEDULED_REPORT_JOB_RUN'; export const ReportSelect = (SelectedReport) => ({ type: REPORT_SELECT, SelectedReport, }); export const ReportAdd = (report) => ({ type: REPORT_ADD, report, }); export const ReportEdit = (report) => ({ type: REPORT_EDIT, report, }); export const ReportDelete = (report) => ({ type: REPORT_DELETE, report, }); export const ExportReady = (exportState) => ({ type: EXPORT_READY, exportState, }); export const ExportEnsureScheduleUuids = () => ({ type: EXPORT_ENSURE_SCHEDULE_UUIDS, }); export const FormatSelect = (SelectedFormat) => ({ type: FORMAT_SELECT, SelectedFormat, }); export const ScheduledReportAdd = (reportSchedule) => ({ type: SCHEDULED_REPORT_ADD, reportSchedule, }); export const ScheduledReportEdit = (reportSchedule) => ({ type: SCHEDULED_REPORT_EDIT, reportSchedule, }); export const ScheduledReportDelete = (reportSchedule) => ({ type: SCHEDULED_REPORT_DELETE, reportSchedule, }); export const ScheduledReportSuspend = (reportSchedule) => ({ type: SCHEDULED_REPORT_SUSPEND, reportSchedule, }); export const ScheduledReportUnSuspend = (reportSchedule) => ({ type: SCHEDULED_REPORT_UNSUSPEND, reportSchedule, }); export const ScheduledReportJobRun = (reportSchedule) => ({ type: SCHEDULED_REPORT_JOB_RUN, reportSchedule, }); function updateScheduledReports(state, updater) { return { ...state, ReportSchedules: updater(state.ReportSchedules ?? []), }; } const initialState = { Reports: EMPTY_ARRAY, ReportSchedules: EMPTY_ARRAY, }; export const ExportReducer = (state = initialState, action) => { let reports; switch (action.type) { case REPORT_SELECT: return Object.assign({}, state, { CurrentReport: action.SelectedReport, }); case REPORT_ADD: { const actionReport = action.report; AdaptableHelper.addAdaptableObjectPrimitives(actionReport); reports = [].concat(state.Reports); reports.push(actionReport); return { ...state, Reports: reports }; } case REPORT_EDIT: const actionReport = action.report; return { ...state, Reports: state.Reports.map((abObject) => abObject.Uuid === actionReport.Uuid ? actionReport : abObject), }; case REPORT_DELETE: { const deletedReport = action.report; return { ...state, Reports: state.Reports.filter((abObject) => abObject.Uuid !== deletedReport.Uuid), ReportSchedules: (state.ReportSchedules ?? []).filter((schedule) => schedule.ReportName !== deletedReport.Name), }; } case FORMAT_SELECT: return Object.assign({}, state, { CurrentFormat: action.SelectedFormat, }); case EXPORT_READY: return ensureExportStateSchedulesUuids(action.exportState ?? state); case EXPORT_ENSURE_SCHEDULE_UUIDS: return ensureExportStateSchedulesUuids(state); case SCHEDULED_REPORT_ADD: { const { reportSchedule } = action; const ensuredState = ensureExportStateSchedulesUuids(state); const normalized = normalizeScheduledReportSchedule(reportSchedule); AdaptableHelper.addAdaptableObjectPrimitives(normalized); return updateScheduledReports(ensuredState, (schedules) => [...schedules, normalized]); } case SCHEDULED_REPORT_EDIT: { const { reportSchedule } = action; const ensuredState = ensureExportStateSchedulesUuids(state); const normalized = normalizeScheduledReportSchedule(reportSchedule); AdaptableHelper.addAdaptableObjectPrimitives(normalized); return updateScheduledReports(ensuredState, (schedules) => replaceScheduledReportScheduleInList(schedules, normalized)); } case SCHEDULED_REPORT_DELETE: { const { reportSchedule } = action; const ensuredState = ensureExportStateSchedulesUuids(state); return updateScheduledReports(ensuredState, (schedules) => schedules.filter((s) => s.Uuid !== reportSchedule.Uuid)); } case SCHEDULED_REPORT_SUSPEND: { const { reportSchedule } = action; const ensuredState = ensureExportStateSchedulesUuids(state); return updateScheduledReports(ensuredState, (schedules) => changeIsSuspendInList(reportSchedule, schedules, true)); } case SCHEDULED_REPORT_UNSUSPEND: { const { reportSchedule } = action; const ensuredState = ensureExportStateSchedulesUuids(state); return updateScheduledReports(ensuredState, (schedules) => changeIsSuspendInList(reportSchedule, schedules, false)); } case SCHEDULED_REPORT_JOB_RUN: return state; default: return state; } };