Skip to content

Commit

Permalink
ADM-777:[frontend]feat: add should refresh data step state
Browse files Browse the repository at this point in the history
  • Loading branch information
weiraneve committed Feb 2, 2024
1 parent 2013e5d commit ac1cfcb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions frontend/__tests__/context/stepperSlice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('stepper reducer', () => {
{
stepNumber: 0,
timeStamp: 0,
shouldRefreshData: true,
},
nextStep(),
);
Expand All @@ -32,6 +33,7 @@ describe('stepper reducer', () => {
{
stepNumber: 0,
timeStamp: 0,
shouldRefreshData: true,
},
backStep(),
);
Expand All @@ -44,6 +46,7 @@ describe('stepper reducer', () => {
{
stepNumber: 2,
timeStamp: 0,
shouldRefreshData: true,
},
backStep(),
);
Expand All @@ -57,6 +60,7 @@ describe('stepper reducer', () => {
{
stepNumber: 2,
timeStamp: 0,
shouldRefreshData: true,
},
updateTimeStamp(mockTime),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getEmojiUrls, removeExtraEmojiName } from '@src/constants/emojis/emoji';
import { Autocomplete, Box, ListItemText, TextField } from '@mui/material';
import { shouldRefreshData } from '@src/context/stepper/StepperSlice';
import { EmojiWrap, StyledAvatar } from '@src/constants/emojis/style';
import React, { useEffect, useState } from 'react';
import { Z_INDEX } from '@src/constants/commons';
import { FormControlWrapper } from './style';
import { useAppSelector } from '@src/hooks';

interface Props {
options: string[];
Expand All @@ -18,6 +20,7 @@ export const SingleSelection = ({ options, label, value, id, onGetSteps, onUpDat
const labelId = `single-selection-${label.toLowerCase().replace(' ', '-')}`;
const [selectedOptions, setSelectedOptions] = useState(value);
const [inputValue, setInputValue] = useState<string>(value);
const shouldRefresh = useAppSelector(shouldRefreshData);

const handleSelectedOptionsChange = (value: string) => {
setSelectedOptions(value);
Expand All @@ -29,7 +32,7 @@ export const SingleSelection = ({ options, label, value, id, onGetSteps, onUpDat
};

useEffect(() => {
if (onGetSteps && !!selectedOptions) {
if (onGetSteps && !!selectedOptions && shouldRefresh) {
onGetSteps(selectedOptions);
}
}, []);
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/context/stepper/StepperSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import type { RootState } from '@src/store';
export interface StepState {
stepNumber: number;
timeStamp: number;
shouldRefreshData: boolean;
}

const initialState: StepState = {
stepNumber: 0,
timeStamp: 0,
shouldRefreshData: true,
};

export const stepperSlice = createSlice({
Expand All @@ -21,9 +23,11 @@ export const stepperSlice = createSlice({
state.timeStamp = initialState.timeStamp;
},
nextStep: (state) => {
state.shouldRefreshData = true;
state.stepNumber += 1;
},
backStep: (state) => {
state.shouldRefreshData = false;
state.stepNumber = state.stepNumber === ZERO ? ZERO : state.stepNumber - 1;
},
updateTimeStamp: (state, action) => {
Expand All @@ -36,5 +40,6 @@ export const { resetStep, nextStep, backStep, updateTimeStamp } = stepperSlice.a

export const selectStepNumber = (state: RootState) => state.stepper.stepNumber;
export const selectTimeStamp = (state: RootState) => state.stepper.timeStamp;
export const shouldRefreshData = (state: RootState) => state.stepper.shouldRefreshData;

export default stepperSlice.reducer;
4 changes: 3 additions & 1 deletion frontend/src/hooks/useGetPipelineToolInfoEffect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@src/context/config/configSlice';
import { pipelineToolClient, IGetPipelineToolInfoResult } from '@src/clients/pipeline/PipelineToolClient';
import { updatePipelineSettings } from '@src/context/Metrics/metricsSlice';
import { shouldRefreshData } from '@src/context/stepper/StepperSlice';
import { useEffect, useState, useRef, useCallback } from 'react';
import { useAppDispatch, useAppSelector } from '@src/hooks';

Expand All @@ -30,6 +31,7 @@ export const useGetPipelineToolInfoEffect = (): IUseVerifyPipeLineToolStateInter
const isProjectCreated = useAppSelector(selectIsProjectCreated);
const restoredPipelineTool = useAppSelector(selectPipelineTool);
const dateRange = useAppSelector(selectDateRange);
const shouldRefresh = useAppSelector(shouldRefreshData);

const getPipelineToolInfo = useCallback(async () => {
const params = {
Expand All @@ -55,7 +57,7 @@ export const useGetPipelineToolInfoEffect = (): IUseVerifyPipeLineToolStateInter
]);

useEffect(() => {
if (!apiTouchedRef.current && !isLoading) {
if (!apiTouchedRef.current && !isLoading && shouldRefresh) {
apiTouchedRef.current = true;
getPipelineToolInfo();
}
Expand Down

0 comments on commit ac1cfcb

Please sign in to comment.