import React from 'react'; import superagent from 'superagent'; import _ from 'lodash'; import { fromUnixTime, formatDistanceToNow, set, setDay, addHours, addDays, getUnixTime } from "date-fns"; import { formatInTimeZone } from 'date-fns-tz'; import { DateInput } from 'semantic-ui-calendar-react'; import constants from '../CloudApi/Constants.js'; import WaxIdBitField from './WaxIdBitField.jsx'; import ToolPathDownloadButton from './ToolPathDownloadButton.jsx'; import JobsList from "./JobsList.jsx"; import * as ApiUtils from '../CloudApi/ApiUtils.js'; import { Segment, Modal, Header, Icon, Button, Table, Form, Divider, Label, Popup, Pagination } from 'semantic-ui-react' import FabricatorWrapper from './FabricatorWrapper.jsx'; const TIMEZONE_LOS_ANGELES = "America/Los_Angeles"; export default class FabricatorHome extends React.Component { constructor(props) { super(props); this.state = { currentPageIndex: 1, pageCount: 0, items: [], query: { origin:"Shopify" }, limit: 50, offset: 0, count: 0, searchBits: _.range(0, 20).map(i => "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"] = parseInt(value); } else if (key === "maxCreatedAt") { query["maxCreatedAt"] = parseInt(value); } else { query[key] = value; } } console.log(query); this.setState({ query }); await this.onUpdateQuery(); } catch (e) { console.log(e); } } getUrlSearchParams() { const queryString = new URLSearchParams(); queryString.append("limit", this.state.limit); queryString.append("offset", this.state.offset); if (this.state.query) { for (const [key, value] of Object.entries(this.state.query)) { if (value === "" || value === null) continue; queryString.append(key, value); } } return queryString; } onChange(e, { name, value }) { const query = this.state.query; if (name) { console.log("onChange", name, value) if (value === "" || value === null) { console.log("delete", name, value); delete query[name]; } else { if (name === "jobState" && value === ApiUtils.ANY_VALUE) { delete query["jobState"]; } else if (name === "minCreatedAt") { query["minCreatedAt"] = new Date(value).getTime(); } else if (name === "maxCreatedAt" && !_.isEmpty(value)) { query["maxCreatedAt"] = new Date(value).getTime(); } else { query[name] = value; } } } console.log(query); this.setState({ query }, async () => { const params = this.getUrlSearchParams(); const newPathQuery = `${window.location.pathname}?${params.toString()}`; window.history.pushState(null, '', newPathQuery); await this.onUpdateQuery(); }); } onClearValue(e, { name, value }) { const query = this.state.query; query[name] = ""; this.setState({ query }, this.onChange(null, {})); } async onUpdateQuery() { try { this.setState({ loading: true }); const params = this.getUrlSearchParams(); console.log(params.toString()); const res = await superagent.get(`/api/admin/fabricator/jobs?${params.toString()}`); const pageCount = Math.ceil(res.body.count / this.state.limit); this.setState({ items: res.body.items, count: res.body.count, pageCount }); } catch (e) { console.log(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); }); } async handleBitClick(bit) { const searchBits = this.state.searchBits; searchBits[bit] = (searchBits[bit] === "1") ? "0" : "1"; this.setState({searchBits}); let searchBitsTemp = [...searchBits]; searchBitsTemp[0] = "0"; let value = parseInt(searchBitsTemp.join(""), 2).toString(16).padStart(5, '0').toUpperCase(); await this.onChange(null, { name: "waxId", value }); } async onClearWaxId() { const query = this.state.query; query["waxId"] = ""; this.setState({ query }, this.onChange(null, {})); } renderWaxIdSearch() { const searchBits = this.state.searchBits; const getWaxIdControls = (from, to) => { return searchBits.slice(from, to).map((bit, i) => { return bit === "1" ? ( this.handleBitClick(i + from)}>⚫) : ( this.handleBitClick(i + from)}>🔵); }); } if (searchBits.length === 20) { let bitFieldLeft = null; let bitFieldRight = null; bitFieldLeft =
{this.state.count} items found
}