reducers.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { createSlice, PayloadAction } from '@reduxjs/toolkit';
  2. import { dateTime } from '@grafana/data';
  3. import { toStateKey } from 'app/features/variables/utils';
  4. import { FooterMode, Report, ReportsState, ReportTimeRange, SchedulingFrequency, StepKey } from '../../types';
  5. export const getTimezone = () => {
  6. // Older browser does not the internationalization API
  7. if (!(window as any).Intl) {
  8. return '';
  9. }
  10. const dateFormat = (window as any).Intl.DateTimeFormat();
  11. if (!dateFormat.resolvedOptions) {
  12. return '';
  13. }
  14. const options = dateFormat.resolvedOptions();
  15. if (!options.timeZone) {
  16. return '';
  17. }
  18. return options.timeZone;
  19. };
  20. export const defaultTimeRange = {
  21. from: dateTime(null),
  22. to: dateTime(null),
  23. raw: { from: '', to: '' },
  24. };
  25. const blankReport: Report = {
  26. id: 0,
  27. name: '',
  28. recipients: '',
  29. replyTo: '',
  30. message: 'Hi, \nPlease find attached a PDF status report. If you have any questions, feel free to contact me!\nBest,',
  31. dashboardId: undefined,
  32. dashboardName: '',
  33. dashboards: [{ dashboard: undefined, timeRange: defaultTimeRange.raw }],
  34. schedule: {
  35. frequency: SchedulingFrequency.Weekly,
  36. timeZone: getTimezone(),
  37. },
  38. formats: ['pdf'],
  39. options: {
  40. orientation: 'landscape',
  41. layout: 'grid',
  42. timeRange: defaultTimeRange.raw as ReportTimeRange,
  43. },
  44. enableDashboardUrl: true,
  45. };
  46. export const initialState: ReportsState = {
  47. reports: [] as Report[],
  48. report: blankReport,
  49. hasFetchedList: false,
  50. hasFetchedSingle: false,
  51. searchQuery: '',
  52. reportCount: 0,
  53. isLoading: true,
  54. isUpdated: false,
  55. visitedSteps: [],
  56. settings: {
  57. branding: {
  58. reportLogoUrl: '',
  59. emailLogoUrl: '',
  60. emailFooterMode: FooterMode.SentBy,
  61. emailFooterText: '',
  62. emailFooterLink: '',
  63. },
  64. },
  65. };
  66. const reportsSlice = createSlice({
  67. name: 'reports',
  68. initialState,
  69. reducers: {
  70. reportsLoaded: (state: ReportsState, action: PayloadAction<Report[]>): ReportsState => ({
  71. ...state,
  72. reports: action.payload,
  73. reportCount: action.payload.length,
  74. hasFetchedList: true,
  75. }),
  76. reportLoaded: (state: ReportsState, action: PayloadAction<Report>): ReportsState => ({
  77. ...state,
  78. report: action.payload,
  79. hasFetchedSingle: true,
  80. isLoading: false,
  81. }),
  82. updateReportProp: (state: ReportsState, action: PayloadAction<Report>): ReportsState => ({
  83. ...state,
  84. isUpdated: true,
  85. report: action.payload,
  86. }),
  87. clearReportState: (state: ReportsState): ReportsState => ({
  88. ...state,
  89. isUpdated: false,
  90. visitedSteps: [],
  91. report: blankReport,
  92. }),
  93. reportLoadingBegin: (state: ReportsState) => ({
  94. ...state,
  95. isLoading: true,
  96. }),
  97. reportLoadingEnd: (state: ReportsState) => ({
  98. ...state,
  99. isLoading: false,
  100. }),
  101. testEmailSendBegin: (state: ReportsState) => ({
  102. ...state,
  103. testEmailIsSending: true,
  104. }),
  105. testEmailSendEnd: (state: ReportsState) => ({
  106. ...state,
  107. testEmailIsSending: false,
  108. }),
  109. setLastUid: (state: ReportsState, action: PayloadAction<string | undefined>): ReportsState => ({
  110. ...state,
  111. lastUid: toStateKey(action.payload),
  112. }),
  113. addVisitedStep: (state: ReportsState, action: PayloadAction<StepKey[]>) => {
  114. return { ...state, visitedSteps: [...state.visitedSteps, ...action.payload] };
  115. },
  116. },
  117. });
  118. export const {
  119. clearReportState,
  120. reportLoaded,
  121. reportsLoaded,
  122. updateReportProp,
  123. reportLoadingBegin,
  124. reportLoadingEnd,
  125. testEmailSendBegin,
  126. testEmailSendEnd,
  127. setLastUid,
  128. addVisitedStep,
  129. } = reportsSlice.actions;
  130. export const reportsReducers = reportsSlice.reducer;
  131. export default {
  132. reports: reportsReducers,
  133. };