2023-12-11 17:05:35 +08:00
|
|
|
'use strict';
|
|
|
|
|
2023-12-15 17:26:52 +08:00
|
|
|
import {createWithEqualityFn} from "zustand/traditional";
|
2024-06-04 16:25:04 +08:00
|
|
|
import {DATA as shared_data, itemsUnfoldedList, API_RFQ} from "./utils";
|
2024-07-11 17:30:34 +08:00
|
|
|
import {FillExtCrateData, FillExtOrderData} from "./options/utils";
|
2023-12-11 17:05:35 +08:00
|
|
|
import {v4 as uuidv4} from "uuid";
|
|
|
|
import {FillResources} from "./count_resources";
|
2024-01-30 13:02:01 +08:00
|
|
|
import {FillExtCardData} from "./options/utils";
|
2023-12-11 17:05:35 +08:00
|
|
|
import {TriggerCrateWarnings, TriggerWarnings} from "./warnings";
|
2023-12-13 12:39:15 +08:00
|
|
|
import {Validation, validateEmail, validateNote, validateJSONInput} from "./validate";
|
|
|
|
import {CratesToJSON, JSONToCrates} from "./json_porter";
|
2024-01-30 14:45:30 +08:00
|
|
|
import {ProcessOptionsToData} from "./options/Options";
|
2024-06-04 16:25:04 +08:00
|
|
|
import {DomainedRFQMessages} from "./Domained";
|
2023-12-11 17:05:35 +08:00
|
|
|
|
|
|
|
|
2024-11-01 17:32:20 +08:00
|
|
|
export const HORIZONTAL_CART_MARKER = "_h";
|
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
const cards_to_pn_map = (cards) => {
|
|
|
|
let result = {};
|
|
|
|
Object.entries(cards).forEach(([key, card], _i) => { result[card.name_number] = key})
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2024-07-11 17:30:34 +08:00
|
|
|
const toArray = (arg) => {
|
|
|
|
return Array.isArray(arg) ? arg : [arg];
|
|
|
|
};
|
|
|
|
|
2024-11-01 17:32:20 +08:00
|
|
|
const unwrapCrateId = (crate_id= "") => {
|
|
|
|
return crate_id.endsWith(HORIZONTAL_CART_MARKER) ? [true, crate_id.substring(0, crate_id.length - HORIZONTAL_CART_MARKER.length)] : [false, crate_id]
|
|
|
|
}
|
|
|
|
|
|
|
|
const whichItems = (horizontal = false) => horizontal ? "h_items" : "items"
|
|
|
|
|
2024-03-15 15:22:05 +08:00
|
|
|
const useCatalog = ((set, get) => ({
|
2023-12-12 16:09:29 +08:00
|
|
|
cards: shared_data.items,
|
2024-03-15 15:22:05 +08:00
|
|
|
groups: shared_data.columns.catalog,
|
2023-12-11 17:05:35 +08:00
|
|
|
cards_list: itemsUnfoldedList,
|
2023-12-12 16:09:29 +08:00
|
|
|
currency: shared_data.currency,
|
2023-12-12 18:21:09 +08:00
|
|
|
pn_to_cards: cards_to_pn_map(shared_data.items),
|
2023-12-12 16:09:29 +08:00
|
|
|
getCardDescription: index => get().cards[get().cards_list[index]],
|
2023-12-12 18:21:09 +08:00
|
|
|
getCardDescriptionByPn: pn => get().cards[get().pn_to_cards[pn]],
|
2023-12-12 16:09:29 +08:00
|
|
|
cardIndexById: card_id => get().cards_list.findIndex((element) => (card_id === element))
|
2023-12-11 17:05:35 +08:00
|
|
|
}));
|
|
|
|
|
2024-03-18 14:48:16 +08:00
|
|
|
const useOptionsNotification = ((set, get) => ({
|
|
|
|
notificationCrateId: null,
|
|
|
|
notificationCardIndex: null,
|
2024-11-01 17:32:20 +08:00
|
|
|
notificationHorizontal: false,
|
2024-03-18 14:48:16 +08:00
|
|
|
notificationTimer: null,
|
2024-11-01 17:32:20 +08:00
|
|
|
_showNotification: (crate_id, card_index, horizontal) => set(state => ({
|
2024-03-18 14:48:16 +08:00
|
|
|
notificationCrateId: crate_id,
|
|
|
|
notificationCardIndex: card_index,
|
2024-11-01 17:32:20 +08:00
|
|
|
notificationHorizontal: horizontal,
|
2024-03-18 14:48:16 +08:00
|
|
|
notificationTimer: setTimeout(() => {
|
|
|
|
state.hideNotification()
|
|
|
|
}, 5000)
|
|
|
|
})),
|
2024-11-01 17:32:20 +08:00
|
|
|
showNotification: (crate_id, card_index, horizontal) => {
|
2024-03-18 14:48:16 +08:00
|
|
|
get().hideNotification()
|
2024-11-01 17:32:20 +08:00
|
|
|
setTimeout(() => get()._showNotification(crate_id, card_index, horizontal), 100);
|
2024-03-18 14:48:16 +08:00
|
|
|
},
|
|
|
|
hideNotification: () => set(state => ({
|
|
|
|
notificationCrateId: null,
|
|
|
|
notificationCardIndex: null,
|
2024-11-01 17:32:20 +08:00
|
|
|
notificationHorizontal: false,
|
2024-03-18 14:48:16 +08:00
|
|
|
notificationTimer: (state.notificationTimer && clearTimeout(state.notificationTimer)) || null,
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
|
2024-02-22 17:26:07 +08:00
|
|
|
const useSearch = ((set, get) => ({
|
|
|
|
search_index: Array.from(Object.values(shared_data.items)
|
|
|
|
.map((card, _) => (
|
|
|
|
[(card.name + " " + card.name_number + " " + card.name_codename).toLowerCase(), card.id]
|
|
|
|
))),
|
|
|
|
search_bar_value: "",
|
|
|
|
listed_cards: [],
|
|
|
|
updateSearchBar: text => set(state => ({
|
|
|
|
search_bar_value: text,
|
|
|
|
listed_cards: text.length > 0 ? Array.from(get().search_index
|
|
|
|
.filter((card, _) => card[0].includes(text.toLowerCase()))
|
|
|
|
.map(([index, card_id], _) => get().cards_list.findIndex(elem => elem === card_id))) : []
|
|
|
|
}))
|
|
|
|
}));
|
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
const useCrateModes = ((set, get) => ({
|
2023-12-12 16:09:29 +08:00
|
|
|
crate_modes: shared_data.crateModes,
|
|
|
|
modes_order: shared_data.crateModeOrder,
|
|
|
|
crateParams: mode => get().crate_modes[mode],
|
2023-12-11 17:05:35 +08:00
|
|
|
}));
|
|
|
|
|
2024-01-29 17:26:59 +08:00
|
|
|
const useCrateOptions = ((set, get) => ({
|
|
|
|
crate_options: shared_data.crateOptions.options,
|
|
|
|
crate_prices: shared_data.crateOptions.prices,
|
|
|
|
|
2024-01-30 13:02:01 +08:00
|
|
|
fillExtCrateData: (crate_id) => set(state => ({
|
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (crate_id === crate.id) {
|
|
|
|
const previous_options = crate.options_data || {};
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
options_data: {
|
|
|
|
...previous_options,
|
|
|
|
ext_data: FillExtCrateData(crate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
|
|
|
|
|
|
|
_updateCrateOption: (crate_id, new_options) => set(state => ({
|
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (crate_id === crate.id) {
|
|
|
|
const previous_options = crate.options_data || {};
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
options_data: {
|
|
|
|
...previous_options,
|
|
|
|
...new_options
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
|
|
|
|
|
|
|
updateCrateOptions: (crate_id, new_options) => {
|
|
|
|
get().fillExtCrateData(crate_id);
|
2024-01-30 17:20:29 +08:00
|
|
|
get().fillOrderExtData();
|
2024-01-30 13:02:01 +08:00
|
|
|
get()._updateCrateOption(crate_id, new_options);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2024-01-30 13:02:01 +08:00
|
|
|
}
|
2024-01-29 17:26:59 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
const useOrderOptions = ((set, get) => ({
|
2024-01-30 17:20:29 +08:00
|
|
|
order_options: shared_data.orderOptions.options,
|
|
|
|
order_prices: shared_data.orderOptions.prices,
|
2024-02-02 17:24:01 +08:00
|
|
|
shipping_summary: shared_data.orderOptions.shippingSummary,
|
2024-01-29 17:26:59 +08:00
|
|
|
order_options_data: {},
|
|
|
|
|
2024-01-30 17:20:29 +08:00
|
|
|
fillOrderExtData: () => set(state => ({
|
|
|
|
order_options_data: {
|
|
|
|
...state.order_options_data,
|
|
|
|
ext_data: FillExtOrderData(state.crates, state.modes_order)
|
|
|
|
}
|
|
|
|
})),
|
2024-01-29 17:26:59 +08:00
|
|
|
|
2024-01-30 13:02:01 +08:00
|
|
|
_updateOrderOptions: (new_options) => set(state => ({
|
2024-01-29 17:26:59 +08:00
|
|
|
order_options_data: {
|
|
|
|
...state.order_options_data,
|
|
|
|
...new_options
|
|
|
|
}
|
2024-01-30 13:02:01 +08:00
|
|
|
})),
|
|
|
|
|
|
|
|
updateOrderOptions: (new_options) => {
|
|
|
|
get()._updateOrderOptions(new_options);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2024-01-30 17:20:29 +08:00
|
|
|
get().fillOrderExtData();
|
2024-01-30 13:02:01 +08:00
|
|
|
}
|
2024-01-25 17:10:14 +08:00
|
|
|
}));
|
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
const useLayout = ((set, get) => ({
|
|
|
|
isTouch: window.isTouchEnabled(),
|
|
|
|
isMobile: window.deviceIsMobile(),
|
|
|
|
sideMenuIsOpen: false,
|
2023-12-13 15:17:14 +08:00
|
|
|
showCardAddedFeedback: false,
|
2024-01-05 17:22:53 +08:00
|
|
|
showNoDestination: false,
|
2023-12-13 15:17:14 +08:00
|
|
|
timerAdded: null,
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2024-03-19 11:10:53 +08:00
|
|
|
_switchSideMenu: () => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
sideMenuIsOpen: !state.sideMenuIsOpen
|
|
|
|
})),
|
2024-03-19 11:10:53 +08:00
|
|
|
switchSideMenu: () => {
|
|
|
|
if (!get().sideMenuIsOpen) {
|
|
|
|
get().hideNotification()
|
|
|
|
}
|
|
|
|
get()._switchSideMenu();
|
|
|
|
},
|
2023-12-13 15:17:14 +08:00
|
|
|
cardAdded: () => set(state => ({
|
|
|
|
showCardAddedFeedback: true,
|
2024-01-05 17:22:53 +08:00
|
|
|
showNoDestination: false,
|
2023-12-13 15:17:14 +08:00
|
|
|
timerAdded: (!!state.timerAdded ? clearTimeout(state.timerAdded) : null) || (state.isMobile && setTimeout(() => {
|
|
|
|
get()._endCardAdded()
|
|
|
|
}, 2000))
|
|
|
|
})),
|
2024-01-05 17:22:53 +08:00
|
|
|
noDestinationWarning: () => set(state => ({
|
|
|
|
showCardAddedFeedback: true,
|
|
|
|
showNoDestination: true,
|
|
|
|
timerAdded: (!!state.timerAdded ? clearTimeout(state.timerAdded) : null) || (setTimeout(() => {
|
|
|
|
get()._endCardAdded()
|
|
|
|
}, 2000))
|
|
|
|
})),
|
2023-12-13 15:17:14 +08:00
|
|
|
_endCardAdded: () => set(state => ({
|
|
|
|
showCardAddedFeedback: false,
|
|
|
|
timerAdded: !!state.timerAdded ? clearTimeout(state.timerAdded) : null
|
|
|
|
}))
|
2023-12-13 12:39:15 +08:00
|
|
|
}));
|
|
|
|
|
|
|
|
const useImportJSON = ((set, get) => ({
|
|
|
|
importShouldOpen: false,
|
|
|
|
importValue: {
|
|
|
|
value: "",
|
|
|
|
error: Validation.OK
|
|
|
|
},
|
2023-12-11 17:05:35 +08:00
|
|
|
openImport: () => set(state => ({
|
2023-12-13 12:39:15 +08:00
|
|
|
importShouldOpen: true
|
2023-12-11 17:05:35 +08:00
|
|
|
})),
|
|
|
|
closeImport: () => set(state => ({
|
2023-12-13 12:39:15 +08:00
|
|
|
importShouldOpen: false
|
2023-12-11 17:05:35 +08:00
|
|
|
})),
|
2024-01-08 15:31:22 +08:00
|
|
|
_loadDescription: () => set(state => {
|
|
|
|
const parsed = JSONToCrates(state.importValue.value);
|
|
|
|
// if (parsed.crates[-1].crate_mode !== "")
|
|
|
|
return {
|
2024-01-30 17:20:29 +08:00
|
|
|
importShouldOpen: false,
|
|
|
|
// additional fields go here
|
|
|
|
crates: parsed.crates,
|
|
|
|
order_options_data: parsed.order_options_data
|
2024-01-08 15:31:22 +08:00
|
|
|
}}),
|
2023-12-15 13:49:54 +08:00
|
|
|
loadDescription: () => {
|
|
|
|
get()._loadDescription()
|
2024-01-30 17:20:29 +08:00
|
|
|
get().fillOrderExtData();
|
2023-12-15 13:49:54 +08:00
|
|
|
get().crates.forEach((crate, _i) => {
|
2024-01-30 14:45:30 +08:00
|
|
|
get().fillExtData(crate.id);
|
|
|
|
get().fillWarnings(crate.id);
|
|
|
|
get().fillExtCrateData(crate.id);
|
|
|
|
});
|
|
|
|
get()._updateTotalOrderPrice();
|
2024-11-01 17:32:20 +08:00
|
|
|
get().showNotification(get().active_crate, null, false);
|
2023-12-15 13:49:54 +08:00
|
|
|
},
|
2023-12-13 12:39:15 +08:00
|
|
|
updateImportDescription: (new_description) => set(state => ({
|
|
|
|
importValue: {
|
|
|
|
value: new_description,
|
|
|
|
error: validateJSONInput(new_description)
|
|
|
|
}
|
|
|
|
}))
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-13 12:39:15 +08:00
|
|
|
}));
|
2023-12-11 17:05:35 +08:00
|
|
|
|
|
|
|
const useSubmitForm = ((set, get) => ({
|
|
|
|
isProcessing: false,
|
2023-12-13 12:39:15 +08:00
|
|
|
shouldShowRFQFeedback: false,
|
|
|
|
processingResult: {
|
|
|
|
status: Validation.OK,
|
|
|
|
message: ""
|
|
|
|
},
|
2024-06-04 16:25:04 +08:00
|
|
|
API_RFQ: API_RFQ,
|
2023-12-12 18:21:09 +08:00
|
|
|
email: {
|
|
|
|
value: "",
|
|
|
|
error: null
|
|
|
|
},
|
|
|
|
note: {
|
|
|
|
value: "",
|
|
|
|
error: Validation.OK
|
|
|
|
},
|
|
|
|
|
|
|
|
description: "",
|
|
|
|
shouldShowDescription: false,
|
|
|
|
|
|
|
|
updateEmail: (new_email) => set(state => ({
|
|
|
|
email: {
|
|
|
|
value: new_email,
|
|
|
|
error: validateEmail(new_email)
|
|
|
|
}
|
|
|
|
})),
|
|
|
|
updateNote: (new_notes) => set(state => ({
|
|
|
|
note: {
|
|
|
|
value: new_notes,
|
|
|
|
error: validateNote(new_notes)
|
|
|
|
}
|
|
|
|
})),
|
2023-12-13 15:17:14 +08:00
|
|
|
resetEmailValidation: () => set(state => ({
|
|
|
|
email: {
|
|
|
|
value: state.email.value,
|
|
|
|
error: Validation.OK
|
|
|
|
}
|
|
|
|
})),
|
2023-12-12 18:21:09 +08:00
|
|
|
|
2023-12-13 12:39:15 +08:00
|
|
|
_revalidateForm: () => set(state => ({
|
|
|
|
email: {
|
|
|
|
value: state.email.value,
|
|
|
|
error: validateEmail(state.email.value)
|
|
|
|
},
|
|
|
|
note: {
|
|
|
|
value: state.note.value,
|
|
|
|
error: validateEmail(state.note.value)
|
|
|
|
},
|
|
|
|
})),
|
|
|
|
|
2023-12-12 18:21:09 +08:00
|
|
|
updateDescription: () => set(state => ({
|
|
|
|
description: CratesToJSON(state.crates)
|
|
|
|
})),
|
|
|
|
showDescription: () => set(state => ({
|
|
|
|
description: CratesToJSON(state.crates),
|
|
|
|
shouldShowDescription: true
|
|
|
|
})),
|
|
|
|
closeDescription: () => set(state => ({
|
|
|
|
shouldShowDescription: false
|
|
|
|
})),
|
2023-12-13 12:39:15 +08:00
|
|
|
_submitForm: () => set(state => ({isProcessing: true})),
|
|
|
|
finishSubmitForm: (result) => set(state => ({
|
|
|
|
isProcessing: false,
|
|
|
|
shouldShowRFQFeedback: true,
|
|
|
|
processingResult: result})),
|
|
|
|
|
|
|
|
submitDisabled: () => (get().email.error !== Validation.OK),
|
|
|
|
|
|
|
|
submitForm: () => {
|
|
|
|
get().updateDescription();
|
|
|
|
get()._revalidateForm();
|
|
|
|
get()._submitForm();
|
|
|
|
if (get().submitDisabled()) return;
|
|
|
|
fetch(get().API_RFQ, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {'Content-Type': 'application/json'},
|
|
|
|
body: JSON.stringify({
|
|
|
|
email: get().email.value,
|
|
|
|
note: get().note.value,
|
|
|
|
configuration: get().description
|
|
|
|
})
|
|
|
|
}).then(response => {
|
|
|
|
if (response.status !== 200) {
|
|
|
|
throw Error("Response status is not OK: " + response.status + ".\n" + response);
|
|
|
|
}
|
2024-06-04 16:25:04 +08:00
|
|
|
get().finishSubmitForm({status: Validation.OK, message: DomainedRFQMessages.OK})
|
2024-01-08 17:42:02 +08:00
|
|
|
}, reason => {
|
|
|
|
console.error("Request rejected, reason:", reason)
|
|
|
|
get().finishSubmitForm({
|
|
|
|
status: Validation.Invalid,
|
2024-06-04 16:25:04 +08:00
|
|
|
message: DomainedRFQMessages.ERROR
|
2024-01-08 17:42:02 +08:00
|
|
|
})
|
2023-12-13 12:39:15 +08:00
|
|
|
}).catch(err => {
|
|
|
|
console.error("Request failed, reason:", err)
|
|
|
|
get().finishSubmitForm({
|
|
|
|
status: Validation.Invalid,
|
2024-06-04 16:25:04 +08:00
|
|
|
message: DomainedRFQMessages.ERROR
|
2023-12-13 12:39:15 +08:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
closeRFQFeedback: () => set(state => ({shouldShowRFQFeedback: false}))
|
2023-12-11 17:05:35 +08:00
|
|
|
}));
|
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
const useHighlighted = ((set, get) => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
highlighted: {
|
|
|
|
crate: "",
|
2024-11-01 17:32:20 +08:00
|
|
|
card: 0,
|
|
|
|
horizontal: false
|
2023-12-11 17:05:35 +08:00
|
|
|
},
|
2023-12-13 15:17:14 +08:00
|
|
|
highlightedTimer: null,
|
2024-01-02 17:13:50 +08:00
|
|
|
|
|
|
|
// #!if disable_card_highlight === false
|
2024-11-01 17:32:20 +08:00
|
|
|
highlightCard: (crate_id, index, horizontal) => set(state => ({
|
2023-12-12 16:09:29 +08:00
|
|
|
highlighted: {
|
|
|
|
crate: crate_id,
|
2024-11-01 17:32:20 +08:00
|
|
|
card: index,
|
|
|
|
horizontal: horizontal
|
2023-12-13 15:17:14 +08:00
|
|
|
},
|
|
|
|
highlightedTimer: (!!state.highlightedTimer ? clearTimeout(state.highlightedTimer) : null) || (state.isTouch && setTimeout(() => {
|
|
|
|
get().highlightReset()
|
|
|
|
}, 2000))
|
2023-12-12 16:09:29 +08:00
|
|
|
})),
|
|
|
|
highlightReset: () => set(state => ({
|
|
|
|
highlighted: {
|
|
|
|
crate: "",
|
2024-11-01 17:32:20 +08:00
|
|
|
card: 0,
|
|
|
|
horizontal: false
|
2023-12-13 15:17:14 +08:00
|
|
|
},
|
|
|
|
highlightedTimer: !!state.highlightedTimer ? clearTimeout(state.highlightedTimer) : null
|
2023-12-12 16:09:29 +08:00
|
|
|
})),
|
2024-01-02 17:13:50 +08:00
|
|
|
// #!else
|
|
|
|
highlightCard: () => {return null;},
|
|
|
|
highlightReset: () => {return null;},
|
|
|
|
// #!endif
|
2023-12-12 16:09:29 +08:00
|
|
|
}));
|
|
|
|
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
const useCart = ((set, get) => ({
|
|
|
|
crates: shared_data.columns.crates,
|
|
|
|
active_crate: "crate0",
|
2024-01-15 13:01:27 +08:00
|
|
|
_defaultCrates: Array.from(shared_data.columns.crates),
|
2024-01-30 14:45:30 +08:00
|
|
|
total_order_price: 0,
|
2023-12-11 17:05:35 +08:00
|
|
|
|
2023-12-13 15:17:14 +08:00
|
|
|
_newCrate: (crate_id) => set((state) => ({
|
2024-01-05 17:22:53 +08:00
|
|
|
crates: state.crates.toSpliced(-1, 0, {
|
2024-01-15 13:01:27 +08:00
|
|
|
...state._defaultCrates[0],
|
2023-12-12 16:09:29 +08:00
|
|
|
id: crate_id || "crate" + state.crates.length,
|
2023-12-13 15:17:14 +08:00
|
|
|
}),
|
|
|
|
active_crate: crate_id || "crate" + state.crates.length
|
|
|
|
})),
|
2024-01-30 17:20:29 +08:00
|
|
|
_delCrate: (id) => set(state => ({
|
2024-01-15 13:01:27 +08:00
|
|
|
crates: state.crates.filter((crate => crate.id !== id || !state.modes_order.includes(crate.crate_mode))),
|
2023-12-13 15:17:14 +08:00
|
|
|
active_crate: state.active_crate === id ? null : state.active_crate,
|
2023-12-11 17:05:35 +08:00
|
|
|
})),
|
2023-12-12 16:09:29 +08:00
|
|
|
_setCrateMode: (id, mode) => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (crate.id === id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
crate_mode: mode
|
|
|
|
}
|
|
|
|
} else return crate;
|
|
|
|
})
|
|
|
|
})),
|
|
|
|
setActiveCrate: (id) => set(state => ({active_crate: id})),
|
2024-11-01 17:32:20 +08:00
|
|
|
_addCardFromCatalog: (crate_to, index_from, index_to, horizontal) => set(state => {
|
|
|
|
const whichH = whichItems(horizontal)
|
2024-07-11 17:30:34 +08:00
|
|
|
const take_from = toArray(index_from).map((item, _i) => (state.cards_list[item]));
|
2023-12-11 17:05:35 +08:00
|
|
|
const dest = crate_to || state.active_crate;
|
2023-12-13 15:17:14 +08:00
|
|
|
if (!dest) return {};
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (dest === crate.id) {
|
2024-11-01 17:32:20 +08:00
|
|
|
index_to = index_to != null ? index_to : crate[whichH].length;
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: crate[whichH].toSpliced(index_to, 0, ...take_from.map((card_name, _) => {
|
2023-12-11 17:05:35 +08:00
|
|
|
return {...state.cards[card_name], id: uuidv4()}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
} else return crate;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
2024-11-01 17:32:20 +08:00
|
|
|
_moveCard: (crate_from, index_from, crate_to, index_to, horizontal) => set(state => {
|
|
|
|
const whichH = whichItems(horizontal)
|
|
|
|
const the_card = state.crates.find((crate, _) => crate_from === crate.id )[whichH][index_from];
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
crates: state.crates.map((crate, _i) => {
|
2023-12-11 17:47:16 +08:00
|
|
|
if (crate_to === crate_from && crate_to === crate.id) {
|
2024-11-01 17:32:20 +08:00
|
|
|
let items_copy = Array.from(crate[whichH]);
|
2023-12-15 13:49:54 +08:00
|
|
|
let item = items_copy.splice(index_from, 1)[0]
|
|
|
|
items_copy.splice(index_to, 0, item).filter((item, _) => !!item)
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: items_copy
|
2023-12-11 17:47:16 +08:00
|
|
|
}
|
|
|
|
} else if (crate_to === crate.id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: crate[whichH].toSpliced(index_to, 0, the_card)
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
} else if (crate_from === crate.id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: crate[whichH].toSpliced(index_to, 1)
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
2024-11-01 17:32:20 +08:00
|
|
|
_deleteCard: (crate_id, index, horizontal) => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
crates: state.crates.map((crate, _i) => {
|
2023-12-12 16:09:29 +08:00
|
|
|
if (crate_id === crate.id) {
|
2024-11-01 17:32:20 +08:00
|
|
|
const whichH = whichItems(horizontal)
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: crate[whichH].toSpliced(index, 1)
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
2023-12-12 16:09:29 +08:00
|
|
|
_clearCrate: (id) => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (id === crate.id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
items: [],
|
|
|
|
h_items: []
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
2023-12-12 16:09:29 +08:00
|
|
|
clearAll: () => set(state => ({
|
2024-01-15 13:01:27 +08:00
|
|
|
crates: state._defaultCrates
|
2023-12-12 16:09:29 +08:00
|
|
|
})),
|
2024-11-01 17:32:20 +08:00
|
|
|
_updateOptions: (crate_id, index, new_options, horizontal) => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
crates: state.crates.map((crate, _i) => {
|
2023-12-12 16:09:29 +08:00
|
|
|
if (crate_id === crate.id) {
|
2024-11-01 17:32:20 +08:00
|
|
|
const whichH = whichItems(horizontal)
|
|
|
|
let itemsCopy = Array.from(crate[whichH]);
|
2023-12-14 17:45:54 +08:00
|
|
|
itemsCopy[index] = {
|
|
|
|
...itemsCopy[index],
|
|
|
|
options_data: {
|
|
|
|
...itemsCopy[index].options_data,
|
|
|
|
...new_options
|
|
|
|
}};
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
2024-11-01 17:32:20 +08:00
|
|
|
[whichH]: itemsCopy
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
|
|
|
|
2023-12-12 16:09:29 +08:00
|
|
|
fillWarnings: (crate_id) => set(state => ({
|
2023-12-11 17:05:35 +08:00
|
|
|
crates: state.crates.map((crate, _i) => {
|
2023-12-12 16:09:29 +08:00
|
|
|
if (crate_id === crate.id) {
|
2023-12-13 15:17:14 +08:00
|
|
|
//console.log("--- CHECK ALERTS ---")
|
2023-12-11 17:05:35 +08:00
|
|
|
let itemsCopy = Array.from(crate.items);
|
2024-01-02 17:35:09 +08:00
|
|
|
const disabled = !!get().crateParams(crate.crate_mode).warnings_disabled;
|
|
|
|
itemsCopy = FillResources(itemsCopy, disabled);
|
|
|
|
itemsCopy = TriggerWarnings(itemsCopy, disabled);
|
2023-12-12 16:09:29 +08:00
|
|
|
const [crate_warnings, occupied] = TriggerCrateWarnings(crate);
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
items: itemsCopy,
|
2023-12-12 16:09:29 +08:00
|
|
|
warnings: crate_warnings,
|
|
|
|
occupiedHP: occupied
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
2023-12-12 16:09:29 +08:00
|
|
|
})),
|
|
|
|
|
2023-12-15 13:49:54 +08:00
|
|
|
fillExtData: (crate_id) => set(state => ({
|
|
|
|
crates: state.crates.map((crate, _i) => {
|
|
|
|
if (crate_id === crate.id) {
|
2024-07-16 11:36:13 +08:00
|
|
|
const options_name = state.crateParams(crate.crate_mode).options;
|
2023-12-15 13:49:54 +08:00
|
|
|
let itemsCopy = Array.from(crate.items);
|
|
|
|
|
|
|
|
itemsCopy = itemsCopy.map((item, index) => {
|
2024-07-16 11:36:13 +08:00
|
|
|
if (!item[options_name]) return item;
|
2023-12-15 13:49:54 +08:00
|
|
|
if (!item.options_data) item.options_data = {};
|
2024-01-30 13:02:01 +08:00
|
|
|
item.options_data.ext_data = FillExtCardData(itemsCopy, index);
|
2023-12-15 13:49:54 +08:00
|
|
|
return item;
|
|
|
|
});
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
items: Array.from(itemsCopy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
})),
|
|
|
|
|
2024-01-30 14:45:30 +08:00
|
|
|
_updateTotalOrderPrice: () => set(state => {
|
2023-12-12 16:09:29 +08:00
|
|
|
let sum = 0;
|
2024-01-25 17:10:14 +08:00
|
|
|
get().crates.forEach( (crate, i) => {
|
2023-12-12 16:09:29 +08:00
|
|
|
sum += get().crate_modes[crate.crate_mode].price;
|
2024-01-30 14:45:30 +08:00
|
|
|
const crate_options = ProcessOptionsToData({options: get().crate_prices, data: crate.options_data || {}});
|
|
|
|
sum += crate_options ? crate_options.reduce((accumulator, currentValue) => accumulator+currentValue.price, 0) : 0;
|
2024-11-01 17:32:20 +08:00
|
|
|
crate.items.concat(crate.h_items).forEach((item, _) => {
|
2023-12-12 16:09:29 +08:00
|
|
|
sum += item.price;
|
|
|
|
});
|
|
|
|
});
|
2024-01-30 17:20:29 +08:00
|
|
|
const order_options = ProcessOptionsToData({options: get().order_prices, data: get().order_options_data || {}});
|
|
|
|
sum += order_options ? order_options.reduce((accumulator, currentValue) => accumulator+currentValue.price, 0) : 0;
|
2024-01-30 14:45:30 +08:00
|
|
|
return {total_order_price: sum};
|
|
|
|
}),
|
2023-12-12 16:09:29 +08:00
|
|
|
|
|
|
|
// Composite actions that require warnings recalculation:
|
|
|
|
|
|
|
|
newCrate: () => {
|
|
|
|
const crate_id = "crate" + get().crates.length;
|
|
|
|
get()._newCrate(crate_id)
|
2023-12-15 13:49:54 +08:00
|
|
|
get().fillExtData(crate_id);
|
2024-01-30 13:02:01 +08:00
|
|
|
get().fillExtCrateData(crate_id);
|
2024-01-30 17:20:29 +08:00
|
|
|
get().fillOrderExtData();
|
2023-12-12 16:09:29 +08:00
|
|
|
get().fillWarnings(crate_id);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
setCrateMode: (id, mode) => {
|
|
|
|
get()._setCrateMode(id, mode)
|
2023-12-15 17:26:52 +08:00
|
|
|
get().fillExtData(id);
|
2024-01-30 13:02:01 +08:00
|
|
|
get().fillExtCrateData(id);
|
2024-01-30 17:20:29 +08:00
|
|
|
get().fillOrderExtData();
|
2023-12-12 16:09:29 +08:00
|
|
|
get().fillWarnings(id);
|
2023-12-13 17:35:17 +08:00
|
|
|
get().setActiveCrate(id);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
|
2024-01-30 17:20:29 +08:00
|
|
|
delCrate: (id) => {
|
|
|
|
get()._delCrate(id);
|
|
|
|
get().fillOrderExtData();
|
2024-10-04 11:25:49 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2024-01-30 17:20:29 +08:00
|
|
|
},
|
|
|
|
|
2024-03-15 15:22:05 +08:00
|
|
|
addCardFromCatalog: (crate_to, index_from, index_to, just_mounted) => {
|
2024-07-11 17:30:34 +08:00
|
|
|
const isCrateless = toArray(index_from).some(value => get().getCardDescription(value).crateless === true);
|
2024-11-01 17:32:20 +08:00
|
|
|
const isHorizontal = toArray(index_from).some(value => !get().getCardDescription(value).image);
|
|
|
|
const dest = isCrateless ? "spare" : crate_to || get().active_crate;
|
2024-01-05 17:22:53 +08:00
|
|
|
if (!dest) {
|
|
|
|
console.warn("No destination");
|
|
|
|
get().noDestinationWarning();
|
|
|
|
return {};
|
|
|
|
}
|
2024-11-01 17:32:20 +08:00
|
|
|
get().showNotification(dest, index_to, isHorizontal);
|
2024-03-15 15:22:05 +08:00
|
|
|
get()._addCardFromCatalog(dest, index_from, index_to)
|
2023-12-15 13:49:54 +08:00
|
|
|
get().fillExtData(dest);
|
2023-12-12 16:09:29 +08:00
|
|
|
get().fillWarnings(dest);
|
2023-12-13 16:42:18 +08:00
|
|
|
get().setActiveCrate(dest);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-13 15:17:14 +08:00
|
|
|
if (!just_mounted) {
|
|
|
|
get().cardAdded()
|
|
|
|
}
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
moveCard: (crate_from, index_from, crate_to, index_to) => {
|
2024-11-01 17:32:20 +08:00
|
|
|
const [isHorizontal, crateFrom] = unwrapCrateId(crate_from)
|
|
|
|
const [_, crateTo] = unwrapCrateId(crate_to)
|
|
|
|
get()._moveCard(crateFrom, index_from, crateTo, index_to, isHorizontal);
|
|
|
|
get().fillExtData(crateTo);
|
|
|
|
get().fillWarnings(crateTo);
|
|
|
|
get().setActiveCrate(crateTo);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2024-11-01 17:32:20 +08:00
|
|
|
if (crateFrom !== crate_to) {
|
|
|
|
get().fillExtData(crateFrom);
|
|
|
|
get().fillWarnings(crateFrom);
|
2023-12-15 13:49:54 +08:00
|
|
|
}
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
deleteCard: (crate_id, index) => {
|
2024-11-01 17:32:20 +08:00
|
|
|
const [isHorizontal, crateId] = unwrapCrateId(crate_id);
|
|
|
|
get()._deleteCard(crateId, index, isHorizontal);
|
|
|
|
get().fillExtData(crateId);
|
|
|
|
get().fillWarnings(crateId);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2024-11-01 17:32:20 +08:00
|
|
|
if (crateId === get().highlighted.crate && index === get().highlighted.card) get().highlightReset()
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
clearCrate: (id) => {
|
|
|
|
get()._clearCrate(id);
|
|
|
|
get().fillWarnings(id);
|
2024-10-04 11:25:49 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
updateOptions: (crate_id, index, new_options) => {
|
2024-11-01 17:32:20 +08:00
|
|
|
const [isHorizontal, crateId] = unwrapCrateId(crate_id);
|
|
|
|
get()._updateOptions(crate_id, index, new_options, isHorizontal);
|
2023-12-15 13:49:54 +08:00
|
|
|
get().fillExtData(crate_id);
|
2023-12-12 16:09:29 +08:00
|
|
|
get().fillWarnings(crate_id);
|
2024-01-25 17:10:14 +08:00
|
|
|
},
|
|
|
|
|
2024-01-30 13:02:01 +08:00
|
|
|
initExtData: () => {
|
|
|
|
get().fillOrderExtData();
|
|
|
|
get().crates.forEach((crate, _i) => {
|
|
|
|
get().fillExtData(crate.id);
|
|
|
|
get().fillExtCrateData(crate.id);
|
|
|
|
})
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-12 16:09:29 +08:00
|
|
|
}
|
2023-12-11 17:05:35 +08:00
|
|
|
}))
|
|
|
|
|
|
|
|
|
2023-12-15 17:26:52 +08:00
|
|
|
export const useShopStore = createWithEqualityFn((...params) => ({
|
2024-03-18 14:48:16 +08:00
|
|
|
...useOptionsNotification(...params),
|
2024-03-15 15:22:05 +08:00
|
|
|
...useCatalog(...params),
|
2024-02-22 17:26:07 +08:00
|
|
|
...useSearch(...params),
|
2023-12-11 17:05:35 +08:00
|
|
|
...useCrateModes(...params),
|
|
|
|
...useCart(...params),
|
|
|
|
...useSubmitForm(...params),
|
2023-12-12 16:09:29 +08:00
|
|
|
...useLayout(...params),
|
|
|
|
...useHighlighted(...params),
|
2023-12-13 12:39:15 +08:00
|
|
|
...useImportJSON(...params),
|
2024-01-29 17:26:59 +08:00
|
|
|
...useCrateOptions(...params),
|
|
|
|
...useOrderOptions(...params),
|
2023-12-11 17:05:35 +08:00
|
|
|
}))
|