1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-28 06:16:39 +07:00

added updates everywhere

This commit is contained in:
Fedor Katurov 2021-04-02 17:49:03 +07:00
parent a451e30499
commit b1e68a8a6d
27 changed files with 246 additions and 79 deletions

View file

@ -1,5 +1,6 @@
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { ILabState } from '~/redux/lab/types';
import { INode } from '~/redux/types';
export const labGetList = (after?: string) => ({
type: LAB_ACTIONS.GET_LIST,
@ -19,3 +20,17 @@ export const labSetStats = (stats: Partial<ILabState['stats']>) => ({
type: LAB_ACTIONS.SET_STATS,
stats,
});
export const labSetUpdates = (updates: Partial<ILabState['updates']>) => ({
type: LAB_ACTIONS.SET_UPDATES,
updates,
});
export const labGetUpdates = () => ({
type: LAB_ACTIONS.GET_UPDATES,
});
export const labSeenNode = (nodeId: INode['id']) => ({
type: LAB_ACTIONS.SEEN_NODE,
nodeId,
});

View file

@ -1,6 +1,11 @@
import { api, cleanResult } from '~/utils/api';
import { API } from '~/constants/api';
import { GetLabNodesRequest, GetLabNodesResult, GetLabStatsResult } from '~/redux/lab/types';
import {
GetLabNodesRequest,
GetLabNodesResult,
GetLabStatsResult,
GetLabUpdatesResult,
} from '~/redux/lab/types';
export const getLabNodes = ({ after }: GetLabNodesRequest) =>
api
@ -8,3 +13,4 @@ export const getLabNodes = ({ after }: GetLabNodesRequest) =>
.then(cleanResult);
export const getLabStats = () => api.get<GetLabStatsResult>(API.LAB.STATS).then(cleanResult);
export const getLabUpdates = () => api.get<GetLabUpdatesResult>(API.LAB.UPDATES).then(cleanResult);

View file

@ -6,4 +6,8 @@ export const LAB_ACTIONS = {
GET_STATS: `${prefix}GET_STATS`,
SET_STATS: `${prefix}SET_STATS`,
SET_UPDATES: `${prefix}SET_UPDATES`,
GET_UPDATES: `${prefix}GET_UPDATES`,
SEEN_NODE: `${prefix}SET_UPDATE_SEEN`,
};

View file

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

View file

@ -16,6 +16,10 @@ const INITIAL_STATE: ILabState = {
tags: [],
error: undefined,
},
updates: {
nodes: [],
isLoading: false,
},
};
export default createReducer(INITIAL_STATE, LAB_HANDLERS);

View file

@ -1,8 +1,15 @@
import { takeLeading, call, put } from 'redux-saga/effects';
import { labGetList, labSetList, labSetStats } from '~/redux/lab/actions';
import { takeLeading, call, put, select } from 'redux-saga/effects';
import {
labGetList,
labSetList,
labSetStats,
labSetUpdates,
labSeenNode,
} from '~/redux/lab/actions';
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { Unwrap } from '~/redux/types';
import { getLabNodes, getLabStats } from '~/redux/lab/api';
import { getLabNodes, getLabStats, getLabUpdates } from '~/redux/lab/api';
import { selectLabUpdatesNodes } from '~/redux/lab/selectors';
function* getList({ after = '' }: ReturnType<typeof labGetList>) {
try {
@ -28,7 +35,28 @@ function* getStats() {
}
}
function* getUpdates() {
try {
yield put(labSetUpdates({ isLoading: true }));
const { nodes }: Unwrap<typeof getLabUpdates> = yield call(getLabUpdates);
yield put(labSetUpdates({ nodes }));
} catch (error) {
console.log(error.message);
} finally {
yield put(labSetUpdates({ isLoading: false }));
}
}
function* seenNode({ nodeId }: ReturnType<typeof labSeenNode>) {
const nodes: ReturnType<typeof selectLabUpdatesNodes> = yield select(selectLabUpdatesNodes);
const newNodes = nodes.filter(node => node.id != nodeId);
yield put(labSetUpdates({ nodes: newNodes }));
}
export default function* labSaga() {
yield takeLeading(LAB_ACTIONS.GET_LIST, getList);
yield takeLeading(LAB_ACTIONS.GET_STATS, getStats);
yield takeLeading(LAB_ACTIONS.GET_UPDATES, getUpdates);
yield takeLeading(LAB_ACTIONS.SEEN_NODE, seenNode);
}

View file

@ -6,3 +6,5 @@ export const selectLabList = (state: IState) => state.lab.list;
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;
export const selectLabUpdates = (state: IState) => state.lab.updates;
export const selectLabUpdatesNodes = (state: IState) => state.lab.updates.nodes;

View file

@ -13,6 +13,10 @@ export type ILabState = Readonly<{
tags: ITag[];
error?: string;
};
updates: {
nodes: INode[];
isLoading: boolean;
};
}>;
export type GetLabNodesRequest = {
@ -34,3 +38,7 @@ export type GetLabStatsResult = {
heroes: INode[];
tags: ITag[];
};
export type GetLabUpdatesResult = {
nodes: INode[];
};