import_products.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { formscreen } from "./formscreen";
  2. import { database } from "./database.js";
  3. import { autocomplete_database } from "./autocomplete_database.js";
  4. import { import_loop } from "./import_loop.js";
  5. import { searcher } from "./searcher.js";
  6. export class import_products extends formscreen {
  7. #file;
  8. #content;
  9. get _name() {
  10. return "Import products JSON";
  11. }
  12. _build_form() {
  13. this._create_input("file", "Database:", "", (input) => {
  14. this.#file = input;
  15. input.type = "file";
  16. input.accept = "application/json";
  17. });
  18. }
  19. async #load_file() {
  20. if (this.#file.files.length === 0) {
  21. throw new Error("Select JSON products database first.");
  22. }
  23. const file = this.#file.files.item(0);
  24. const text = await file.text();
  25. return JSON.parse(text);
  26. }
  27. async _process() {
  28. try {
  29. this._info = "Loading file...";
  30. this.#content = await this.#load_file();
  31. this._info = "Parsing file to dataset...";
  32. const dataset = new database(this.#content)
  33. .on_skip((fail) => {
  34. this._info = "Skipping " + fail.product.barcode + "...";
  35. })
  36. .process()
  37. .results();
  38. const loop = new import_loop(dataset)
  39. .on_autocomplete((target) => {
  40. this._info = "Searching for " + target.barcode + "...";
  41. })
  42. .on_create((target) => {
  43. this._info = "Creating " + target.barcode + "...";
  44. })
  45. .on_single_fail((target) => {
  46. this._info = "Can not add " + target.barcode + "...";
  47. })
  48. .on_skip((target) => {
  49. this._info = "Skipping " + target.barcode + "...";
  50. })
  51. .on_single_success((target) => {
  52. this._info = "Created " + target.barcode + " success.";
  53. })
  54. .finally((broken) => {
  55. searcher.reload();
  56. if (broken.length === 0) {
  57. this._success = "All items imported.";
  58. setTimeout(() => {
  59. this.hide();
  60. });
  61. } else {
  62. console.log(broken);
  63. this._success = "Not all items imported...";
  64. }
  65. })
  66. .process();
  67. } catch (error) {
  68. console.log(error);
  69. this._error = new String(error);
  70. }
  71. }
  72. }