diff --git a/src/components/Charts/Bar.spec.tsx b/src/components/Charts/Bar.spec.tsx index 6880d2f3940f15a12b246c4095b613acc581b038..9e48e45460184c2d2775a8bcf858e56165bd8f43 100644 --- a/src/components/Charts/Bar.spec.tsx +++ b/src/components/Charts/Bar.spec.tsx @@ -6,7 +6,7 @@ import { DateTime } from 'luxon' import React from 'react' import * as reactRedux from 'react-redux' import { Provider } from 'react-redux' -import * as chartActions from 'store/chart/chart.actions' +import * as chartActions from 'store/chart/chart.slice' import { graphData } from '../../../tests/__mocks__/chartData.mock' import { createMockEcolyoStore } from '../../../tests/__mocks__/store' import Bar from './Bar' @@ -39,7 +39,7 @@ const useDispatchSpy = jest.spyOn(reactRedux, 'useDispatch') const setSelectedDateSpy = jest.spyOn(chartActions, 'setSelectedDate') const setCurrentDatachartIndexSpy = jest.spyOn( chartActions, - 'setCurrentDatachartIndex' + 'setCurrentDataChartIndex' ) describe('Bar component test', () => { diff --git a/src/components/Charts/Bar.tsx b/src/components/Charts/Bar.tsx index 498d72b1200941a2cbef29bf5ccd5a36c68c80d9..435eb671ec5ecff95e351d3063794889b47adb58 100644 --- a/src/components/Charts/Bar.tsx +++ b/src/components/Charts/Bar.tsx @@ -9,9 +9,9 @@ import { useDispatch, useSelector } from 'react-redux' import DateChartService from 'services/dateChart.service' import { AppActionsTypes, AppStore } from 'store' import { - setCurrentDatachartIndex, + setCurrentDataChartIndex, setSelectedDate, -} from 'store/chart/chart.actions' +} from 'store/chart/chart.slice' interface BarProps { index: number @@ -177,7 +177,7 @@ const Bar = ({ useEffect(() => { if (isSelectedDate && !isDuel) { setClicked(true) - dispatch(setCurrentDatachartIndex(index)) + dispatch(setCurrentDataChartIndex(index)) } }, [dispatch, isSelectedDate, isDuel, index]) diff --git a/src/components/ConsumptionVisualizer/InfoDataConsumptionVisualizer.tsx b/src/components/ConsumptionVisualizer/InfoDataConsumptionVisualizer.tsx index b4524da551ebc4fafe33f835a41b6b3b131ed7ae..758a187ff9a7a57a9fc747fa71e834c559c23924 100644 --- a/src/components/ConsumptionVisualizer/InfoDataConsumptionVisualizer.tsx +++ b/src/components/ConsumptionVisualizer/InfoDataConsumptionVisualizer.tsx @@ -7,9 +7,9 @@ import React, { Dispatch, useCallback, useState } from 'react' import { useDispatch, useSelector } from 'react-redux' import DateChartService from 'services/dateChart.service' import { AppActionsTypes, AppStore } from 'store' -import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.actions' -import './infoDataConsumptionVisualizer.scss' +import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.slice' import NoDataModal from './NoDataModal' +import './infoDataConsumptionVisualizer.scss' interface InfoDataConsumptionVisualizerProps { dataload: Dataload diff --git a/src/components/DateNavigator/DateNavigator.tsx b/src/components/DateNavigator/DateNavigator.tsx index 9bdb35a7e6d6f32912b0a016cd17974d8ee138f1..ba0054f21db61154cfa79c8a3a9f10763f1173ee 100644 --- a/src/components/DateNavigator/DateNavigator.tsx +++ b/src/components/DateNavigator/DateNavigator.tsx @@ -12,7 +12,7 @@ import React, { Dispatch } from 'react' import { useDispatch, useSelector } from 'react-redux' import DateChartService from 'services/dateChart.service' import { AppActionsTypes, AppStore } from 'store' -import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.actions' +import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.slice' import { isLastDateReached } from 'utils/date' import { isKonnectorActive } from 'utils/utils' import './datenavigator.scss' diff --git a/src/components/FluidChart/FluidChartSlide.tsx b/src/components/FluidChart/FluidChartSlide.tsx index b272ab25a177fc30f98ca24ebe55542e199ce9a1..999fda16cac71d21d354bd93423a793a14316418 100644 --- a/src/components/FluidChart/FluidChartSlide.tsx +++ b/src/components/FluidChart/FluidChartSlide.tsx @@ -11,7 +11,7 @@ import { useDispatch, useSelector } from 'react-redux' import ConsumptionService from 'services/consumption.service' import DateChartService from 'services/dateChart.service' import { AppActionsTypes, AppStore } from 'store' -import { setCurrentDatachart, setLoading } from 'store/chart/chart.actions' +import { setCurrentDataChart, setLoading } from 'store/chart/chart.slice' import './fluidChartSlide.scss' interface FluidChartSlideProps { @@ -112,7 +112,7 @@ const FluidChartSlide: React.FC<FluidChartSlideProps> = ({ useEffect(() => { if (index === currentIndex) { - dispatch(setCurrentDatachart(chartData)) + dispatch(setCurrentDataChart(chartData)) } }, [dispatch, index, currentIndex, chartData]) diff --git a/src/components/FluidChart/FluidChartSwipe.tsx b/src/components/FluidChart/FluidChartSwipe.tsx index 00f87e72468b2848597a4bafe5f78af2fbdef272..fa6a84cf2dac013788de62ad4fad65338e1756a8 100644 --- a/src/components/FluidChart/FluidChartSwipe.tsx +++ b/src/components/FluidChart/FluidChartSwipe.tsx @@ -8,7 +8,7 @@ import SwipeableViews from 'react-swipeable-views' import { virtualize } from 'react-swipeable-views-utils' import DateChartService from 'services/dateChart.service' import { AppActionsTypes, AppStore } from 'store' -import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.actions' +import { setCurrentIndex, setSelectedDate } from 'store/chart/chart.slice' import './fluidChartSwipe.scss' const VirtualizeSwipeableViews = virtualize(SwipeableViews) diff --git a/src/components/Home/ConsumptionView.spec.tsx b/src/components/Home/ConsumptionView.spec.tsx index 63d30b40b750f89f1d3d6d2ee2927d8c15e19160..3d8d1dc09c3d3eaf668c38d0c8c61abbd558c1b5 100644 --- a/src/components/Home/ConsumptionView.spec.tsx +++ b/src/components/Home/ConsumptionView.spec.tsx @@ -6,7 +6,7 @@ import { FluidStatus } from 'models' import React from 'react' import * as reactRedux from 'react-redux' import { Provider } from 'react-redux' -import * as chartActions from 'store/chart/chart.actions' +import * as chartActions from 'store/chart/chart.slice' import { mockTestProfile1 } from '../../../tests/__mocks__/profileType.mock' import { createMockEcolyoStore, diff --git a/src/components/Home/ConsumptionView.tsx b/src/components/Home/ConsumptionView.tsx index d5fe2a2b6bc98296719abc8213ed62a16104e0fe..778eb6384278077d5552397c3d7b14f5d275070e 100644 --- a/src/components/Home/ConsumptionView.tsx +++ b/src/components/Home/ConsumptionView.tsx @@ -20,7 +20,7 @@ import { useDispatch, useSelector } from 'react-redux' import { useNavigate } from 'react-router-dom' import ProfileService from 'services/profile.service' import { AppActionsTypes, AppStore } from 'store' -import { setCurrentTimeStep, setLoading } from 'store/chart/chart.actions' +import { setCurrentTimeStep, setLoading } from 'store/chart/chart.slice' import { setCustomPopup, showReleaseNotes } from 'store/global/global.actions' import { openPartnersModal } from 'store/modal/modal.slice' import { diff --git a/src/components/Hooks/useKonnectorAuth.tsx b/src/components/Hooks/useKonnectorAuth.tsx index 2c5cfbad339da68ac1308a190808b657edadc046..d1d1c0bf9bea1937fdb12a4a0feae1f674b127a3 100644 --- a/src/components/Hooks/useKonnectorAuth.tsx +++ b/src/components/Hooks/useKonnectorAuth.tsx @@ -16,7 +16,7 @@ import AccountService from 'services/account.service' import ConnectionService from 'services/connection.service' import UsageEventService from 'services/usageEvent.service' import { AppActionsTypes, AppStore } from 'store' -import { setLoading } from 'store/chart/chart.actions' +import { setLoading } from 'store/chart/chart.slice' import { updatedFluidConnection } from 'store/global/global.actions' import logApp from 'utils/logger' diff --git a/src/components/Konnector/KonnectorViewerCard.tsx b/src/components/Konnector/KonnectorViewerCard.tsx index 9d4d51715dc4e34abebae9f459171a93d8353a96..4ea9c7b4fa0203b3370f71e8bc80f920bfa7fdfe 100644 --- a/src/components/Konnector/KonnectorViewerCard.tsx +++ b/src/components/Konnector/KonnectorViewerCard.tsx @@ -55,7 +55,7 @@ import PartnersInfoService from 'services/partnersInfo.service' import UsageEventService from 'services/usageEvent.service' import { AppActionsTypes, AppStore } from 'store' import { setChallengeConsumption } from 'store/challenge/challenge.actions' -import { setSelectedDate } from 'store/chart/chart.actions' +import { setSelectedDate } from 'store/chart/chart.slice' import { setFluidStatus, toggleChallengeDuelNotification, diff --git a/src/components/Splash/SplashRoot.tsx b/src/components/Splash/SplashRoot.tsx index a4f030fa5fa3de18e7880d19cfaae3c88e3a7333..dad9a6ff5bf431b04542cc825a28858be272a642 100644 --- a/src/components/Splash/SplashRoot.tsx +++ b/src/components/Splash/SplashRoot.tsx @@ -42,7 +42,7 @@ import { setUserChallengeList, updateUserChallengeList, } from 'store/challenge/challenge.actions' -import { setSelectedDate } from 'store/chart/chart.actions' +import { setSelectedDate } from 'store/chart/chart.slice' import { setCustomPopup, setFluidStatus, diff --git a/src/components/TimeStepSelector/TimeStepSelector.spec.tsx b/src/components/TimeStepSelector/TimeStepSelector.spec.tsx index 619d07743f63c4e7236320a5ada1a099bbedbb9b..6678b795d7a4f017c738096bb22a3ab1c3a57ee1 100644 --- a/src/components/TimeStepSelector/TimeStepSelector.spec.tsx +++ b/src/components/TimeStepSelector/TimeStepSelector.spec.tsx @@ -8,7 +8,7 @@ import React from 'react' import * as reactRedux from 'react-redux' import { Provider } from 'react-redux' import UsageEventService from 'services/usageEvent.service' -import * as chartActions from 'store/chart/chart.actions' +import * as chartActions from 'store/chart/chart.slice' import { createMockEcolyoStore } from '../../../tests/__mocks__/store' jest.mock('cozy-ui/transpiled/react/I18n', () => { diff --git a/src/components/TimeStepSelector/TimeStepSelector.tsx b/src/components/TimeStepSelector/TimeStepSelector.tsx index cf6c3fc9c1e6f74fbf8d4b35faedee2668e57832..e38857efb36221428a57f9493562c2e4c61f7bbe 100644 --- a/src/components/TimeStepSelector/TimeStepSelector.tsx +++ b/src/components/TimeStepSelector/TimeStepSelector.tsx @@ -14,7 +14,7 @@ import { setCurrentIndex, setCurrentTimeStep, setSelectedDate, -} from 'store/chart/chart.actions' +} from 'store/chart/chart.slice' import './timeStepSelector.scss' interface TimeStepSelectorProps { diff --git a/src/store/chart/chart.action.spec.ts b/src/store/chart/chart.action.spec.ts deleted file mode 100644 index c7b1b47fa86d1b0eb653dc1aee7e519edfe8c18a..0000000000000000000000000000000000000000 --- a/src/store/chart/chart.action.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -import { TimeStep } from 'enum/timeStep.enum' -import { DateTime } from 'luxon' -import { graphData } from '../../../tests/__mocks__/chartData.mock' -import { - setCurrentDatachart, - setCurrentDatachartIndex, - setCurrentIndex, - setCurrentTimeStep, - setLoading, - setSelectedDate, - SET_CURRENT_DATACHART, - SET_CURRENT_DATACHART_INDEX, - SET_CURRENT_INDEX, - SET_CURRENT_TIMESTEP, - SET_LOADING, - SET_SELECTED_DATE, -} from './chart.actions' - -describe('chart actions', () => { - it('should create an action to set selected date', () => { - const date = DateTime.local().setZone('utc', { - keepLocalTime: true, - }) - const expectedAction = { - type: SET_SELECTED_DATE, - payload: date, - } - expect(setSelectedDate(date)).toEqual(expectedAction) - }) - - it('should create an action to set time step', () => { - const timeStep = TimeStep.DAY - const expectedAction = { - type: SET_CURRENT_TIMESTEP, - payload: timeStep, - } - expect(setCurrentTimeStep(timeStep)).toEqual(expectedAction) - }) - - it('should create an action to set index', () => { - const expectedAction = { - type: SET_CURRENT_INDEX, - payload: 1, - } - expect(setCurrentIndex(1)).toEqual(expectedAction) - }) - - it('should create an action to set datachart', () => { - const expectedAction = { - type: SET_CURRENT_DATACHART, - payload: graphData, - } - expect(setCurrentDatachart(graphData)).toEqual(expectedAction) - }) - - it('should create an action to set datachart index', () => { - const expectedAction = { - type: SET_CURRENT_DATACHART_INDEX, - payload: 1, - } - expect(setCurrentDatachartIndex(1)).toEqual(expectedAction) - }) - - it('should create an action to set loading', () => { - const expectedAction = { - type: SET_LOADING, - payload: true, - } - expect(setLoading(true)).toEqual(expectedAction) - }) -}) diff --git a/src/store/chart/chart.actions.ts b/src/store/chart/chart.actions.ts deleted file mode 100644 index 0b65793885c3501cba3f123b5823042a1978eb6c..0000000000000000000000000000000000000000 --- a/src/store/chart/chart.actions.ts +++ /dev/null @@ -1,96 +0,0 @@ -import { TimeStep } from 'enum/timeStep.enum' -import { DateTime } from 'luxon' -import { Datachart } from 'models' -import { defaultAction } from 'store' - -export const SET_CURRENT_DATACHART = 'SET_CURRENT_DATACHART' -export const SET_CURRENT_DATACHART_INDEX = 'SET_CURRENT_DATACHART_INDEX' -export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX' -export const SET_CURRENT_TIMESTEP = 'SET_CURRENT_TIMESTEP' -export const SET_LOADING = 'SET_LOADING' -export const SET_SELECTED_DATE = 'SET_SELECTED_DATE' - -interface SetSelectedDate { - type: typeof SET_SELECTED_DATE - payload?: DateTime -} - -interface SetCurrentTimeStep { - type: typeof SET_CURRENT_TIMESTEP - payload?: TimeStep -} - -interface SetCurrentIndex { - type: typeof SET_CURRENT_INDEX - payload?: number -} - -interface SetCurrentDataChart { - type: typeof SET_CURRENT_DATACHART - payload?: Datachart -} - -interface SetCurrentDataChartIndex { - type: typeof SET_CURRENT_DATACHART_INDEX - payload?: number -} - -interface SetLoading { - type: typeof SET_LOADING - payload?: boolean -} - -export function setSelectedDate(date: DateTime): SetSelectedDate { - return { - type: SET_SELECTED_DATE, - payload: date, - } -} - -export function setCurrentTimeStep(timeStep: TimeStep): SetCurrentTimeStep { - return { - type: SET_CURRENT_TIMESTEP, - payload: timeStep, - } -} - -export function setCurrentIndex(currentIndex: number): SetCurrentIndex { - return { - type: SET_CURRENT_INDEX, - payload: currentIndex, - } -} - -export function setCurrentDatachart( - currentDatachart: Datachart -): SetCurrentDataChart { - return { - type: SET_CURRENT_DATACHART, - payload: currentDatachart, - } -} - -export function setCurrentDatachartIndex( - currentDatachartIndex: number -): SetCurrentDataChartIndex { - return { - type: SET_CURRENT_DATACHART_INDEX, - payload: currentDatachartIndex, - } -} - -export function setLoading(isLoading: boolean): SetLoading { - return { - type: SET_LOADING, - payload: isLoading, - } -} - -export type ChartActionTypes = - | SetSelectedDate - | SetCurrentTimeStep - | SetCurrentIndex - | SetCurrentDataChart - | SetCurrentDataChartIndex - | SetLoading - | typeof defaultAction diff --git a/src/store/chart/chart.reducer.spec.ts b/src/store/chart/chart.reducer.spec.ts deleted file mode 100644 index 1f167acb432198679abfce62e431d36101c2e147..0000000000000000000000000000000000000000 --- a/src/store/chart/chart.reducer.spec.ts +++ /dev/null @@ -1,138 +0,0 @@ -import { TimeStep } from 'enum/timeStep.enum' -import { DateTime } from 'luxon' -import { defaultAction } from 'store' -import { graphData } from '../../../tests/__mocks__/chartData.mock' -import { mockInitialChartState } from '../../../tests/__mocks__/store' -import { - SET_CURRENT_DATACHART, - SET_CURRENT_DATACHART_INDEX, - SET_CURRENT_INDEX, - SET_CURRENT_TIMESTEP, - SET_LOADING, - SET_SELECTED_DATE, -} from './chart.actions' -import { chartReducer } from './chart.reducer' - -describe('chart reducer', () => { - it('should return the initial state', () => { - const state = chartReducer(undefined, { ...defaultAction }) - expect(state).toEqual(mockInitialChartState) - }) - - describe('SET_SELECTED_DATE', () => { - it('should handle SET_SELECTED_DATE with payload', () => { - const mockDate = DateTime.fromISO('2021-01-01T00:00:00.000Z', { - zone: 'utc', - }) - const state = chartReducer(mockInitialChartState, { - type: SET_SELECTED_DATE, - payload: mockDate, - }) - expect(state).toEqual({ - ...mockInitialChartState, - selectedDate: mockDate, - }) - }) - it('should handle SET_SELECTED_DATE without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_SELECTED_DATE, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) - - describe('SET_CURRENT_TIMESTEP', () => { - it('should handle SET_CURRENT_TIMESTEP with payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_TIMESTEP, - payload: TimeStep.MONTH, - }) - expect(state).toEqual({ - ...mockInitialChartState, - currentTimeStep: TimeStep.MONTH, - }) - }) - it('should handle SET_CURRENT_TIMESTEP without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_TIMESTEP, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) - - describe('SET_CURRENT_INDEX', () => { - it('should handle SET_CURRENT_INDEX with payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_INDEX, - payload: 1, - }) - expect(state).toEqual({ - ...mockInitialChartState, - currentIndex: 1, - }) - }) - it('should handle SET_CURRENT_INDEX without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_INDEX, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) - - describe('SET_CURRENT_DATACHART', () => { - it('should handle SET_CURRENT_DATACHART with payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_DATACHART, - payload: graphData, - }) - expect(state).toEqual({ - ...mockInitialChartState, - currentDatachart: graphData, - }) - }) - it('should handle SET_CURRENT_DATACHART without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_DATACHART, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) - - describe('SET_CURRENT_DATACHART_INDEX', () => { - it('should handle SET_CURRENT_DATACHART_INDEX with payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_DATACHART_INDEX, - payload: 1, - }) - expect(state).toEqual({ - ...mockInitialChartState, - currentDatachartIndex: 1, - }) - }) - it('should handle SET_CURRENT_DATACHART_INDEX without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_CURRENT_DATACHART_INDEX, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) - - describe('SET_LOADING', () => { - it('should handle SET_LOADING with payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_LOADING, - payload: false, - }) - expect(state).toEqual({ - ...mockInitialChartState, - loading: false, - }) - }) - it('should handle SET_LOADING without payload', () => { - const state = chartReducer(mockInitialChartState, { - type: SET_LOADING, - }) - expect(state).toEqual(mockInitialChartState) - }) - }) -}) diff --git a/src/store/chart/chart.reducer.ts b/src/store/chart/chart.reducer.ts deleted file mode 100644 index 9d79d2593a8b1830c45a3d16954b74313d4cde31..0000000000000000000000000000000000000000 --- a/src/store/chart/chart.reducer.ts +++ /dev/null @@ -1,59 +0,0 @@ -import { TimeStep } from 'enum/timeStep.enum' -import { DateTime } from 'luxon' -import { ChartState } from 'models/chart.model' -import { Reducer } from 'redux' -import { - ChartActionTypes, - SET_CURRENT_DATACHART, - SET_CURRENT_DATACHART_INDEX, - SET_CURRENT_INDEX, - SET_CURRENT_TIMESTEP, - SET_LOADING, - SET_SELECTED_DATE, -} from 'store/chart/chart.actions' - -const initialState: ChartState = { - selectedDate: DateTime.local().endOf('minute').setZone('utc', { - keepLocalTime: true, - }), - currentTimeStep: TimeStep.WEEK, - currentIndex: 0, - currentDatachart: { actualData: [], comparisonData: null }, - currentDatachartIndex: 0, - loading: true, -} - -export const chartReducer: Reducer<ChartState, ChartActionTypes> = ( - state = initialState, - action -) => { - if (action.payload == undefined) return state - - const updateState = (updates: Partial<ChartState>): ChartState => ({ - ...state, - ...updates, - }) - - switch (action.type) { - case SET_SELECTED_DATE: - return updateState({ selectedDate: action.payload }) - - case SET_CURRENT_TIMESTEP: - return updateState({ currentTimeStep: action.payload }) - - case SET_CURRENT_INDEX: - return updateState({ currentIndex: action.payload }) - - case SET_CURRENT_DATACHART: - return updateState({ currentDatachart: action.payload }) - - case SET_CURRENT_DATACHART_INDEX: - return updateState({ currentDatachartIndex: action.payload }) - - case SET_LOADING: - return updateState({ loading: action.payload }) - - default: - return state - } -} diff --git a/src/store/chart/chart.slice.spec.ts b/src/store/chart/chart.slice.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..fbee57efd23523e4b87857356f8dfc07bb0f2daa --- /dev/null +++ b/src/store/chart/chart.slice.spec.ts @@ -0,0 +1,103 @@ +import { TimeStep } from 'enum/timeStep.enum' +import { DateTime } from 'luxon' +import { graphData } from '../../../tests/__mocks__/chartData.mock' +import { mockInitialChartState } from '../../../tests/__mocks__/store' +import { + chartSlice, + setCurrentDataChart, + setCurrentDataChartIndex, + setCurrentIndex, + setCurrentTimeStep, + setLoading, + setSelectedDate, +} from './chart.slice' + +describe('chart reducer', () => { + it('should return the initial state', () => { + const initialState = chartSlice.reducer(undefined, { type: undefined }) + expect(initialState).toEqual(mockInitialChartState) + }) + + it('should return same state if no action', () => { + const state = chartSlice.reducer(mockInitialChartState, { type: undefined }) + expect(state).toEqual(mockInitialChartState) + }) + + describe('setSelectedDate', () => { + it('should handle setSelectedDate with payload', () => { + const mockDate = DateTime.fromISO('2021-01-01T00:00:00.000Z', { + zone: 'utc', + }) + const state = chartSlice.reducer( + mockInitialChartState, + setSelectedDate(mockDate) + ) + expect(state).toEqual({ + ...mockInitialChartState, + selectedDate: mockDate, + }) + }) + }) + + describe('setCurrentTimeStep', () => { + it('should handle setCurrentTimeStep with payload', () => { + const state = chartSlice.reducer( + mockInitialChartState, + setCurrentTimeStep(TimeStep.MONTH) + ) + expect(state).toEqual({ + ...mockInitialChartState, + currentTimeStep: TimeStep.MONTH, + }) + }) + }) + + describe('setCurrentIndex', () => { + it('should handle setCurrentIndex with payload', () => { + const state = chartSlice.reducer( + mockInitialChartState, + setCurrentIndex(1) + ) + expect(state).toEqual({ + ...mockInitialChartState, + currentIndex: 1, + }) + }) + }) + + describe('setCurrentDataChart', () => { + it('should handle setCurrentDataChart with payload', () => { + const state = chartSlice.reducer( + mockInitialChartState, + setCurrentDataChart(graphData) + ) + expect(state).toEqual({ + ...mockInitialChartState, + currentDatachart: graphData, + }) + }) + }) + + describe('setCurrentDataChartIndex', () => { + it('should handle setCurrentDataChartIndex with payload', () => { + const state = chartSlice.reducer( + mockInitialChartState, + setCurrentDataChartIndex(1) + ) + expect(state).toEqual({ + ...mockInitialChartState, + currentDatachartIndex: 1, + }) + }) + }) + + describe('setLoading', () => { + it('should handle setLoading with payload', () => { + const state = chartSlice.reducer(mockInitialChartState, setLoading(false)) + expect(state).toEqual({ + ...mockInitialChartState, + loading: false, + }) + }) + }) +}) diff --git a/src/store/chart/chart.slice.ts b/src/store/chart/chart.slice.ts new file mode 100644 index 0000000000000000000000000000000000000000..ad1041a5e409d9f2451c46abf2e720410ee0b1d5 --- /dev/null +++ b/src/store/chart/chart.slice.ts @@ -0,0 +1,64 @@ +import { PayloadAction, createSlice } from '@reduxjs/toolkit' +import { TimeStep } from 'enum/timeStep.enum' +import { DateTime } from 'luxon' +import { ChartState, Datachart } from 'models' + +type SetCurrentDataChart = PayloadAction<Datachart> +type SetCurrentDataChartIndex = PayloadAction<number> +type SetCurrentIndex = PayloadAction<number> +type SetCurrentTimeStep = PayloadAction<TimeStep> +type SetLoading = PayloadAction<boolean> +type SetSelectedDate = PayloadAction<DateTime> + +export type ChartActionTypes = + | SetCurrentDataChart + | SetCurrentDataChartIndex + | SetCurrentIndex + | SetCurrentTimeStep + | SetLoading + | SetSelectedDate + +const initialState: ChartState = { + selectedDate: DateTime.local().endOf('minute').setZone('utc', { + keepLocalTime: true, + }), + currentTimeStep: TimeStep.WEEK, + currentIndex: 0, + currentDatachart: { actualData: [], comparisonData: null }, + currentDatachartIndex: 0, + loading: true, +} + +export const chartSlice = createSlice({ + name: 'chart', + initialState, + reducers: { + setCurrentDataChart: (state, action: SetCurrentDataChart) => { + state.currentDatachart = action.payload + }, + setCurrentDataChartIndex: (state, action: SetCurrentDataChartIndex) => { + state.currentDatachartIndex = action.payload + }, + setCurrentIndex: (state, action: SetCurrentIndex) => { + state.currentIndex = action.payload + }, + setCurrentTimeStep: (state, action: SetCurrentTimeStep) => { + state.currentTimeStep = action.payload + }, + setLoading: (state, action: SetLoading) => { + state.loading = action.payload + }, + setSelectedDate: (state, action: SetSelectedDate) => { + state.selectedDate = action.payload + }, + }, +}) + +export const { + setCurrentDataChart, + setCurrentDataChartIndex, + setCurrentIndex, + setCurrentTimeStep, + setLoading, + setSelectedDate, +} = chartSlice.actions diff --git a/src/store/index.ts b/src/store/index.ts index 0f372642928a24fd1e6a6f136d015c39bf68cb5a..dc884f4bf91245a8e3dfeb76bb5ec4972e4aecc6 100644 --- a/src/store/index.ts +++ b/src/store/index.ts @@ -21,8 +21,7 @@ import thunkMiddleware from 'redux-thunk' import { globalReducer } from 'store/global/global.reducer' import { ChallengeActionTypes } from './challenge/challenge.actions' import { challengeReducer } from './challenge/challenge.reducer' -import { ChartActionTypes } from './chart/chart.actions' -import { chartReducer } from './chart/chart.reducer' +import { ChartActionTypes, chartSlice } from './chart/chart.slice' import { GlobalActionTypes } from './global/global.actions' import { ModalActionTypes, modalSlice } from './modal/modal.slice' import { ProfileActionTypes } from './profile/profile.actions' @@ -46,7 +45,7 @@ export const defaultAction = { type: null, payload: undefined } const ecolyoReducer = combineReducers({ challenge: challengeReducer, - chart: chartReducer, + chart: chartSlice.reducer, global: globalReducer, modal: modalSlice.reducer, profile: profileReducer, diff --git a/src/store/modal/modal.slice.spec.ts b/src/store/modal/modal.slice.spec.ts index e2e52d06b89e04b0b7703df5e9f99e51d30e471d..28de84b03f87957373c8051d1f28c60c5d4cf5c9 100644 --- a/src/store/modal/modal.slice.spec.ts +++ b/src/store/modal/modal.slice.spec.ts @@ -19,13 +19,6 @@ describe('modal reducer', () => { isFeedbacksOpen: true, }) }) - // to remove ? not very useful - it('should handle UPDATE_FEEDBACK_MODAL without payload', () => { - const state = modalSlice.reducer(mockInitialModalState, { - type: undefined, - }) - expect(state).toEqual(mockInitialModalState) - }) }) describe('PartnersInfo', () => {