import React from 'react'; import superagent from 'superagent'; import { fromUnixTime } from "date-fns"; import { formatInTimeZone } from 'date-fns-tz'; import { Segment, Header, Icon, Button, Table, Form, Label, Modal, Message, Card, Image, Radio, Grid, Step, Divider } from 'semantic-ui-react' import * as ApiUtils from '../CloudApi/ApiUtils.js'; import constants, { DEFAULT_TIMEZONE } from '../CloudApi/Constants.js'; import BigApiErrorMessage from '../CloudApi/BigApiErrorMessage.jsx'; import BigOrdersWrapper from './BigOrdersWrapper.jsx'; import BigOrderComponent from './BigOrderComponent.jsx'; export default class BigCreateCustomOrder2 extends React.Component { constructor(props) { super(props); this.state = { loading: false, shopifyVariantIds: [], customerName: null, ownerBigscreenAccountId: null, sendEmail: true, priority: 55, scanRequestAction: "Unknown", showScanRequestModal: false, testOrder: true, ipdOverride: -1, convertToBigOrder: false, searchPerformed: false, error: null }; } async componentDidMount() { try { this.setState({ loading: true }); let schemas = await ApiUtils.getFabricatorSchemas(); const res = await superagent.get(`/api/admin/shop/products`); this.setState({ schemas, products: res.body, productsCount: res.body.length }); } catch (e) { console.error(e); } this.setState({ loading: false }); } async onCreateReplacement(order) { this.setState({ bigscreenAccountId: order.ownerBigscreenAccountId }); } onChange(e, { name, value }) { this.setState({ [name]: value }) } async onFindCustomer() { this.setState({ loading: true, error: null, searchPerformed: false }); let query = ""; if (this.state.customerSearchValue.startsWith("#BS")) { query = `/api/admin/shop/orders?origin=Shopify&limit=1&shopifyOrderName=${encodeURIComponent(this.state.customerSearchValue)}` } else { // Assume it's an email query = `/api/admin/shop/orders?origin=Shopify&shopifyCustomerEmail=${encodeURIComponent(this.state.customerSearchValue)}` } try { const res = await superagent.get(query).accept('json'); console.log(res.body); if (res.body.items.length > 0) { // Load the customer account info. const ownerBigscreenAccountId = res.body.items[0].ownerBigscreenAccountId; this.setState({ customerName: res.body.items[0].shopifyOrderSnapshot.shipping_address.name, ownerBigscreenAccountId }); } else { this.setState({ customerName: null, ownerBigscreenAccountId: null }); } } catch (e) { this.setState({ error: e }); console.error(e); } this.setState({ loading: false, searchPerformed: true }); } getFilteredVariants() { if (!this.state.products) return []; const variants = []; for (const product of this.state.products) { for (const variant of product.variants) { if (variant.sku && variant.sku !== "BS2S-UP" && (variant.sku.startsWith("BS2S") || variant.sku.startsWith("CB") || variant.sku.startsWith("RB"))) { let imageSrc = product.image ? product.image.src : null; const variantImage = product.images.find(image => image.id === variant.image_id); if (variantImage) { imageSrc = variantImage.src; } variants.push({ product, variant, imageSrc }); } } } return variants; } hasCushionSelected() { if (!this.state.products) return false; const filtered = this.getFilteredVariants(); return this.state.shopifyVariantIds.some(id => { const match = filtered.find(f => f.variant.id === id); return match && match.variant.sku === "RBS1F"; }); } hasCourtesyHeadsetSelected() { if (!this.state.products) return false; const filtered = this.getFilteredVariants(); return this.state.shopifyVariantIds.some(id => { const match = filtered.find(f => f.variant.id === id); return match && (match.variant.sku === "CBS2" || match.variant.sku === "CBS2E"); }); } hasShellSelected() { if (!this.state.products) return false; const filtered = this.getFilteredVariants(); return this.state.shopifyVariantIds.some(id => { const match = filtered.find(f => f.variant.id === id); return match && match.variant.sku && match.variant.sku.startsWith("BS2S-"); }); } needsShellWarning() { return this.hasCourtesyHeadsetSelected() && !this.hasShellSelected(); } openScanRequestModal() { this.setState({ showScanRequestModal: true }); } closeScanRequestModal() { this.setState({ showScanRequestModal: false }); } onToggleVariantSelection(productKey) { return (e) => { let shopifyVariantIds = [...this.state.shopifyVariantIds]; if (shopifyVariantIds.includes(productKey)) { shopifyVariantIds = shopifyVariantIds.filter((id) => id !== productKey); } else { shopifyVariantIds.push(productKey); } const newState = { shopifyVariantIds }; // Check if a cushion variant was just toggled on - open modal if (!this.state.shopifyVariantIds.includes(productKey) && shopifyVariantIds.includes(productKey)) { const filtered = this.getFilteredVariants(); const match = filtered.find(f => f.variant.id === productKey); if (match && match.variant.sku === "RBS1F") { newState.showScanRequestModal = true; } } this.setState(newState); } } async onCreateCustomOrder() { this.setState({ loading: true, error: null }); try { const payload = { ownerBigscreenAccountId: this.state.ownerBigscreenAccountId, shopifyVariants: this.state.shopifyVariantIds, scanRequestAction: this.state.scanRequestAction, sendEmail: this.state.sendEmail, priority: this.state.priority, shopifyOrderMode: this.state.testOrder ? "TestOrder" : "ProductionOrder" }; console.log(payload); let res = await superagent.post(`/api/admin/shop/shopify_order_2`).send(payload).accept('json'); console.log(res.body); window.location.href = `/shop/order/${res.body.bigOrder.shopifyOrderName}`; } catch (e) { this.setState({ error: e }); console.error(e); } this.setState({ loading: false }); } render() { if (!this.state.schemas) { return Loading...; } const customerSelected = !!(this.state.customerName && this.state.customerName.length > 0 && this.state.ownerBigscreenAccountId); let createButtonDisabled = true; if (this.state.shopifyVariantIds.length > 0 && customerSelected) { if (this.hasCushionSelected()) { createButtonDisabled = this.state.scanRequestAction === "Unknown"; } else { createButtonDisabled = false; } } if (this.needsShellWarning()) { createButtonDisabled = true; } const filteredVariants = this.getFilteredVariants(); let customOrderForm = null; if (this.state.products) { customOrderForm = ( <>
Add Items
{filteredVariants.map(({ product, variant, imageSrc }) => { const isSelected = this.state.shopifyVariantIds.includes(variant.id); return ( {imageSrc && (
)} {product.title} {variant.title} {isSelected ? (
{/* Scan Request Modal */} Scan Request Action

A cushion item has been added. Please select what to do with the scan request:


); } const cartColumn = (
Order Summary {this.state.shopifyVariantIds.length} item{this.state.shopifyVariantIds.length !== 1 ? 's' : ''}
{this.state.shopifyVariantIds.length === 0 ? (
No items added yet
) : (
{this.state.shopifyVariantIds.map((id, index) => { const match = filteredVariants.find(f => f.variant.id === id); if (!match) return null; return ( {index > 0 && }
{match.product.title}
{match.variant.title}
SKU: {match.variant.sku}
); })}
)} {this.hasCushionSelected() && ( <>
Scan Request Action
{this.state.scanRequestAction === "Unknown" ? ( ) : ( )} {' '}
)} {this.needsShellWarning() && ( Shell color required Add a shell color (BS2S-C, BS2S-B, BS2S-O, or BS2S-P) to this order. )}
Order Mode
this.setState({ testOrder: true })} style={{ color: '#fff' }} /> this.setState({ testOrder: false })} style={{ color: '#fff' }} />
{this.state.testOrder && ( Test Order This is a test order. BE CAREFUL WITH SHIPPING THIS ORDER! )}