2024-06-27 16:06:39 +02:00
|
|
|
import { createSelector, createSlice, PayloadAction } from '@reduxjs/toolkit';
|
2024-07-06 00:00:08 +02:00
|
|
|
import { Id, Product } from '@/lib/product';
|
|
|
|
import { dimensions_t, ProductData } from "@/lib/dimensions_t";
|
2024-06-27 16:06:39 +02:00
|
|
|
import uuid from "react-native-uuid";
|
|
|
|
import { RootState } from '@/app/store';
|
2024-07-02 17:34:23 +02:00
|
|
|
import { Length } from 'convert';
|
2024-06-27 16:06:39 +02:00
|
|
|
|
|
|
|
const initialState = {
|
2024-06-30 18:37:27 +02:00
|
|
|
products: [] as ProductData[],
|
2024-07-02 17:34:23 +02:00
|
|
|
units: "ft",
|
|
|
|
};
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export type UpdateAttribute = {
|
|
|
|
product_id: Id,
|
|
|
|
attributeKey: string,
|
|
|
|
attributeValue: any,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type UpdateAttributeKey = {
|
|
|
|
product_id: Id,
|
|
|
|
oldKey: string,
|
|
|
|
newKey: string,
|
|
|
|
}
|
|
|
|
|
|
|
|
export type AddAttribute = {
|
|
|
|
product_id: Id,
|
|
|
|
}
|
|
|
|
|
|
|
|
const cp = (obj: any) => JSON.parse(JSON.stringify(obj));
|
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
const productsState = createSlice({
|
|
|
|
name: 'products-slice',
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
2024-07-02 17:34:23 +02:00
|
|
|
setUnits(state, action : PayloadAction<Length>) {
|
|
|
|
state.units = action.payload;
|
|
|
|
},
|
2024-06-30 18:37:27 +02:00
|
|
|
createProduct(state, action: PayloadAction<ProductData>) {
|
2024-06-27 16:06:39 +02:00
|
|
|
if (!state) {
|
|
|
|
return initialState
|
|
|
|
}
|
|
|
|
const product = action.payload;
|
|
|
|
if (!product.id) product.id = uuid.v4().toString();
|
|
|
|
state.products = [...state.products, action.payload];
|
|
|
|
return state;
|
|
|
|
},
|
2024-06-30 18:37:27 +02:00
|
|
|
deleteProduct(state, action: PayloadAction<Id>) {
|
2024-06-27 16:06:39 +02:00
|
|
|
if (!state) return initialState;
|
2024-06-30 18:37:27 +02:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: [...state.products.filter((prod) => {
|
|
|
|
return prod.id?.valueOf() !== action.payload.valueOf();
|
|
|
|
})],
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
2024-06-30 18:37:27 +02:00
|
|
|
},
|
|
|
|
updateAttribute(state, action: PayloadAction<UpdateAttribute>) {
|
|
|
|
const { product_id, attributeKey, attributeValue } = action.payload
|
|
|
|
if (!state) return initialState;
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
const attributes = cp(prod.attributes);
|
|
|
|
attributes[attributeKey] = attributeValue;
|
|
|
|
return {
|
|
|
|
...prod,
|
|
|
|
attributes,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
},
|
|
|
|
changeKey(state, action: PayloadAction<UpdateAttributeKey>) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
const { product_id, oldKey, newKey } = action.payload
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
|
|
|
|
const attributes = cp(prod.attributes);
|
|
|
|
attributes[newKey] = attributes[oldKey];
|
|
|
|
delete attributes[oldKey];
|
|
|
|
attributes.id = prod.id;
|
|
|
|
return {
|
|
|
|
...prod,
|
|
|
|
attributes,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
},
|
|
|
|
addAttribute(state, action: PayloadAction<Id>) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
const product_id = action.payload;
|
|
|
|
state.products = state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
const i = (Object.keys(prod.attributes || {}).filter(k => k.match(/attribute [\d]+/)) || []).length;
|
|
|
|
const newAttribute = `attribute ${i + 1}`;
|
|
|
|
return {
|
|
|
|
...prod,
|
|
|
|
attributes: {
|
|
|
|
...prod.attributes,
|
|
|
|
[newAttribute]: `value`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2024-06-27 16:06:39 +02:00
|
|
|
return state;
|
|
|
|
},
|
2024-06-30 18:37:27 +02:00
|
|
|
|
|
|
|
deleteAttribute(state, action: PayloadAction<{ product_id: Id, attribute: string }>) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
const { product_id, attribute } = action.payload;
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
const attributes = Object.fromEntries(Object.entries(prod).filter(([k, v]) => (k !== attribute)));
|
|
|
|
return {
|
|
|
|
...prod,
|
|
|
|
attributes,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
updatePrice(state, action: PayloadAction<{ product_id: Id, pricePerUnit: number }>) {
|
2024-06-27 16:06:39 +02:00
|
|
|
if (!state) return initialState;
|
2024-06-30 18:37:27 +02:00
|
|
|
const { product_id, pricePerUnit } = action.payload;
|
|
|
|
state.products = state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
prod.pricePerUnit = pricePerUnit;
|
|
|
|
return prod;
|
|
|
|
});
|
2024-06-27 16:06:39 +02:00
|
|
|
return state;
|
2024-06-30 18:37:27 +02:00
|
|
|
},
|
|
|
|
updateDimensions(state, action: PayloadAction<{ product_id: Id, dimensions: dimensions_t }>) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
const { product_id, dimensions } = action.payload;
|
|
|
|
console.log("Changing dimensions: %o", action.payload);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: state.products.map(prod => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
return {
|
|
|
|
...prod,
|
|
|
|
dimensions,
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
export const selectProductsDatas = (state: RootState) => {
|
|
|
|
return state.products;
|
2024-06-27 16:06:39 +02:00
|
|
|
}
|
|
|
|
|
2024-07-02 17:34:23 +02:00
|
|
|
export const selectUnits = (state : RootState) => {
|
|
|
|
return (state.units || "ft") as Length;
|
|
|
|
}
|
|
|
|
|
2024-07-01 21:23:45 +02:00
|
|
|
export const selectProducts = createSelector([selectProductsDatas], productsData => {
|
|
|
|
return productsData.map(d => Product.fromObject(d));
|
|
|
|
})
|
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export const selectProductIds = createSelector([selectProducts], products => {
|
|
|
|
return products.map(p => p.id);
|
|
|
|
})
|
2024-06-29 15:09:22 +02:00
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export const selectProductAttributes = createSelector([selectProducts], products => {
|
|
|
|
return Object.fromEntries(products.map(p => {
|
|
|
|
return [
|
|
|
|
p.id,
|
|
|
|
p.attributesAsList,
|
|
|
|
]
|
|
|
|
}))
|
|
|
|
})
|
2024-06-27 16:06:39 +02:00
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
...productsState.actions
|
|
|
|
};
|
|
|
|
|
|
|
|
export const {
|
2024-07-02 17:34:23 +02:00
|
|
|
setUnits,
|
2024-06-27 16:06:39 +02:00
|
|
|
createProduct,
|
|
|
|
deleteProduct,
|
2024-06-30 18:37:27 +02:00
|
|
|
changeKey,
|
|
|
|
updateAttribute,
|
|
|
|
addAttribute,
|
|
|
|
deleteAttribute,
|
|
|
|
updatePrice,
|
|
|
|
updateDimensions,
|
2024-06-27 16:06:39 +02:00
|
|
|
} = productsState.actions;
|
|
|
|
|
|
|
|
export default productsState.reducer;
|