import {Action} from "../SelectActionPopupWindow"; import {atom, useAtom} from "jotai"; import {ComponentRuntimeDataWithParentType} from "../components"; import React from "react"; import {Note} from "../cards"; import {merge, set} from "lodash"; import {produce} from "immer"; import {OfferData, useNodesStore} from "../store"; import {atomsStore, PopupWindowState, SelectActionContextOffers, SelectActionPopupWindowStateAtom} from "../atoms"; import {useOpenTestablesPopupWindow} from "../useOpenTestablesPopupWindow"; import {getOffersPopupWindowContext} from "../../OffersBranch"; import {useBranch} from "../TreeHooks"; import {BuyXGetXMeta} from "../cards/BuyXGetX"; import {__} from "../../globals"; import {getOpenPopupsCount} from "../hooks"; import {getPopupStackDimensionCalculators} from "../ulitilies"; const SelectedOfferAtom = atom | null>(null) const SelectedOffersAtom = atom([]) atomsStore.set(SelectedOfferAtom, null) atomsStore.set(SelectedOffersAtom, []) function isEditMode(): boolean { const {context: {data}} = atomsStore.get(SelectActionPopupWindowStateAtom) as PopupWindowState // @ts-ignore return !!data?.offerId } export const incompatibleOffersFromInsideTiers = [ BuyXGetXMeta.id ]; let offersTestableisOpen = false // @ts-ignore export const SelectOffersNew: Action = { id: 'select-offers', data: { name: __('Apply Offers'), icon: }, reset: () => { atomsStore.set(SelectedOfferAtom, null) atomsStore.set(SelectedOffersAtom, []) }, // @ts-ignore screens: ({context: {targetBranchId}}) => [ { id: 2, // BECAUSE 1 IS THE MAIN SELECT ACTIONS SCREEN title: '', content: ({screenNavigation, close}) => { if (offersTestableisOpen ) { return } const openTestablesPopup = useOpenTestablesPopupWindow(); const branch = useBranch(targetBranchId) const addBranchOffer = useNodesStore(store => store.addBranchOffer) offersTestableisOpen = true // @ts-ignore openTestablesPopup({ // @ts-ignore context: merge( { data: getPopupStackDimensionCalculators( getOpenPopupsCount()) }, getOffersPopupWindowContext([ // exclude get x if innside another branch (aka in tiers) bc get x is not supported in tiers currently ...(branch?.isInsideAnotherBranch() ? incompatibleOffersFromInsideTiers : []) ]) ), onClose: (data) => { const {status, components} = data if (status === 'success' && components?.length > 0) { // let's js ad the offer to the branch, the grove will handle the necessary logic for converting the branch if necessary, adding the offer to the store, etc. addBranchOffer(components[0], targetBranchId) close(status) } else { // failed, so go back to the previous screen // this usually happens when the user hit "back" or the "x" button on the popup screenNavigation.prev() } offersTestableisOpen = false /*// @ts-ignore const saver = new TestablesSaver(context) // @ts-ignore saver.add(data.components)*/ } }) return // alight so here we'll open the testables popup window set to offers // then we'll add a handler so that we can talk to it after its been closed // there are two possible outcomes after closing: // 1- offers have been seected // 2- no offers have been selected, the user quit the popup // lets just first open it // @ts-ignore /*const context = { id: 'offers-new', scope: 'tree-node', data: { componentType: 'filter', targetType: 'root', // root | testableComposite targetId: null, // string id if testableComposite, index if root (eg: 0 for the first root, 1 for the second root, etc) } } as PopupWindowStateContext*/ /*openTestablesPopup({ // @ts-ignore context, onClose: (data) => { console.log('got a request to close the popup wit: ', data) /!*!// @ts-ignore const saver = new TestablesSaver(context) // @ts-ignore saver.add(data.components)*!/ } })*/ /** * const openNewTestablesPopupWindow = useNewTestablesPopupWindow({ * onClose: () => {} // here we'll handle the offers that were selected * }) * * opennewNewTestablesPopupWindow({ * //here we'll set the data for the popup window (context, etc.) * }) */ }, } ], }