import_products.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. import { import_log } from "./import_log.js";
  7. import { downloader } from "./downloader.js";
  8. export class import_products extends formscreen {
  9. #file;
  10. #content;
  11. get _name() {
  12. return _("import-products-json");
  13. }
  14. _build_form() {
  15. this._create_input("file", "Database:", "", (input) => {
  16. this.#file = input;
  17. input.type = "file";
  18. input.accept = "application/json";
  19. });
  20. }
  21. async #load_file() {
  22. if (this.#file.files.length === 0) {
  23. throw new Error("select-products-json-database-first");
  24. }
  25. const file = this.#file.files.item(0);
  26. const text = await file.text();
  27. return JSON.parse(text);
  28. }
  29. async _process() {
  30. try {
  31. this._info = _("loading-file");
  32. this.#content = await this.#load_file();
  33. this._info = _("parsing-file-to-dataset");
  34. const result = new import_log();
  35. const dataset = new database(this.#content)
  36. .on_skip((fail) => {
  37. this._info = _("skipping-import-product-__barcode__").format({
  38. barcode: fail.product.barcode
  39. });
  40. result.skip(fail);
  41. })
  42. .process()
  43. .results();
  44. const loop = new import_loop(dataset)
  45. .on_autocomplete((target) => {
  46. this._info = _("searching-for-product-__barcode__").format({
  47. barcode: target.barcode
  48. });
  49. })
  50. .on_create((target) => {
  51. this._info = _("creating-product-__barcode__").format({
  52. barcode: target.barcode
  53. });
  54. })
  55. .on_single_fail((target) => {
  56. this._info = _("can-not-add-product-__barcode__").format({
  57. barcode: target.product.barcode
  58. })
  59. result.fail(target);
  60. })
  61. .on_skip((target) => {
  62. this._info = _("skipping-product-__barcode").format({
  63. barcode: target.product.barcode
  64. })
  65. result.skip(target);
  66. })
  67. .on_single_success((target) => {
  68. this._info = _("created-product-__barcode__").format({
  69. barcode: target.barcode
  70. })
  71. })
  72. .finally(() => {
  73. searcher.reload();
  74. const log = new downloader()
  75. .content(result.content())
  76. .type("text/plain")
  77. .encode("utf-8")
  78. .name("import-json.log")
  79. .process();
  80. if (result.length === 0) {
  81. this._success = _("all-items-imported");
  82. setTimeout(() => {
  83. this.hide();
  84. });
  85. } else {
  86. this._success = _("not-all-items-imported");
  87. }
  88. })
  89. .process();
  90. } catch (error) {
  91. this._error = new String(error);
  92. }
  93. }
  94. }