123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import { createSlice, PayloadAction } from '@reduxjs/toolkit';
- import { dateTime } from '@grafana/data';
- import { toStateKey } from 'app/features/variables/utils';
- import { FooterMode, Report, ReportsState, ReportTimeRange, SchedulingFrequency, StepKey } from '../../types';
- export const getTimezone = () => {
- // Older browser does not the internationalization API
- if (!(window as any).Intl) {
- return '';
- }
- const dateFormat = (window as any).Intl.DateTimeFormat();
- if (!dateFormat.resolvedOptions) {
- return '';
- }
- const options = dateFormat.resolvedOptions();
- if (!options.timeZone) {
- return '';
- }
- return options.timeZone;
- };
- export const defaultTimeRange = {
- from: dateTime(null),
- to: dateTime(null),
- raw: { from: '', to: '' },
- };
- const blankReport: Report = {
- id: 0,
- name: '',
- recipients: '',
- replyTo: '',
- message: 'Hi, \nPlease find attached a PDF status report. If you have any questions, feel free to contact me!\nBest,',
- dashboardId: undefined,
- dashboardName: '',
- dashboards: [{ dashboard: undefined, timeRange: defaultTimeRange.raw }],
- schedule: {
- frequency: SchedulingFrequency.Weekly,
- timeZone: getTimezone(),
- },
- formats: ['pdf'],
- options: {
- orientation: 'landscape',
- layout: 'grid',
- timeRange: defaultTimeRange.raw as ReportTimeRange,
- },
- enableDashboardUrl: true,
- };
- export const initialState: ReportsState = {
- reports: [] as Report[],
- report: blankReport,
- hasFetchedList: false,
- hasFetchedSingle: false,
- searchQuery: '',
- reportCount: 0,
- isLoading: true,
- isUpdated: false,
- visitedSteps: [],
- settings: {
- branding: {
- reportLogoUrl: '',
- emailLogoUrl: '',
- emailFooterMode: FooterMode.SentBy,
- emailFooterText: '',
- emailFooterLink: '',
- },
- },
- };
- const reportsSlice = createSlice({
- name: 'reports',
- initialState,
- reducers: {
- reportsLoaded: (state: ReportsState, action: PayloadAction<Report[]>): ReportsState => ({
- ...state,
- reports: action.payload,
- reportCount: action.payload.length,
- hasFetchedList: true,
- }),
- reportLoaded: (state: ReportsState, action: PayloadAction<Report>): ReportsState => ({
- ...state,
- report: action.payload,
- hasFetchedSingle: true,
- isLoading: false,
- }),
- updateReportProp: (state: ReportsState, action: PayloadAction<Report>): ReportsState => ({
- ...state,
- isUpdated: true,
- report: action.payload,
- }),
- clearReportState: (state: ReportsState): ReportsState => ({
- ...state,
- isUpdated: false,
- visitedSteps: [],
- report: blankReport,
- }),
- reportLoadingBegin: (state: ReportsState) => ({
- ...state,
- isLoading: true,
- }),
- reportLoadingEnd: (state: ReportsState) => ({
- ...state,
- isLoading: false,
- }),
- testEmailSendBegin: (state: ReportsState) => ({
- ...state,
- testEmailIsSending: true,
- }),
- testEmailSendEnd: (state: ReportsState) => ({
- ...state,
- testEmailIsSending: false,
- }),
- setLastUid: (state: ReportsState, action: PayloadAction<string | undefined>): ReportsState => ({
- ...state,
- lastUid: toStateKey(action.payload),
- }),
- addVisitedStep: (state: ReportsState, action: PayloadAction<StepKey[]>) => {
- return { ...state, visitedSteps: [...state.visitedSteps, ...action.payload] };
- },
- },
- });
- export const {
- clearReportState,
- reportLoaded,
- reportsLoaded,
- updateReportProp,
- reportLoadingBegin,
- reportLoadingEnd,
- testEmailSendBegin,
- testEmailSendEnd,
- setLastUid,
- addVisitedStep,
- } = reportsSlice.actions;
- export const reportsReducers = reportsSlice.reducer;
- export default {
- reports: reportsReducers,
- };
|