product_editor.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { formscreen } from "./formscreen.js";
  2. import { edit_request } from "./edit_request.js";
  3. import { searcher } from "./searcher.js";
  4. import { edit_image_request } from "./edit_image_request.js";
  5. export class product_editor extends formscreen {
  6. #target;
  7. #name;
  8. #description;
  9. #author;
  10. #barcode;
  11. #stock_count;
  12. #image;
  13. constructor(target) {
  14. super();
  15. this.#target = target;
  16. }
  17. get target() {
  18. return this.#target;
  19. }
  20. get _name() {
  21. return "Product editor";
  22. }
  23. _build_form() {
  24. this.#name = this._create_input(
  25. "name",
  26. "Name:",
  27. "Sample...",
  28. (input) => {
  29. input.value = this.#target.name;
  30. }
  31. );
  32. this.#description = this._create_input(
  33. "description",
  34. "Description:",
  35. "This is sample product...",
  36. (input) => {
  37. input.value = this.#target.description;
  38. }
  39. );
  40. this.#author = this._create_input(
  41. "author",
  42. "Author:",
  43. "Jack Black",
  44. (input) => {
  45. input.value = this.#target.author;
  46. }
  47. );
  48. this.#barcode = this._create_input(
  49. "barcode",
  50. "Barcode (EAN):",
  51. "123456789012...",
  52. (input) => {
  53. input.type = "number";
  54. input.value = this.#target.barcode
  55. }
  56. );
  57. this.#stock_count = this._create_input(
  58. "stock_count",
  59. "Stock count:",
  60. "10...",
  61. (input) => {
  62. input.type = "number";
  63. input.value = this.#target.stock_count
  64. }
  65. );
  66. this._create_input(
  67. "image",
  68. "Change product image:",
  69. "",
  70. (input) => {
  71. this.#image = input;
  72. input.type = "file";
  73. input.accept = "image/*";
  74. }
  75. );
  76. }
  77. async #code_image() {
  78. if (this.#image.files.length === 0) {
  79. return null;
  80. }
  81. const file = this.#image.files.item(0);
  82. const buffer = await file.arrayBuffer();
  83. const list = new Uint8Array(buffer);
  84. let content = new String();
  85. list.forEach((code) => {
  86. content += String.fromCharCode(code);
  87. });
  88. return btoa(content);
  89. }
  90. async #submit() {
  91. const copy = this.#target.copy();
  92. copy.name = this.#name();
  93. copy.description = this.#description();
  94. copy.author = this.#author();
  95. copy.barcode = this.#barcode();
  96. copy.stock_count = this.#stock_count();
  97. const request = new edit_request(this.#target, copy);
  98. const response = await request.connect();
  99. if (!response.result) {
  100. throw new Error(response.cause);
  101. }
  102. this.#target = copy;
  103. }
  104. async #image_submit() {
  105. const image = await this.#code_image();
  106. if (image === null) {
  107. return;
  108. }
  109. const request = new edit_image_request(this.#target, image);
  110. const response = await request.connect();
  111. if (!response.result) {
  112. throw new Error(response.cause);
  113. }
  114. }
  115. async _process() {
  116. try {
  117. this._info = "Uploading...";
  118. await this.#submit();
  119. this._info = "Processing image...";
  120. await this.#image_submit();
  121. this._success = "Updated success!";
  122. searcher.reload();
  123. setTimeout(() => {
  124. this.hide();
  125. }, 500);
  126. } catch (error) {
  127. this._error = new String(error);
  128. }
  129. }
  130. }