2024-08-15 23:07:19 +02:00
|
|
|
import { createSelector, createSlice, PayloadAction } from "@reduxjs/toolkit";
|
|
|
|
import { attributesAsList, Id, Product } from "@/lib/product";
|
|
|
|
import { dimensions_t, length_t } from "@/lib/dimensions";
|
2024-06-27 16:06:39 +02:00
|
|
|
import uuid from "react-native-uuid";
|
2024-08-15 23:07:19 +02:00
|
|
|
import { AppStore, RootState } from "@/app/store";
|
|
|
|
import { Length } from "convert";
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
export const DEFAULT_UNITS: Length = "ft";
|
|
|
|
|
|
|
|
export type PlywoodCalculationState = {
|
|
|
|
product?: Id | null;
|
|
|
|
units: Length;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type CarpetCalculationState = {
|
|
|
|
product?: Id | null;
|
|
|
|
length: length_t;
|
|
|
|
inner_d: length_t;
|
|
|
|
outer_d: length_t;
|
2024-07-02 17:34:23 +02:00
|
|
|
};
|
2024-06-27 16:06:39 +02:00
|
|
|
|
2024-08-15 23:07:19 +02:00
|
|
|
export type AppStoreState = {
|
|
|
|
products: Product[];
|
|
|
|
calculations?: {
|
|
|
|
plywood: PlywoodCalculationState;
|
|
|
|
carpet: CarpetCalculationState;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const DEFAULT_PRELOADED_STATE = {
|
|
|
|
products: [] as Product[],
|
|
|
|
calculations: {
|
|
|
|
plywood: {
|
|
|
|
product: undefined,
|
|
|
|
units: DEFAULT_UNITS,
|
|
|
|
},
|
|
|
|
carpet: {
|
|
|
|
product: undefined,
|
|
|
|
length: {
|
|
|
|
l: 0,
|
|
|
|
u: DEFAULT_UNITS,
|
|
|
|
},
|
|
|
|
inner_d: {
|
|
|
|
l: 0,
|
|
|
|
u: DEFAULT_UNITS,
|
|
|
|
},
|
|
|
|
outer_d: {
|
|
|
|
l: 0,
|
|
|
|
u: DEFAULT_UNITS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
} as AppStoreState;
|
|
|
|
|
|
|
|
const initialState: AppStoreState = DEFAULT_PRELOADED_STATE;
|
|
|
|
|
2024-06-30 18:37:27 +02:00
|
|
|
export type UpdateAttribute = {
|
2024-08-15 23:07:19 +02:00
|
|
|
product_id: Id;
|
|
|
|
attributeKey: string;
|
|
|
|
attributeValue: any;
|
|
|
|
};
|
2024-06-30 18:37:27 +02:00
|
|
|
|
|
|
|
export type UpdateAttributeKey = {
|
2024-08-15 23:07:19 +02:00
|
|
|
product_id: Id;
|
|
|
|
oldKey: string;
|
|
|
|
newKey: string;
|
|
|
|
};
|
2024-06-30 18:37:27 +02:00
|
|
|
|
|
|
|
export type AddAttribute = {
|
2024-08-15 23:07:19 +02:00
|
|
|
product_id: Id;
|
|
|
|
};
|
2024-06-30 18:37:27 +02:00
|
|
|
|
|
|
|
const cp = (obj: any) => JSON.parse(JSON.stringify(obj));
|
|
|
|
|
2024-06-27 16:06:39 +02:00
|
|
|
const productsState = createSlice({
|
2024-08-15 23:07:19 +02:00
|
|
|
name: "products-slice",
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
setPlywoodCalculation(
|
|
|
|
state,
|
|
|
|
action: PayloadAction<PlywoodCalculationState>
|
|
|
|
) {
|
|
|
|
if (!state) {
|
|
|
|
return initialState;
|
|
|
|
}
|
|
|
|
const newCalc = action.payload;
|
|
|
|
state.calculations.plywood = newCalc;
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
setCarpetCalculation(state, action: PayloadAction<CarpetCalculationState>) {
|
|
|
|
if (!state) {
|
|
|
|
return initialState;
|
|
|
|
}
|
|
|
|
const newCalc = action.payload;
|
|
|
|
state.calculations.carpet = newCalc;
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
createProduct(state, action: PayloadAction<Product>) {
|
|
|
|
if (!state) {
|
|
|
|
return initialState;
|
|
|
|
}
|
|
|
|
const product = action.payload;
|
|
|
|
if (!product.id) product.id = uuid.v4().toString();
|
|
|
|
state.products = [...state.products, action.payload];
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
deleteProduct(state, action: PayloadAction<Id>) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
products: [
|
|
|
|
...state.products.filter((prod) => {
|
|
|
|
return prod.id?.valueOf() !== action.payload.valueOf();
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
};
|
|
|
|
},
|
|
|
|
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`,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
|
|
|
|
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 }>
|
|
|
|
) {
|
|
|
|
if (!state) return initialState;
|
|
|
|
const { product_id, pricePerUnit } = action.payload;
|
|
|
|
state.products = state.products.map((prod) => {
|
|
|
|
if (prod.id !== product_id) return prod;
|
|
|
|
prod.pricePerUnit = pricePerUnit;
|
|
|
|
return prod;
|
|
|
|
});
|
|
|
|
return state;
|
|
|
|
},
|
|
|
|
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-31 19:01:45 +02:00
|
|
|
export const selectProducts = (state: RootState) => {
|
2024-08-15 23:07:19 +02:00
|
|
|
return state.products;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const selectPlywoodCalc = (state: RootState) => {
|
|
|
|
return state.calculations.plywood;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const selectCarpetCalc = (state: RootState) => {
|
|
|
|
return state.calculations.carpet;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const selectProductIds = createSelector([selectProducts], (products) => {
|
|
|
|
return products.map((p) => p.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
export const selectProductAttributes = createSelector(
|
|
|
|
[selectProducts],
|
|
|
|
(products) => {
|
|
|
|
return Object.fromEntries(
|
|
|
|
products.map((p) => {
|
|
|
|
return [p.id, p.attributes ? attributesAsList(p.attributes) : []];
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2024-06-27 16:06:39 +02:00
|
|
|
|
|
|
|
export const actions = {
|
2024-08-15 23:07:19 +02:00
|
|
|
...productsState.actions,
|
2024-06-27 16:06:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const {
|
2024-08-15 23:07:19 +02:00
|
|
|
setCarpetCalculation,
|
|
|
|
setPlywoodCalculation,
|
|
|
|
createProduct,
|
|
|
|
deleteProduct,
|
|
|
|
changeKey,
|
|
|
|
updateAttribute,
|
|
|
|
addAttribute,
|
|
|
|
deleteAttribute,
|
|
|
|
updatePrice,
|
|
|
|
updateDimensions,
|
2024-06-27 16:06:39 +02:00
|
|
|
} = productsState.actions;
|
|
|
|
|
|
|
|
export default productsState.reducer;
|