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
|
|
|
|
|
|
|
|
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-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,
|
|
|
|
notificationTimer: null,
|
|
|
|
_showNotification: (crate_id, card_index) => set(state => ({
|
|
|
|
notificationCrateId: crate_id,
|
|
|
|
notificationCardIndex: card_index,
|
|
|
|
notificationTimer: setTimeout(() => {
|
|
|
|
state.hideNotification()
|
|
|
|
}, 5000)
|
|
|
|
})),
|
|
|
|
showNotification: (crate_id, card_index) => {
|
|
|
|
get().hideNotification()
|
|
|
|
setTimeout(() => get()._showNotification(crate_id, card_index), 100);
|
|
|
|
},
|
|
|
|
hideNotification: () => set(state => ({
|
|
|
|
notificationCrateId: null,
|
|
|
|
notificationCardIndex: null,
|
|
|
|
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-03-18 14:48:16 +08:00
|
|
|
get().showNotification(get().active_crate, null);
|
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: "",
|
|
|
|
card: 0
|
|
|
|
},
|
2023-12-13 15:17:14 +08:00
|
|
|
highlightedTimer: null,
|
2024-01-02 17:13:50 +08:00
|
|
|
|
|
|
|
// #!if disable_card_highlight === false
|
2023-12-12 16:09:29 +08:00
|
|
|
highlightCard: (crate_id, index) => set(state => ({
|
|
|
|
highlighted: {
|
|
|
|
crate: crate_id,
|
|
|
|
card: index
|
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: "",
|
|
|
|
card: 0
|
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-03-15 15:22:05 +08:00
|
|
|
_addCardFromCatalog: (crate_to, index_from, index_to) => set(state => {
|
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) {
|
2023-12-12 16:09:29 +08:00
|
|
|
index_to = index_to != null ? index_to : crate.items.length;
|
2023-12-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
items: crate.items.toSpliced(index_to, 0, ...take_from.map((card_name, _) => {
|
|
|
|
return {...state.cards[card_name], id: uuidv4()}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
} else return crate;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
2023-12-12 16:09:29 +08:00
|
|
|
_moveCard: (crate_from, index_from, crate_to, index_to) => set(state => {
|
2023-12-11 17:47:16 +08:00
|
|
|
const the_card = state.crates.find((crate, _) => crate_from === crate.id ).items[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) {
|
|
|
|
let items_copy = Array.from(crate.items);
|
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,
|
2023-12-15 13:49:54 +08:00
|
|
|
items: items_copy
|
2023-12-11 17:47:16 +08:00
|
|
|
}
|
|
|
|
} else if (crate_to === crate.id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
items: crate.items.toSpliced(index_to, 0, the_card)
|
2023-12-11 17:05:35 +08:00
|
|
|
}
|
|
|
|
} else if (crate_from === crate.id) {
|
|
|
|
return {
|
|
|
|
...crate,
|
|
|
|
items: crate.items.toSpliced(index_to, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else return crate;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}),
|
2023-12-12 16:09:29 +08:00
|
|
|
_deleteCard: (crate_id, index) => 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-11 17:05:35 +08:00
|
|
|
return {
|
|
|
|
...crate,
|
2023-12-12 16:09:29 +08:00
|
|
|
items: crate.items.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,
|
|
|
|
items: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
})),
|
|
|
|
_updateOptions: (crate_id, index, new_options) => 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-11 17:05:35 +08:00
|
|
|
let itemsCopy = Array.from(crate.items);
|
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,
|
|
|
|
items: itemsCopy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
2023-12-12 16:09:29 +08:00
|
|
|
crate.items.forEach((item, _) => {
|
|
|
|
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-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);
|
|
|
|
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-03-18 14:48:16 +08:00
|
|
|
get().showNotification(dest, index_to);
|
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) => {
|
|
|
|
get()._moveCard(crate_from, index_from, crate_to, index_to);
|
2023-12-15 13:49:54 +08:00
|
|
|
get().fillExtData(crate_to);
|
2023-12-12 16:09:29 +08:00
|
|
|
get().fillWarnings(crate_to);
|
2023-12-13 16:42:18 +08:00
|
|
|
get().setActiveCrate(crate_to);
|
2024-01-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-15 13:49:54 +08:00
|
|
|
if (crate_from !== crate_to) {
|
|
|
|
get().fillExtData(crate_from);
|
|
|
|
get().fillWarnings(crate_from);
|
|
|
|
}
|
2023-12-12 16:09:29 +08:00
|
|
|
},
|
|
|
|
deleteCard: (crate_id, index) => {
|
|
|
|
get()._deleteCard(crate_id, index);
|
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-30 14:45:30 +08:00
|
|
|
get()._updateTotalOrderPrice();
|
2023-12-13 15:17:14 +08:00
|
|
|
if (crate_id === 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);
|
|
|
|
},
|
|
|
|
|
|
|
|
updateOptions: (crate_id, index, new_options) => {
|
|
|
|
get()._updateOptions(crate_id, index, new_options);
|
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
|
|
|
}))
|