createPizza.render.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {setPizzasListToLS} from '../services/localStorage.service.js'
  2. import {createImgPath} from '../helpers/path.helper.js'
  3. import {compositionList} from '../data/compositionList.js'
  4. import {pizzaList} from '../data/pizzaList.js'
  5. import { renderCards } from './pizza.render.js';
  6. export function openCreatePizza() {
  7. const popup = document.getElementById('modal-create-pizza');
  8. // popup.classList.add('modal--active');
  9. const formCreatePizza = document.querySelector('.form-create-pizza');
  10. formCreatePizza.innerHTML = '';
  11. // create block pizza img
  12. const pizzaImgBox = document.createElement('div');
  13. pizzaImgBox.classList.add('form-group');
  14. pizzaImgBox.innerHTML = `
  15. <input id="img-upload" type="file">
  16. <img id="pizzaImg" src="" height="100" alt="Image preview...">`;
  17. formCreatePizza.appendChild(pizzaImgBox);
  18. document.querySelector('#img-upload').addEventListener('change', previewImg);
  19. // create block pizza name
  20. const pizzaTitle = document.createElement('div');
  21. pizzaTitle.classList.add('form-group');
  22. pizzaTitle.innerHTML = `<input type="text" class="pizza-name form-control" placeholder="Название пиццы...">`;
  23. formCreatePizza.appendChild(pizzaTitle);
  24. // create block for pizza compozition
  25. const pizzaCompositionBlock = document.createElement('div');
  26. pizzaCompositionBlock.className = 'form-group pizza-composition';
  27. // filling the block with ingredients
  28. for (const ingredient of compositionList) {
  29. const formCheck = document.createElement('div');
  30. formCheck.classList.add('form-check', 'form-check-inline');
  31. formCheck.innerHTML = `
  32. <input class="form-check-input" type="checkbox" id="checkbox${ingredient.id}" value="${ingredient.name}">
  33. <label class="form-check-label" for="checkbox${ingredient.id}">${ingredient.name}</label>`
  34. pizzaCompositionBlock.appendChild(formCheck);
  35. }
  36. formCreatePizza.appendChild(pizzaCompositionBlock);
  37. const btnCreatePizza = document.createElement('button');
  38. btnCreatePizza.className = 'btn btn-danger btn-create-pizza';
  39. btnCreatePizza.type = 'submit';
  40. btnCreatePizza.textContent = 'Создать';
  41. formCreatePizza.appendChild(btnCreatePizza);
  42. formCreatePizza.addEventListener('submit', function (e) {
  43. e.preventDefault();
  44. cretePizza();
  45. // popup.classList.remove('modal--active');
  46. })
  47. }
  48. function cretePizza() {
  49. const formCreatePizza = document.querySelector('.form-create-pizza');
  50. const pizzaImgSrc = document.getElementById('pizzaImg').src;
  51. const pizzaNameValue = formCreatePizza.querySelector('.pizza-name').value;
  52. const pizzaComposition = formCreatePizza.querySelector('.pizza-composition');
  53. const composition = pizzaComposition.querySelectorAll('input[type="checkbox"]');
  54. let compositionCheckedValue = Array.from(composition)
  55. .filter(item => item.checked)
  56. .map(item => item.value);
  57. let pizzaPrice = 0;
  58. let pizzaCaloricity = 100;
  59. for (const ingredient of compositionList) {
  60. compositionCheckedValue.forEach(item => {
  61. if (ingredient.name === item) {
  62. pizzaPrice += ingredient.price;
  63. pizzaCaloricity += ingredient.caloricity;
  64. }
  65. })
  66. }
  67. if (pizzaNameValue && compositionCheckedValue && pizzaPrice) {
  68. pizzaList.unshift({
  69. id: pizzaList.length + 1,
  70. img: pizzaImgSrc,
  71. name: pizzaNameValue,
  72. composition: compositionCheckedValue,
  73. caloricity: pizzaCaloricity,
  74. price: pizzaPrice,
  75. selfCreated: true,
  76. });
  77. setPizzasListToLS();
  78. renderCards(pizzaList);
  79. }
  80. }
  81. const convertToBase64 = file => {
  82. const fileReader = new FileReader();
  83. return new Promise((resolve, reject) => {
  84. fileReader.onerror = () => {
  85. fileReader.abort()
  86. reject("Problem parsing input file.");
  87. }
  88. fileReader.onload = () => {
  89. resolve(fileReader.result);
  90. }
  91. fileReader.readAsDataURL(file);
  92. })
  93. }
  94. const previewImg = async (e) => {
  95. const preview = document.getElementById('pizzaImg');
  96. const file = e.target.files[0];
  97. try {
  98. preview.src = file ? await convertToBase64(file) : "";
  99. } catch (e) {
  100. console.warn(e.message)
  101. }
  102. }