Advertisement
EntropyStarRover

details

Mar 19th, 2021
67
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { html, render } from 'https://unpkg.com/lit-html?module';
  2. import page from "//unpkg.com/page/page.mjs"
  3.  
  4. function detailsTemplate(item) {
  5. return html`
  6. <div class="container">
  7. <div class="row space-top">
  8. <div class="col-md-12">
  9. <h1>Furniture Details</h1>
  10. </div>
  11. </div>
  12. <div class="row space-top">
  13. <div class="col-md-4">
  14. <div class="card text-white bg-primary">
  15. <div class="card-body">
  16. <img src=${item.img} />
  17. </div>
  18. </div>
  19. </div>
  20. <div class="col-md-4">
  21. <p>Make: <span>${item.make}</span></p>
  22. <p>Model: <span>${item.model}</span></p>
  23. <p>Year: <span>${item.year}</span></p>
  24. <p>Description: <span>${item.description}</span></p>
  25. <p>Price: <span>${item.price}</span></p>
  26. <p>Material: <span>${item.material}</span></p>
  27. ${item._ownerId==sessionStorage.getItem("userId")?html` <div>
  28. <a href="/edit/${item._id}" class="btn btn-info">Edit</a>
  29. <a href=”#” class="btn btn-red">Delete</a>
  30. </div>`:""}
  31.  
  32. </div>
  33. </div>
  34. </div>
  35. `
  36.  
  37. }
  38.  
  39. async function getCurrentItem(id) {
  40. let response = await fetch("http://localhost:3030/data/catalog/" + id);
  41. let currentItem = await response.json();
  42. return currentItem;
  43. }
  44.  
  45.  
  46. export async function showDetails(context) {
  47. let item = await (getCurrentItem(context.params.id))
  48. let container = document.getElementsByClassName("container")[0];
  49.  
  50. render(detailsTemplate(item),container);
  51.  
  52. let delBtn=document.getElementsByClassName("btn btn-red")[0];
  53. if (delBtn){
  54. delBtn.addEventListener("click", async function (e){
  55. e.preventDefault();
  56. let confirmation= confirm("Are you sure you want to delete this item?");
  57. if (confirmation){
  58.  
  59. let myHeaders = new Headers();
  60. myHeaders.append("Content-Type", "application/json");
  61. myHeaders.append("X-Authorization", sessionStorage.getItem("authToken"))
  62.  
  63. let options = {
  64. method: 'DELETE',
  65. headers: myHeaders,
  66. redirect: 'follow'
  67. };
  68. let response= await fetch("http://localhost:3030/data/catalog/"+item._id, options);
  69. let result=await response.json();
  70. if (result){
  71. page.redirect('/');
  72. }
  73. }
  74.  
  75. })
  76. }
  77. }
  78.  
  79.  
Advertisement
RAW Paste Data Copied
Advertisement