1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 21:06:42 +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

@ -58,6 +58,7 @@ import { messagesSet } from '~/redux/messages/actions';
import { SagaIterator } from 'redux-saga';
import { isEmpty } from 'ramda';
import { AxiosError } from 'axios';
import { labGetUpdates } from '~/redux/lab/actions';
function* setTokenSaga({ token }: ReturnType<typeof authSetToken>) {
localStorage.setItem('token', token);
@ -193,6 +194,7 @@ function* getUpdates() {
function* startPollingSaga() {
while (true) {
yield call(getUpdates);
yield put(labGetUpdates());
yield delay(60000);
}
}

View file

@ -2,6 +2,10 @@ import { FLOW_ACTIONS } from './constants';
import { IFlowState } from './reducer';
import { INode } from '../types';
export const flowGetFlow = () => ({
type: FLOW_ACTIONS.GET_FLOW,
});
export const flowSetNodes = (nodes: IFlowState['nodes']) => ({
nodes,
type: FLOW_ACTIONS.SET_NODES,
@ -50,3 +54,8 @@ export const flowChangeSearch = (search: Partial<IFlowState['search']>) => ({
export const flowLoadMoreSearch = () => ({
type: FLOW_ACTIONS.LOAD_MORE_SEARCH,
});
export const flowSeenNode = (nodeId: INode['id']) => ({
type: FLOW_ACTIONS.SEEN_NODE,
nodeId,
});

View file

@ -14,4 +14,6 @@ export const FLOW_ACTIONS = {
SET_SEARCH: `${prefix}SET_SEARCH`,
CHANGE_SEARCH: `${prefix}CHANGE_SEARCH`,
LOAD_MORE_SEARCH: `${prefix}LOAD_MORE_SEARCH`,
SEEN_NODE: `${prefix}SEEN_NODE`,
};

View file

@ -16,6 +16,8 @@ import { Unwrap } from '../types';
import { selectFlow, selectFlowNodes } from './selectors';
import { getSearchResults, postCellView } from './api';
import { uniq } from 'ramda';
import { labSeenNode, labSetUpdates } from '~/redux/lab/actions';
import { selectLabUpdatesNodes } from '~/redux/lab/selectors';
function hideLoader() {
const loader = document.getElementById('main_loader');
@ -185,10 +187,16 @@ function* loadMoreSearch() {
}
}
function* seenNode({ nodeId }: ReturnType<typeof labSeenNode>) {
const { updated }: ReturnType<typeof selectFlow> = yield select(selectFlow);
yield put(flowSetUpdated(updated.filter(node => node.id != nodeId)));
}
export default function* nodeSaga() {
yield takeLatest([FLOW_ACTIONS.GET_FLOW, REHYDRATE], onGetFlow);
yield takeLatest(FLOW_ACTIONS.SET_CELL_VIEW, onSetCellView);
yield takeLeading(FLOW_ACTIONS.GET_MORE, getMore);
yield takeLatest(FLOW_ACTIONS.CHANGE_SEARCH, changeSearch);
yield takeLatest(FLOW_ACTIONS.LOAD_MORE_SEARCH, loadMoreSearch);
yield takeLatest(FLOW_ACTIONS.SEEN_NODE, seenNode);
}

View file

@ -3,3 +3,4 @@ import { IFlowState } from './reducer';
export const selectFlow = (state: IState): IFlowState => state.flow;
export const selectFlowNodes = (state: IState) => state.flow.nodes;
export const selectFlowUpdated = (state: IState) => state.flow.updated;

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[];
};

View file

@ -177,13 +177,6 @@ function* onNodeLoad({ id }: ReturnType<typeof nodeLoadNode>) {
})
);
} catch {}
// Remove current node from recently updated
const { updated } = yield select(selectFlow);
if (updated.some(item => item.id === id)) {
yield put(flowSetUpdated(updated.filter(item => item.id !== id)));
}
}
function* onPostComment({ nodeId, comment, callback }: ReturnType<typeof nodePostLocalComment>) {