import { For, createSignal, onMount } from "solid-js"; import { Survey } from "../../util/survey-utils/survey"; import mainStylesheet from "./MintedUpcPicker.css?inline"; import { GridCell } from "./types"; import { buildGridCells, fetchLocations, getGridDimensions, restructurePlano } from "./util"; const QUESTION_TEXT = /Scan ALL.* Cards out of stocks/i; const submittedUpcs = new Set(); export const TOGGLE_MODAL_EVENT_TYPE = "toggle-minted-upc-picker-event"; export interface MintedUpcPickerProps { pogName: string; } export function MintedUpcPicker(props: MintedUpcPickerProps) { const [isVisible, setIsVisible] = createSignal(false); const [numColumns, setNumColumns] = createSignal(0); const [gridCells, setGridCells] = createSignal([]); const [selectedUpcs, setSelectedUpcs] = createSignal>(new Set()); function toggleVisible(): void { setIsVisible((prev) => !prev); } function toggleCell(cell: GridCell): void { if (cell.isDisabled) { return; } setSelectedUpcs((prev) => { const next = new Set(prev); const isCurrentlySelected = cell.upcs.every((upc) => next.has(upc)); cell.upcs.forEach((upc) => (isCurrentlySelected ? next.delete(upc) : next.add(upc))); return next; }); } function handleSubmit(): void { const oosUpcs = [...selectedUpcs()]; const survey = Survey.getInstance(); oosUpcs.forEach((upc) => { if (submittedUpcs.has(upc)) { return; } survey.answer("scan-input", QUESTION_TEXT, upc, { clickAdd: true }); submittedUpcs.add(upc); }); setSelectedUpcs(new Set()); setIsVisible(false); } onMount(() => { window.addEventListener(TOGGLE_MODAL_EVENT_TYPE, toggleVisible); document.addEventListener("keydown", (event) => { if (event.key === "Escape") { setIsVisible(false); } }); void loadPlanogram(); }); async function loadPlanogram(): Promise { const planogramLocationsResponse = await fetchLocations(props.pogName); const locationData = restructurePlano(planogramLocationsResponse.home_locations); const horizontalSectionThresholds = new Set( planogramLocationsResponse.planogram.horizontal_section_thresholds, ); const [, maxRowLetter, columnCount] = getGridDimensions(locationData); setNumColumns(columnCount); setGridCells( buildGridCells(locationData, columnCount, maxRowLetter, horizontalSectionThresholds), ); } return (
); }