import React from 'react';
import superagent from 'superagent';
import { fromUnixTime } from "date-fns";
import { formatInTimeZone } from 'date-fns-tz';
import { Accordion, Segment, Header, Icon, Button, Table, Form, Label, Modal, Message, Card, Image, Radio, Grid, SegmentGroup } 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 BigCreateCustomOrder extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: false,
bigOrderSearchValue: "",
shopifyProductKeys: [],
sendEmail: true,
priority: 55,
scanRequestOption: "Unknown",
ipdOverride: -1,
convertToBigOrder: false,
error: null
};
}
async componentDidMount() {
try {
let schemas = await ApiUtils.getFabricatorSchemas();
this.setState({ schemas });
} catch (e) {
console.error(e);
}
}
async onFindBigOrders() {
this.setState({ loading: true, error: null });
let query = "";
if (this.state.bigOrderSearchValue.startsWith("#BS")) {
query = `/api/admin/shop/orders?origin=Shopify&limit=1&shopifyOrderName=${encodeURIComponent(this.state.bigOrderSearchValue)}`
} else {
// Assume it's an email
query = `/api/admin/shop/orders?origin=Shopify&shopifyCustomerEmail=${encodeURIComponent(this.state.bigOrderSearchValue)}`
}
try {
const res = await superagent.get(query).accept('json');
console.log(res.body);
if (res.body.items.length === 1) {
this.setState({ bigOrders: res.body.items, selectedBigOrder: null });
} else if (res.body.items.length > 1) {
this.setState({ bigOrders: res.body.items, selectedBigOrder: null });
} else {
this.setState({ bigOrders: null, selectedBigOrder: null });
}
} catch (e) {
this.setState({ error: e });
console.error(e);
}
this.setState({ loading: false });
}
async onCreateReplacement(order) {
this.setState({ selectedBigOrder: order, ipdOverride: order.ipd, bigscreenAccountId: order.ownerBigscreenAccountId });
}
onChange(e, { name, value }) {
console.log(name, value);
this.setState({ [name]: value })
}
async onCreateCustomOrder() {
this.setState({ loading: true, error: null });
try {
const payload = {
originalBigOrderId: this.state.selectedBigOrder.id,
scanRequestAction: this.state.scanRequestOption,
sendEmail: this.state.sendEmail,
priority: this.state.priority,
test: false
};
payload.shopifyProductIds = this.state.shopifyProductKeys.map((key) => this.state.schemas.ShopifyProductIds[key]);
if (this.state.ipdOverride !== -1) {
payload.ipd = this.state.ipdOverride;
}
let res = await superagent.post(`/api/admin/shop/shopify_order`).send(payload).accept('json');
// Check for errors in the response (order created but with issues)
if (res.body.errors && res.body.errors.length > 0) {
const errorMessages = res.body.errors.map(e => e.message).join(', ');
console.error("Order created with errors:", errorMessages);
// Still redirect but with warning indication
window.location.href = `/shop/order/${res.body.bigOrder.shopifyOrderName}?warning=${encodeURIComponent(errorMessages)}`;
} else {
window.location.href = `/shop/order/${res.body.bigOrder.shopifyOrderName}`;
}
} catch (e) {
this.setState({ error: e });
console.error(e);
}
this.setState({ loading: false });
}
onToggleProductSelection(productKey) {
return (e) => {
let shopifyProductKeys = this.state.shopifyProductKeys;
if (shopifyProductKeys.includes(productKey)) {
shopifyProductKeys = shopifyProductKeys.filter((id) => id !== productKey);
} else {
shopifyProductKeys.push(productKey);
}
this.setState({ shopifyProductKeys });
}
}
renderProductCheckbox(productKey) {
let cardColor = "";
let inverted = true;
if (this.state.shopifyProductKeys.includes(productKey)) {
cardColor = "green";
inverted = false;
}
let productType = productKey;
return (
{this.state.shopifyProductKeys.includes(productKey) && <> >}
{ApiUtils.splitUpperCamelCase(productKey)}
{this.state.shopifyProductKeys.includes(productKey) && (
Added to order
)}
{this.state.shopifyProductKeys.includes(productKey) && productType == "ReplacementBigscreenBeyondV1" && (
)}
{this.state.shopifyProductKeys.includes(productKey) && }
{!this.state.shopifyProductKeys.includes(productKey) && }
)
}
render() {
if (!this.state.schemas) {
return Loading...;
}
let createButtonDisabled = true;
if (this.state.shopifyProductKeys.length > 0) {
if (this.state.shopifyProductKeys.includes("ReplacementBeyondCushionV1")) {
createButtonDisabled = this.state.scanRequestOption === "Unknown";
} else {
createButtonDisabled = false;
}
}
let customOrderForm = null;
if (this.state.selectedBigOrder) {
customOrderForm = (
2. Create Replacement Order
);
}
return (
1. Find an existing Big Order
{(this.state.bigOrders && this.state.bigOrders.length > 0) &&
{this.state.bigOrders.map(order => (
))}
}
{customOrderForm}
);
}
}