import { formscreen } from "./formscreen.js"; import { create_request } from "./create_request.js"; import { bool_response } from "./bool_response.js"; import { product_base } from "./product_base.js"; import { searcher } from "./searcher.js"; export class product_adder extends formscreen { #name; #description; #author; #barcode; #stock_count; #image; get _name() { return "Add product"; } _build_form() { this.#name = this._create_input( "name", "Name:", "Sample..." ); this.#description = this._create_input( "description", "Description:", "This is sample product..." ); this.#author = this._create_input( "author", "Author:", "Jack Black" ); this.#barcode = this._create_input( "barcode", "Barcode (EAN):", "123456789012...", (input) => { input.type = "number"; } ); this.#stock_count = this._create_input( "stock_count", "Stock count:", "10...", (input) => { input.type = "number"; } ); this._create_input( "image", "Product image:", "", (input) => { this.#image = input; input.type = "file"; input.accept = "image/*"; } ); } async #code_image() { if (this.#image.files.length === 0) { throw new Error("Upload image for product."); } const file = this.#image.files.item(0); const buffer = await file.arrayBuffer(); const list = new Uint8Array(buffer); let content = new String(); list.forEach((code) => { content += String.fromCharCode(code); }); return btoa(content); } async #submit() { const product = new product_base(); product.name = this.#name(); product.description = this.#description(); product.author = this.#author(); product.stock_count = this.#stock_count(); product.barcode = this.#barcode(); const image = await this.#code_image(); const request = new create_request(product, image); const response = await request.connect(); if (!response.result) { throw new Error(response.cause); } } async _process() { try { this._info = "Uploading..."; await this.#submit(); this._success = "Created success!"; searcher.reload(); setTimeout(() => { this.hide(); }, 500); } catch (error) { this._error = new String(error); } } }