Skip to content
Snippets Groups Projects
Commit 8199c8ef authored by Yoan VALLET's avatar Yoan VALLET
Browse files

feat: update chart store

parent bc69618d
No related branches found
No related tags found
1 merge request!248Features/us365 change fluid navigation
import { TimeStep } from 'enum/timeStep.enum'
import { DateTime } from 'luxon'
import { Datachart } from 'models'
export interface ChartState {
selectedDate: DateTime
currentTimeStep: TimeStep
currentIndex: number
currentDatachart: Datachart
currentDatachartIndex: number
loading: boolean
}
......@@ -4,12 +4,17 @@ import {
SET_CURRENT_TIMESTEP,
SET_SELECTED_DATE,
SET_CURRENT_INDEX,
SET_CURRENT_DATACHART,
SET_LOADING,
setCurrentTimeStep,
setSelectedDate,
setCurrentIndex,
setCurrentDatachart,
setCurrentDatachartIndex,
setLoading,
SET_CURRENT_DATACHART_INDEX,
} from './chart.actions'
import { graphData } from '../../../test/__mocks__/datachartData.mock'
describe('chart actions', () => {
it('should create an action to set selected date', () => {
......@@ -40,6 +45,22 @@ describe('chart actions', () => {
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,
......
import { TimeStep } from 'enum/timeStep.enum'
import { DateTime } from 'luxon'
import { Datachart } from 'models'
export const SET_CURRENT_TIMESTEP = 'SET_CURRENT_TIMESTEP'
export const SET_SELECTED_DATE = 'SET_SELECTED_DATE'
export const SET_CURRENT_INDEX = 'SET_CURRENT_INDEX'
export const SET_CURRENT_DATACHART = 'SET_CURRENT_DATACHART'
export const SET_CURRENT_DATACHART_INDEX = 'SET_CURRENT_DATACHART_INDEX'
export const SET_LOADING = 'SET_LOADING'
interface SetSelectedDate {
......@@ -21,6 +24,16 @@ interface SetCurrentIndex {
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
......@@ -30,6 +43,8 @@ export type ChartActionTypes =
| SetSelectedDate
| SetCurrentTimeStep
| SetCurrentIndex
| SetCurrentDataChart
| SetCurrentDataChartIndex
| SetLoading
export function setSelectedDate(date: DateTime): ChartActionTypes {
......@@ -53,6 +68,24 @@ export function setCurrentIndex(currentIndex: number): ChartActionTypes {
}
}
export function setCurrentDatachart(
currentDatachart: Datachart
): ChartActionTypes {
return {
type: SET_CURRENT_DATACHART,
payload: currentDatachart,
}
}
export function setCurrentDatachartIndex(
currentDatachartIndex: number
): ChartActionTypes {
return {
type: SET_CURRENT_DATACHART_INDEX,
payload: currentDatachartIndex,
}
}
export function setLoading(isLoading: boolean): ChartActionTypes {
return {
type: SET_LOADING,
......
......@@ -3,11 +3,14 @@ import {
SET_CURRENT_TIMESTEP,
SET_SELECTED_DATE,
SET_CURRENT_INDEX,
SET_CURRENT_DATACHART,
SET_CURRENT_DATACHART_INDEX,
SET_LOADING,
} from './chart.actions'
import { DateTime } from 'luxon'
import { TimeStep } from 'enum/timeStep.enum'
import { mockInitialChartState } from '../../../test/__mocks__/store'
import { DateTime } from 'luxon'
import { graphData } from '../../../test/__mocks__/datachartData.mock'
describe('chart reducer', () => {
it('should return the initial state', () => {
......@@ -76,6 +79,42 @@ describe('chart reducer', () => {
expect(result).toEqual(mockInitialChartState)
})
it('should handle SET_CURRENT_DATACHART with payload', () => {
const result = chartReducer(mockInitialChartState, {
type: SET_CURRENT_DATACHART,
payload: graphData,
})
expect(result).toEqual({
...mockInitialChartState,
currentDatachart: graphData,
})
})
it('should handle SET_CURRENT_DATACHART without payload', () => {
const result = chartReducer(mockInitialChartState, {
type: SET_CURRENT_DATACHART,
})
expect(result).toEqual(mockInitialChartState)
})
it('should handle SET_CURRENT_DATACHART_INDEX with payload', () => {
const result = chartReducer(mockInitialChartState, {
type: SET_CURRENT_DATACHART_INDEX,
payload: 1,
})
expect(result).toEqual({
...mockInitialChartState,
currentDatachartIndex: 1,
})
})
it('should handle SET_CURRENT_DATACHART_INDEX without payload', () => {
const result = chartReducer(mockInitialChartState, {
type: SET_CURRENT_DATACHART_INDEX,
})
expect(result).toEqual(mockInitialChartState)
})
it('should handle SET_LOADING with payload', () => {
const result = chartReducer(mockInitialChartState, {
type: SET_LOADING,
......
......@@ -3,6 +3,8 @@ import {
SET_SELECTED_DATE,
SET_CURRENT_TIMESTEP,
SET_CURRENT_INDEX,
SET_CURRENT_DATACHART,
SET_CURRENT_DATACHART_INDEX,
SET_LOADING,
ChartActionTypes,
} from 'store/chart/chart.actions'
......@@ -18,6 +20,8 @@ const initialState: ChartState = {
}),
currentTimeStep: TimeStep.WEEK,
currentIndex: 0,
currentDatachart: { actualData: [], comparisonData: null },
currentDatachartIndex: 0,
loading: true,
}
......@@ -47,6 +51,20 @@ export const chartReducer: Reducer<ChartState> = (
currentIndex: action.payload,
}
: state
case SET_CURRENT_DATACHART:
return action.payload != undefined
? {
...state,
currentDatachart: action.payload,
}
: state
case SET_CURRENT_DATACHART_INDEX:
return action.payload != undefined
? {
...state,
currentDatachartIndex: action.payload,
}
: state
case SET_LOADING:
return action.payload != undefined
? {
......
......@@ -135,6 +135,8 @@ export const mockInitialChartState: ChartState = {
}),
currentTimeStep: TimeStep.WEEK,
currentIndex: 0,
currentDatachart: { actualData: [], comparisonData: null },
currentDatachartIndex: 0,
loading: true,
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment