models-reducer.ts 3.4 KB
Newer Older
1 2
import { AnyAction } from 'redux';

3 4
import { ModelsActionTypes } from 'actions/models-actions';
import { AuthActionTypes } from 'actions/auth-actions';
5 6 7 8
import { ModelsState } from './interfaces';

const defaultState: ModelsState = {
    initialized: false,
9
    fetching: false,
10 11 12 13
    creatingStatus: '',
    models: [],
    visibleRunWindows: false,
    activeRunTask: null,
14
    inferences: {},
15 16 17 18 19 20 21 22
};

export default function (state = defaultState, action: AnyAction): ModelsState {
    switch (action.type) {
        case ModelsActionTypes.GET_MODELS: {
            return {
                ...state,
                initialized: false,
23
                fetching: true,
24 25 26 27 28 29 30
            };
        }
        case ModelsActionTypes.GET_MODELS_SUCCESS: {
            return {
                ...state,
                models: action.payload.models,
                initialized: true,
31
                fetching: false,
32 33 34 35 36 37
            };
        }
        case ModelsActionTypes.GET_MODELS_FAILED: {
            return {
                ...state,
                initialized: true,
38
                fetching: false,
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
            };
        }
        case ModelsActionTypes.DELETE_MODEL_SUCCESS: {
            return {
                ...state,
                models: state.models.filter(
                    (model): boolean => model.id !== action.payload.id,
                ),
            };
        }
        case ModelsActionTypes.CREATE_MODEL: {
            return {
                ...state,
                creatingStatus: '',
            };
        }
        case ModelsActionTypes.CREATE_MODEL_STATUS_UPDATED: {
            return {
                ...state,
                creatingStatus: action.payload.status,
            };
        }
        case ModelsActionTypes.CREATE_MODEL_FAILED: {
            return {
                ...state,
                creatingStatus: '',
            };
        }
        case ModelsActionTypes.CREATE_MODEL_SUCCESS: {
            return {
                ...state,
                initialized: false,
                creatingStatus: 'CREATED',
            };
        }
74
        case ModelsActionTypes.SHOW_RUN_MODEL_DIALOG: {
75 76
            return {
                ...state,
77 78
                visibleRunWindows: true,
                activeRunTask: action.payload.taskInstance,
79 80
            };
        }
81
        case ModelsActionTypes.CLOSE_RUN_MODEL_DIALOG: {
82 83
            return {
                ...state,
84 85
                visibleRunWindows: false,
                activeRunTask: null,
86 87
            };
        }
88 89 90 91 92 93 94 95
        case ModelsActionTypes.GET_INFERENCE_STATUS_SUCCESS: {
            const inferences = { ...state.inferences };
            if (action.payload.activeInference.status === 'finished') {
                delete inferences[action.payload.taskID];
            } else {
                inferences[action.payload.taskID] = action.payload.activeInference;
            }

96 97
            return {
                ...state,
98
                inferences,
99 100
            };
        }
101 102 103 104
        case ModelsActionTypes.GET_INFERENCE_STATUS_FAILED: {
            const inferences = { ...state.inferences };
            delete inferences[action.payload.taskID];

105 106
            return {
                ...state,
107
                inferences,
108 109
            };
        }
110 111 112
        case AuthActionTypes.LOGOUT_SUCCESS: {
            return {
                ...defaultState,
B
Boris Sekachev 已提交
113
            };
114
        }
115 116 117 118 119 120 121
        default: {
            return {
                ...state,
            };
        }
    }
}