Skip to content
Snippets Groups Projects
search-multiple.reducer.ts 4.28 KiB
Newer Older
  • Learn to ignore specific revisions
  • import * as actions from './search-multiple.action';
    
    import { Attribute } from "../../metamodel/model";
    import { DatasetCount, SearchMultipleQueryParams } from "./model";
    
    
    export interface State {
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        pristine: boolean;
    
        currentStep: string;
        positionStepChecked: boolean;
        datasetsStepChecked: boolean;
        resultStepChecked: boolean;
        queryParams: SearchMultipleQueryParams;
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        selectedDatasets: string[];
    
        datasetsCountIsLoading: boolean;
        datasetsCountIsLoaded: boolean;
    
        attributeList: { dname: string, attributeList: Attribute[] }[];
    
        data: { dname: string, data: any[] }[];
        selectedData: { dname: string, data: (string | number)[] }[];
    
    }
    
    export const initialState: State = {
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
        pristine: true,
    
        currentStep: null,
        positionStepChecked: false,
        datasetsStepChecked: false,
        resultStepChecked: false,
        queryParams: null,
    
        selectedDatasets: [],
        datasetsCountIsLoading: false,
        datasetsCountIsLoaded: false,
        datasetsCount: [],
    
        data: [],
        selectedData: []
    
    };
    
    export function reducer(state: State = initialState, action: actions.Actions): State {
        switch (action.type) {
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            case actions.INIT_SELECTED_DATASETS:
                return {
                    ...state,
    
                    selectedDatasets: action.payload
    
            case actions.CHANGE_STEP:
                return {
                    ...state,
    
                    currentStep: action.payload
    
                };
    
            case actions.POSITION_CHECKED:
                return {
                    ...state,
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
                    pristine: false,
    
                    positionStepChecked: true
                };
    
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
            case actions.DATASETS_CHECKED:
                return {
                    ...state,
                    datasetsStepChecked: true
                };
    
    
            case actions.RETRIEVE_DATASETS_COUNT:
                return {
                    ...state,
                    datasetsCountIsLoading: true
                };
    
            case actions.RETRIEVE_DATASETS_COUNT_SUCCESS:
                return {
                    ...state,
                    datasetsCountIsLoading: false,
                    datasetsCountIsLoaded: true,
                    datasetsCount: action.payload
                };
    
            case actions.RETRIEVE_DATASETS_COUNT_FAIL:
                return {
                    ...state,
                    datasetsCountIsLoading: false
                };
    
    
            case actions.UPDATE_SELECTED_DATASETS:
    
                return {
                    ...state,
    
                    selectedDatasets: action.payload
    
            case actions.RETRIEVE_ATTRIBUTE_LIST_SUCCESS:
                return {
                    ...state,
    
                    attributeList: [...state.attributeList, action.payload]
    
            case actions.RETRIEVE_DATA_SUCCESS:
                const data = [...state.data.filter(d => d.dname !== action.payload.dname), action.payload];
                return {
                    ...state,
                    data
                };
    
    
            case actions.UPDATE_SELECTED_DATA:
                return {
                    ...state,
                    selectedData: action.payload
                };
    
    
            case actions.DESTROY_RESULTS:
                return {
                    ...state,
                    datasetsCountIsLoaded: false,
    
                    data: [],
                    selectedData: []
    
            case actions.RESET_SEARCH:
                return { ...initialState };
    
    
            default:
                return state;
        }
    }
    
    
    export const getCurrentStep = (state: State) => state.currentStep;
    export const getPositionStepChecked = (state: State) => state.positionStepChecked;
    export const getDatasetsStepChecked = (state: State) => state.datasetsStepChecked;
    export const getResultStepChecked = (state: State) => state.resultStepChecked;
    export const getQueryParams = (state: State) => state.queryParams;
    
    Tifenn Guillas's avatar
    Tifenn Guillas committed
    export const getSelectedDatasets = (state: State) => state.selectedDatasets;
    
    export const getDatasetsCountIsLoading = (state: State) => state.datasetsCountIsLoading;
    export const getDatasetsCountIsLoaded = (state: State) => state.datasetsCountIsLoaded;
    export const getDatasetsCount = (state: State) => state.datasetsCount;
    export const getAttributeList = (state: State) => state.attributeList;
    
    export const getData = (state: State) => state.data;
    
    export const getSelectedData = (state: State) => state.selectedData;