You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
73 lines
2.2 KiB
import { createAsyncThunk, createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
import { KTJOrg, RailType } from '../../services/apiTypes';
|
|
import { labeledKtjOrgs } from '../../utils/helper';
|
|
|
|
import { ktjOrgs } from '../../utils/constant';
|
|
import Bridge from '../../utils/bridge';
|
|
import { RootState } from '..';
|
|
|
|
interface BaseDataState {
|
|
ktjOrgs: KTJOrg[];
|
|
railTypes: RailType[];
|
|
syncingBaseData: boolean;
|
|
}
|
|
|
|
const initialState: BaseDataState = {
|
|
ktjOrgs: ktjOrgs as KTJOrg[],
|
|
railTypes: [],
|
|
syncingBaseData: false,
|
|
};
|
|
|
|
export const fetchRailTypes = createAsyncThunk('base/fetchRailTypes', async (_, thunkAPI) => {
|
|
const res = await Bridge.getBasicTrackList();
|
|
return res.success ? res.data : null;
|
|
});
|
|
|
|
export const syncBaseData = createAsyncThunk('base/syncBaseData', async () => {
|
|
return await Bridge.syncBaseData();
|
|
});
|
|
|
|
export const fetchOrgTree = createAsyncThunk('base/fetchOrgTree', async () => {
|
|
return await Bridge.getOrgTree();
|
|
});
|
|
|
|
export const baseDataSlice = createSlice({
|
|
name: 'baseData',
|
|
initialState,
|
|
reducers: {
|
|
updateRailTypes: (state, action: PayloadAction<RailType[]>) => {
|
|
state.railTypes = action.payload;
|
|
},
|
|
updateRailPoints: (state, action: PayloadAction<RailType>) => {
|
|
const r = state.railTypes.find((r) => r.code === action.payload.code);
|
|
if (r) {
|
|
r.points = action.payload.points;
|
|
r.calPoints = action.payload.calPoints;
|
|
}
|
|
},
|
|
},
|
|
extraReducers: (builder) => {
|
|
builder.addCase(fetchRailTypes.fulfilled, (state, action) => {
|
|
state.railTypes = action.payload || [];
|
|
});
|
|
builder.addCase(syncBaseData.pending, (state) => {
|
|
state.syncingBaseData = true;
|
|
});
|
|
builder.addCase(syncBaseData.fulfilled, (state) => {
|
|
state.syncingBaseData = false;
|
|
});
|
|
builder.addCase(fetchOrgTree.fulfilled, (state, action) => {
|
|
if (action.payload.success) {
|
|
state.ktjOrgs = action.payload.data;
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
export const { updateRailPoints } = baseDataSlice.actions;
|
|
export default baseDataSlice.reducer;
|
|
|
|
export const selectKtjOrgs = (state: RootState) => state.baseData.ktjOrgs;
|
|
export const selectLabeledKtjOrgs = createSelector(selectKtjOrgs, (orgs) => {
|
|
return labeledKtjOrgs(orgs);
|
|
});
|