frontend/store/reducers/nodesInputReducer.ts

84 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-10-22 10:37:05 +03:00
import {createSlice, PayloadAction, createSelector} from '@reduxjs/toolkit';
import {search, createHints} from './asyncActions';
export interface INode{
2022-10-22 19:11:56 +03:00
type: "All" | "Category" | "Name" | string;
2022-10-22 10:37:05 +03:00
value: string;
}
export interface IHint{
2022-10-22 19:11:56 +03:00
value: INode;
2022-10-22 10:37:05 +03:00
coordinate: number;
}
export interface IProduct{
name: string;
2022-10-22 21:01:16 +03:00
id:number,
score:number,
characteristic: {
2022-10-22 10:37:05 +03:00
name: string;
value: string;
}[];
}
interface INodesInput {
nodes: INode[],
hints: {
coordinate: number,
2022-10-22 19:11:56 +03:00
value: INode
2022-10-22 10:37:05 +03:00
}[],
current_word: string,
2022-10-23 00:17:15 +03:00
products: IProduct[],
loading: boolean
2022-10-22 10:37:05 +03:00
}
const initialState: INodesInput = {
nodes: [],
hints: [],
current_word: "",
2022-10-23 00:17:15 +03:00
products: [],
loading:false
2022-10-22 10:37:05 +03:00
}
const nodesInputSlice = createSlice({
name: 'nodesInput',
initialState,
reducers: {
setCurrentWord(state, action: PayloadAction<string>) {
state.current_word = action.payload;
},
createNode(state, action: PayloadAction<INode>) {
state.nodes = state.nodes.concat([action.payload]);
2022-10-22 22:44:45 +03:00
},
deleteNode(state, action: PayloadAction<string>) {
state.nodes = state.nodes.filter((e) => e.value != action.payload)
},
2022-10-23 00:17:15 +03:00
setLoading(state, action: PayloadAction<boolean>){
state.loading = action.payload
}
2022-10-22 22:44:45 +03:00
2022-10-22 10:37:05 +03:00
},
extraReducers: (builder) => {
builder.addCase(search.fulfilled, (state, action) => {
state.products = action.payload;
2022-10-23 00:17:15 +03:00
state.loading = false
2022-10-22 10:37:05 +03:00
})
builder.addCase(createHints.fulfilled, (state, action) => {
state.hints = action.payload;
})
}
})
2022-10-23 00:17:15 +03:00
export const {setCurrentWord, createNode, deleteNode, setLoading} = nodesInputSlice.actions;
2022-10-22 10:37:05 +03:00
export const hints = createSelector((state: INodesInput) => state.hints, hints => hints)
export const currentWord = createSelector((state: INodesInput) => state.current_word, word => word)
export const products = createSelector((state: INodesInput) => state.products, products => products)
2022-10-22 22:44:45 +03:00
export const nodes = createSelector((state: INodesInput) => state.nodes, nodes => nodes)
2022-10-23 00:17:15 +03:00
export const loading = createSelector((state: INodesInput) => state.loading, loading => loading)
2022-10-22 10:37:05 +03:00
export default nodesInputSlice.reducer;