import React from 'react'; import superagent from 'superagent'; import { fromUnixTime, formatDistanceToNow } from "date-fns"; import { formatInTimeZone } from 'date-fns-tz'; import { Segment, Form, Table, Statistic, Label, Button, Icon, Pagination, Header } from 'semantic-ui-react' import { DateInput } from 'semantic-ui-calendar-react'; import constants, { DEFAULT_TIMEZONE } from '../CloudApi/Constants.js'; import * as ApiUtils from '../CloudApi/ApiUtils.js'; import InventoryWrapper from './InventoryWrapper.jsx'; import InventoryItemsTable from './InventoryItemsTable.jsx'; import HandScannerInput from '../Util/HandScannerInput.jsx'; export default class InventoryHome extends React.Component { constructor(props) { super(props); this.state = { currentPageIndex: 1, pageCount: 0, items: [], query: { origin: "Shopify" }, limit: 50, offset: 0, count: 0, }; } async componentDidMount() { try { let schemas = await ApiUtils.getFabricatorSchemas(); this.setState({ schemas }); const params = new URLSearchParams(window.location.search); const query = {}; for (const [key, value] of params.entries()) { if (key === "limit") { this.setState({ limit: value }); } else if (key === "offset") { this.setState({ offset: value }); } else if (key === "minCreatedAt") { query["minCreatedAt"] = new Date(parseInt(value)).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); } else if (key === "maxCreatedAt") { query["maxCreatedAt"] = new Date(parseInt(value)).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); } else { query[key] = value; } } console.log(query); this.setState({ query }); await this.onUpdateQuery(); } catch (e) { console.log(e); } let res2 = await superagent.get(`/api/admin/inventory/stats`); this.setState({ stats: res2.body }); } async onSerialNumberScanned(scannedSerialNumber) { return this.onChange(null, { name: "serialNumberLike", value: scannedSerialNumber }); } onChange(e, { name, value }) { const query = this.state.query; if (_.isEmpty(value)) { console.log("delete", name, value); delete query[name]; } else { if (name === "state" && value === ApiUtils.ANY_VALUE) { delete query["state"]; } else if (name === "shopifyMinCreatedAt") { query["minCreatedAt"] = new Date(value).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); // console.log("minCreatedAt", query["minCreatedAt"]); //debug } else if (name === "shopifyMaxCreatedAt" && !_.isEmpty(value)) { query["maxCreatedAt"] = new Date(value).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' }); } else { query[name] = value; } } this.setState({ query }, async () => { const params = this.getUrlSearchParams(); console.log('onChange query setState', params.toString()); const newPathQuery = `${window.location.pathname}?${params.toString()}`; window.history.pushState(null, '', newPathQuery); await this.onUpdateQuery(); }); } getUrlSearchParams() { const queryString = new URLSearchParams(); queryString.append("limit", this.state.limit); queryString.append("offset", this.state.offset); console.log("getUrlSearchParams", Object.entries(this.state.query)) if (this.state.query) { for (const [key, value] of Object.entries(this.state.query)) { if (value === "" || value === null) continue; if (key.includes("CreatedAt")) { queryString.append(key, new Date(value).getTime()); } else { queryString.append(key, value); } } } return queryString; } async onUpdateQuery() { try { this.setState({ loading: true }); const params = this.getUrlSearchParams(); const res = await superagent.get(`/api/admin/inventory/items?${params.toString()}`); const pageCount = Math.ceil(res.body.count / this.state.limit); console.log(res.body); this.setState({ items: res.body.items, count: res.body.count, pageCount }); } catch (e) { this.setState({ error: e }); } this.setState({ loading: false }); } // newPageIndex is 1-based async onPageChanged(newPageIndex) { this.setState({ currentPageIndex: newPageIndex, offset: Math.max(newPageIndex - 1, 0) * this.state.limit }, () => { this.onUpdateQuery(0); }); } render() { if (!this.state.schemas) { return
; } const pagination = (
this.onPageChanged(activePage)} totalPages={this.state.pageCount} ellipsisItem firstItem lastItem />
{this.state.items &&

{this.state.count} items found

}
); return (
Inventory Items In Stock:
{this.state.stats && this.state.stats.statsByTypeInStock.map((stat, index) => { if (stat.count <= 5) { return null; } return {stat.count} {ApiUtils.splitUpperCamelCase(stat.type)} })} {this.state.schemas &&
} {!this.state.loading && this.state.count > 0 && pagination} {this.state.count > 0 && } {this.state.loading && } {!this.state.loading && this.state.count === 0 &&
Nothing found.
} {!this.state.loading && this.state.count > 0 && pagination}
); } }