1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-28 06:16:39 +07:00
This commit is contained in:
Fedor Katurov 2021-03-22 15:56:35 +07:00
parent 9745b895f1
commit 11fd582453
23 changed files with 328 additions and 101 deletions

View file

@ -10,3 +10,12 @@ export const labSetList = (list: Partial<ILabState['list']>) => ({
type: LAB_ACTIONS.SET_LIST,
list,
});
export const labGetStats = () => ({
type: LAB_ACTIONS.GET_STATS,
});
export const labSetStats = (stats: Partial<ILabState['stats']>) => ({
type: LAB_ACTIONS.SET_STATS,
stats,
});

View file

@ -1,8 +1,10 @@
import { api, cleanResult } from '~/utils/api';
import { API } from '~/constants/api';
import { GetLabNodesRequest, GetLabNodesResult } from '~/redux/lab/types';
import { GetLabNodesRequest, GetLabNodesResult, GetLabStatsResult } from '~/redux/lab/types';
export const getLabNodes = ({ after }: GetLabNodesRequest) =>
api
.get<GetLabNodesResult>(API.LAB.NODES, { params: { after } })
.then(cleanResult);
export const getLabStats = () => api.get<GetLabStatsResult>(API.LAB.STATS).then(cleanResult);

View file

@ -3,4 +3,7 @@ const prefix = 'LAB.';
export const LAB_ACTIONS = {
GET_LIST: `${prefix}GET_LIST`,
SET_LIST: `${prefix}SET_LIST`,
GET_STATS: `${prefix}GET_STATS`,
SET_STATS: `${prefix}SET_STATS`,
};

View file

@ -1,5 +1,5 @@
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { labSetList } from '~/redux/lab/actions';
import { labSetList, labSetStats } from '~/redux/lab/actions';
import { ILabState } from '~/redux/lab/types';
type LabHandler<T extends (...args: any) => any> = (
@ -15,6 +15,15 @@ const setList: LabHandler<typeof labSetList> = (state, { list }) => ({
},
});
const setStats: LabHandler<typeof labSetStats> = (state, { stats }) => ({
...state,
stats: {
...state.stats,
...stats,
},
});
export const LAB_HANDLERS = {
[LAB_ACTIONS.SET_LIST]: setList,
[LAB_ACTIONS.SET_STATS]: setStats,
};

View file

@ -1,6 +1,7 @@
import { createReducer } from '~/utils/reducer';
import { LAB_HANDLERS } from '~/redux/lab/handlers';
import { ILabState } from '~/redux/lab/types';
import { INode, ITag } from '~/redux/types';
const INITIAL_STATE: ILabState = {
list: {
@ -9,6 +10,12 @@ const INITIAL_STATE: ILabState = {
count: 0,
error: '',
},
stats: {
is_loading: false,
heroes: [],
tags: [],
error: undefined,
},
};
export default createReducer(INITIAL_STATE, LAB_HANDLERS);

View file

@ -1,8 +1,8 @@
import { takeLeading, call, put } from 'redux-saga/effects';
import { labGetList, labSetList } from '~/redux/lab/actions';
import { labGetList, labSetList, labSetStats } from '~/redux/lab/actions';
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { Unwrap } from '~/redux/types';
import { getLabNodes } from '~/redux/lab/api';
import { getLabNodes, getLabStats } from '~/redux/lab/api';
function* getList({ after = '' }: ReturnType<typeof labGetList>) {
try {
@ -16,6 +16,19 @@ function* getList({ after = '' }: ReturnType<typeof labGetList>) {
}
}
function* getStats() {
try {
yield put(labSetStats({ is_loading: true }));
const { heroes, tags }: Unwrap<typeof getLabStats> = yield call(getLabStats);
yield put(labSetStats({ heroes, tags }));
} catch (error) {
yield put(labSetStats({ error: error.message }));
} finally {
yield put(labSetStats({ is_loading: false }));
}
}
export default function* labSaga() {
yield takeLeading(LAB_ACTIONS.GET_LIST, getList);
yield takeLeading(LAB_ACTIONS.GET_STATS, getStats);
}

View file

@ -2,3 +2,6 @@ import { IState } from '~/redux/store';
export const selectLab = (state: IState) => state.lab;
export const selectLabListNodes = (state: IState) => state.lab.list.nodes;
export const selectLabStatsHeroes = (state: IState) => state.lab.stats.heroes;
export const selectLabStatsTags = (state: IState) => state.lab.stats.tags;
export const selectLabStatsLoading = (state: IState) => state.lab.stats.is_loading;

View file

@ -1,4 +1,4 @@
import { IError, INode } from '~/redux/types';
import { IError, INode, ITag } from '~/redux/types';
export type ILabState = Readonly<{
list: {
@ -7,6 +7,12 @@ export type ILabState = Readonly<{
count: number;
error: IError;
};
stats: {
is_loading: boolean;
heroes: Partial<INode>[];
tags: ITag[];
error?: string;
};
}>;
export type GetLabNodesRequest = {
@ -17,3 +23,8 @@ export type GetLabNodesResult = {
nodes: INode[];
count: number;
};
export type GetLabStatsResult = {
heroes: INode[];
tags: ITag[];
};