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-23 09:02:36 +03:00
|
|
|
},
|
|
|
|
updateNumberNode(state, action: PayloadAction<{value: string, number: number}>) {
|
|
|
|
state.nodes = state.nodes.map((e) => {
|
|
|
|
var operation = '=';
|
|
|
|
if (e.value[0] == '>' && e.value[1] == '=') operation = '>='
|
|
|
|
if (e.value[0] == '<' && e.value[1] == '=') operation = '<='
|
|
|
|
return e.value == e.value ? {
|
|
|
|
type: e.type,
|
|
|
|
value: operation + action.payload.number.toString()
|
|
|
|
} : e
|
|
|
|
})
|
|
|
|
},
|
|
|
|
updateOperationNode(state, action: PayloadAction<{value: string, operation: string}>) {
|
|
|
|
state.nodes = state.nodes.map((e) => {
|
|
|
|
var value = 0
|
|
|
|
if (action.payload.operation.length == 2) {
|
|
|
|
value = Number.parseInt(e.value.slice(2, e.value.length))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = Number.parseInt(e.value.slice(1, e.value.length))
|
|
|
|
}
|
|
|
|
return e.value == e.value ? {
|
|
|
|
type: e.type,
|
|
|
|
value: action.payload.operation + value
|
|
|
|
} : e
|
|
|
|
})
|
2022-10-23 00:17:15 +03:00
|
|
|
}
|
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 09:02:36 +03:00
|
|
|
export const {
|
|
|
|
setCurrentWord,
|
|
|
|
createNode,
|
|
|
|
deleteNode,
|
|
|
|
setLoading,
|
|
|
|
updateNumberNode,
|
|
|
|
updateOperationNode
|
|
|
|
} = 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;
|