mirror of
https://github.com/house-of-vanity/OutFleet.git
synced 2025-10-26 10:09:08 +00:00
feat: added redux & ducks
This commit is contained in:
30
client/src/features/servers/duck/actions.ts
Normal file
30
client/src/features/servers/duck/actions.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { serversSlice } from './slice';
|
||||
import { getServers } from './api';
|
||||
import { createAsyncThunk } from '@reduxjs/toolkit';
|
||||
import { getServersState } from './selectors';
|
||||
import type { RootState } from '../../../store';
|
||||
|
||||
const PREFFIX = 'servers'
|
||||
|
||||
export const fetchServers = createAsyncThunk(
|
||||
`${PREFFIX}/fetchAll`,
|
||||
async (_, { dispatch, getState }) => {
|
||||
const { loading } = getServersState(getState() as RootState);
|
||||
try {
|
||||
if (loading) {
|
||||
return;
|
||||
}
|
||||
|
||||
dispatch(serversSlice.actions.setLoading(true));
|
||||
const response = await getServers().then(({ data }) => data);
|
||||
dispatch(serversSlice.actions.setServers(response));
|
||||
|
||||
} catch (e) {
|
||||
const message =
|
||||
e instanceof Error ? e.message : `Unknown error in ${PREFFIX}/fetchAll`;
|
||||
dispatch(serversSlice.actions.setError(message));
|
||||
} finally {
|
||||
dispatch(serversSlice.actions.setLoading(false));
|
||||
}
|
||||
},
|
||||
);
|
||||
5
client/src/features/servers/duck/api.ts
Normal file
5
client/src/features/servers/duck/api.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import type { AxiosResponse } from 'axios';
|
||||
import { api } from '../../../api/api';
|
||||
import type { ServerDTO } from './dto';
|
||||
|
||||
export const getServers = () => api.get<never, AxiosResponse<ServerDTO[]>>('/servers');
|
||||
7
client/src/features/servers/duck/dto.ts
Normal file
7
client/src/features/servers/duck/dto.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface ServerDTO {
|
||||
id: number | string
|
||||
name: string
|
||||
hostname: string
|
||||
grpc_port: number
|
||||
status: string
|
||||
}
|
||||
4
client/src/features/servers/duck/index.ts
Normal file
4
client/src/features/servers/duck/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './actions'
|
||||
export * from './dto'
|
||||
export * from './slice'
|
||||
export * from './selectors'
|
||||
3
client/src/features/servers/duck/selectors.ts
Normal file
3
client/src/features/servers/duck/selectors.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import type { RootState } from '../../../store';
|
||||
|
||||
export const getServersState = (state: RootState) => state.servers;
|
||||
32
client/src/features/servers/duck/slice.ts
Normal file
32
client/src/features/servers/duck/slice.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { createSlice } from '@reduxjs/toolkit';
|
||||
import type { PayloadAction } from '@reduxjs/toolkit';
|
||||
import type { ServerDTO } from './dto';
|
||||
|
||||
export interface ServersState {
|
||||
loading: boolean;
|
||||
servers: ServerDTO[];
|
||||
error: null | string;
|
||||
}
|
||||
|
||||
const initialState: ServersState = {
|
||||
loading: false,
|
||||
servers: [],
|
||||
error: null,
|
||||
};
|
||||
|
||||
export const serversSlice = createSlice({
|
||||
name: 'servers',
|
||||
initialState,
|
||||
reducers: {
|
||||
setLoading: (state, action: PayloadAction<boolean>) => {
|
||||
state.loading = action.payload
|
||||
},
|
||||
setServers: (state, action: PayloadAction<ServerDTO[]>) => {
|
||||
state.servers = action.payload
|
||||
},
|
||||
setError: (state, action: PayloadAction<string>) => {
|
||||
state.error = action.payload
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user