| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import { formscreen } from "./formscreen";
- import { database } from "./database.js";
- import { autocomplete_database } from "./autocomplete_database.js";
- import { import_loop } from "./import_loop.js";
- import { searcher } from "./searcher.js";
- export class import_products extends formscreen {
- #file;
- #content;
- get _name() {
- return "Import products JSON";
- }
- _build_form() {
- this._create_input("file", "Database:", "", (input) => {
- this.#file = input;
- input.type = "file";
- input.accept = "application/json";
- });
- }
- async #load_file() {
- if (this.#file.files.length === 0) {
- throw new Error("Select JSON products database first.");
- }
- const file = this.#file.files.item(0);
- const text = await file.text();
-
- return JSON.parse(text);
- }
- async _process() {
- try {
- this._info = "Loading file...";
- this.#content = await this.#load_file();
-
- this._info = "Parsing file to dataset...";
-
- const dataset = new database(this.#content)
- .on_skip((fail) => {
- this._info = "Skipping " + fail.product.barcode + "...";
- })
- .process()
- .results();
- const loop = new import_loop(dataset)
- .on_autocomplete((target) => {
- this._info = "Searching for " + target.barcode + "...";
- })
- .on_create((target) => {
- this._info = "Creating " + target.barcode + "...";
- })
- .on_single_fail((target) => {
- this._info = "Can not add " + target.barcode + "...";
- })
- .on_skip((target) => {
- this._info = "Skipping " + target.barcode + "...";
- })
- .on_single_success((target) => {
- this._info = "Created " + target.barcode + " success.";
- })
- .finally((broken) => {
- searcher.reload();
-
- if (broken.length === 0) {
- this._success = "All items imported.";
- setTimeout(() => {
- this.hide();
- });
- } else {
- console.log(broken);
- this._success = "Not all items imported...";
- }
- })
- .process();
- } catch (error) {
- console.log(error);
- this._error = new String(error);
- }
- }
- }
|