1
0
Fork 0
mirror of https://github.com/muerwre/vault-frontend.git synced 2025-04-25 12:56:41 +07:00

#23 added lab node layout (sample)

This commit is contained in:
Fedor Katurov 2021-03-12 13:56:23 +07:00
parent 18ec220a4e
commit 3aa2d4f609
18 changed files with 218 additions and 38 deletions

12
src/redux/lab/actions.ts Normal file
View file

@ -0,0 +1,12 @@
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { ILabState } from '~/redux/lab/types';
export const labGetList = (after?: string) => ({
type: LAB_ACTIONS.GET_LIST,
after,
});
export const labSetList = (list: Partial<ILabState['list']>) => ({
type: LAB_ACTIONS.SET_LIST,
list,
});

8
src/redux/lab/api.ts Normal file
View file

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

View file

@ -0,0 +1,6 @@
const prefix = 'LAB.';
export const LAB_ACTIONS = {
GET_LIST: `${prefix}GET_LIST`,
SET_LIST: `${prefix}SET_LIST`,
};

20
src/redux/lab/handlers.ts Normal file
View file

@ -0,0 +1,20 @@
import { LAB_ACTIONS } from '~/redux/lab/constants';
import { labSetList } from '~/redux/lab/actions';
import { ILabState } from '~/redux/lab/types';
type LabHandler<T extends (...args: any) => any> = (
state: Readonly<ILabState>,
payload: ReturnType<T>
) => Readonly<ILabState>;
const setList: LabHandler<typeof labSetList> = (state, { list }) => ({
...state,
list: {
...state.list,
...list,
},
});
export const LAB_HANDLERS = {
[LAB_ACTIONS.SET_LIST]: setList,
};

14
src/redux/lab/index.ts Normal file
View file

@ -0,0 +1,14 @@
import { createReducer } from '~/utils/reducer';
import { LAB_HANDLERS } from '~/redux/lab/handlers';
import { ILabState } from '~/redux/lab/types';
const INITIAL_STATE: ILabState = {
list: {
is_loading: false,
nodes: [],
count: 0,
error: '',
},
};
export default createReducer(INITIAL_STATE, LAB_HANDLERS);

21
src/redux/lab/sagas.ts Normal file
View file

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

View file

@ -0,0 +1,4 @@
import { IState } from '~/redux/store';
export const selectLab = (state: IState) => state.lab;
export const selectLabListNodes = (state: IState) => state.lab.list.nodes;

19
src/redux/lab/types.ts Normal file
View file

@ -0,0 +1,19 @@
import { IError, INode } from '~/redux/types';
export type ILabState = Readonly<{
list: {
is_loading: boolean;
nodes: INode[];
count: number;
error: IError;
};
}>;
export type GetLabNodesRequest = {
after?: string;
};
export type GetLabNodesResult = {
nodes: INode[];
count: number;
};

View file

@ -17,6 +17,10 @@ import nodeSaga from '~/redux/node/sagas';
import flow, { IFlowState } from '~/redux/flow/reducer';
import flowSaga from '~/redux/flow/sagas';
import lab from '~/redux/lab';
import labSaga from '~/redux/lab/sagas';
import { ILabState } from '~/redux/lab/types';
import uploads, { IUploadState } from '~/redux/uploads/reducer';
import uploadSaga from '~/redux/uploads/sagas';
@ -69,6 +73,7 @@ export interface IState {
boris: IBorisState;
messages: IMessagesState;
tag: ITagState;
lab: ILabState;
}
export const sagaMiddleware = createSagaMiddleware();
@ -93,6 +98,7 @@ export const store = createStore(
player: persistReducer(playerPersistConfig, player),
messages,
tag: tag,
lab: lab,
}),
composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
);
@ -110,6 +116,7 @@ export function configureStore(): {
sagaMiddleware.run(borisSaga);
sagaMiddleware.run(messagesSaga);
sagaMiddleware.run(tagSaga);
sagaMiddleware.run(labSaga);
window.addEventListener('message', message => {
if (message && message.data && message.data.type === 'oauth_login' && message.data.token)