1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | 3x 16x 26x 16x 5x 5x 5x 2x 12x 12x 3x 3x 28x 1x 1x 1x 1x 1x 2x 1x 1x 1x 1x 1x 3x 18x 18x 2x 3x 3x 5x 3x 3x 5x 1x 3x 3x 3x 3x 5x 2x 3x | import moment from 'moment'; import { actionTypes as asyncDataFetchActionTypes } from '@bufferapp/async-data-fetch'; import keyWrapper from '@bufferapp/keywrapper'; export const actionTypes = keyWrapper('DATE_PICKER', { OPEN_DATE_PICKER: 'OPEN_DATE_PICKER', CLOSE_DATE_PICKER: 'CLOSE_DATE_PICKER', SET_MONTH: 'SET_MONTH', SET_DATE_RANGE: 'SET_DATE_RANGE', SET_START_DATE: 'SET_START_DATE', CLEAR_START_DATE: 'CLEAR_START_DATE', SET_END_DATE: 'SET_END_DATE', CLEAR_END_DATE: 'CLEAR_END_DATE', OPEN_CALENDAR: 'OPEN_CALENDAR', CLOSE_CALENDAR: 'CLOSE_CALENDAR', }); function getDayFromTimestamp(timestamp) { return moment.unix(timestamp).startOf('day'); } function formatDay(momentDay) { return momentDay.format('MM/DD/YYYY'); } function formatTimestamp(timestamp) { return formatDay(getDayFromTimestamp(timestamp)); } function calculateDateRange(range) { // We need to enfoce the start of the day to make sure // that the range is not effected by the time of the day const startDate = formatDay(moment().startOf('day').subtract(range, 'days')); const endDate = formatDay(moment().startOf('day').subtract(1, 'days')); return { startDate, endDate }; } export function updatePresets(presets, selectedPreset) { return { presets: presets.map((preset) => { preset.selected = selectedPreset ? preset.range === selectedPreset.range : preset.name === 'Custom'; return preset; }) }; } export const presets = [ { name: '90 Days', label: 'Past 90 Days', range: 90, selected: false, disabled: false, }, { name: '30 Days', label: 'Past 30 Days', range: 30, selected: false, disabled: false, }, { name: '28 Days', label: 'Past 28 Days', range: 28, selected: false, disabled: false, }, { name: '7 Days', label: 'Past 7 Days', range: 7, selected: true, disabled: false, }, { name: 'Yesterday', label: 'Yesterday', range: 1, selected: false, disabled: false, }, { name: 'Custom', label: 'Custom', range: Infinity, selected: false, disabled: false, }, ]; const initialState = { loading: true, open: false, calendarOpen: false, minDate: null, maxDate: moment().valueOf(), month: moment().startOf('month').unix(), ...calculateDateRange(7), presets, }; export default (state = initialState, action) => { switch (action.type) { case actionTypes.SET_MONTH: return { ...state, month: action.date, }; case actionTypes.CLEAR_START_DATE: return { ...state, startDate: null, }; case actionTypes.SET_START_DATE: return { ...state, startDate: action.date, }; case actionTypes.CLEAR_END_DATE: return { ...state, endDate: null, }; case actionTypes.SET_END_DATE: return { ...state, endDate: action.date, }; case actionTypes.SET_DATE_RANGE: return { ...state, ...updatePresets(state.presets, action.preset), startDate: action.startDate, endDate: action.endDate, previousStartDate: null, previousEndDate: null, }; case `get_report_${asyncDataFetchActionTypes.FETCH_START}`: return { ...state, loading: true, }; case `get_report_${asyncDataFetchActionTypes.FETCH_FAIL}`: return { ...initialState, loading: false, hasError: true, }; case `get_report_${asyncDataFetchActionTypes.FETCH_SUCCESS}`: return { ...state, loading: false, }; case `analytics_start_date_${asyncDataFetchActionTypes.FETCH_START}`: return { ...state, loading: true, }; case `analytics_start_date_${asyncDataFetchActionTypes.FETCH_FAIL}`: return { ...state, loading: false, }; case `analytics_start_date_${asyncDataFetchActionTypes.FETCH_SUCCESS}`: return { ...state, loading: false, minDate: action.result, presets: state.presets.map((preset) => { preset.disabled = action.result > moment().subtract(preset.range, 'days'); return preset; }), }; case actionTypes.OPEN_DATE_PICKER: return { ...state, open: true, }; case actionTypes.CLOSE_DATE_PICKER: return { ...state, open: false, calendarOpen: false, startDate: state.previousStartDate ? state.previousStartDate : state.startDate, endDate: state.previousEndDate ? state.previousEndDate : state.endDate, }; case actionTypes.OPEN_CALENDAR: return { ...state, calendarOpen: true, previousStartDate: state.startDate, previousEndDate: state.endDate, startDate: null, endDate: null, }; default: return state; } }; export const actions = { open: () => ({ type: actionTypes.OPEN_DATE_PICKER, }), close: () => ({ type: actionTypes.CLOSE_DATE_PICKER, }), openCalendar: () => ({ type: actionTypes.OPEN_CALENDAR, }), setStartDate: date => ({ type: actionTypes.SET_START_DATE, date: formatTimestamp(date), }), clearStartDate: () => ({ type: actionTypes.CLEAR_START_DATE, }), setEndDate: date => ({ type: actionTypes.SET_END_DATE, date: formatTimestamp(date), }), clearEndDate: () => ({ type: actionTypes.CLEAR_END_DATE, }), setDateRange: (startDate, endDate) => ({ type: actionTypes.SET_DATE_RANGE, startDate: formatTimestamp(startDate), endDate: formatTimestamp(endDate), }), setDatePreset: preset => ({ type: actionTypes.SET_DATE_RANGE, preset, ...calculateDateRange(preset.range), }), setMonth: date => ({ type: actionTypes.SET_MONTH, date, }), setCurrentTab: tabId => ({ type: actionTypes.SET_CURRENT_TAB, tabId, }), setDateRangeAndPreset: ({ preset, startDate, endDate }) => ({ type: actionTypes.SET_DATE_RANGE, startDate, endDate, preset, }), }; |