Advertisement
AngelKejov

Untitled

Nov 30th, 2022
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { deleteById, getById } from '../api/data.js';
  2. import {html, nothing} from '../lib.js';
  3.  
  4. const detailsTemplate = (shoe, hasUser, isOwner, onDelete) => html `
  5. <section id="details">
  6. <div id="details-wrapper">
  7.   <p id="details-title">Shoe Details</p>
  8.   <div id="img-wrapper">
  9.     <img src="${shoe.imageUrl}" alt="example1" />
  10.   </div>
  11.   <div id="info-wrapper">
  12.     <p>Brand: <span id="details-brand">${shoe.brand}</span></p>
  13.     <p>
  14.       Model: <span id="details-model">${shoe.model}</span>
  15.     </p>
  16.     <p>Release date: <span id="details-release">${shoe.release}</span></p>
  17.     <p>Designer: <span id="details-designer">${shoe.designer}</span></p>
  18.     <p>Value: <span id="details-value">${shoe.value}</span></p>
  19.   </div>
  20.  
  21.   ${hasUser ? html `
  22.     <div class="actionBtn">
  23.         ${isOwner ?  html `
  24.         <a href="/edit/${pet._id}" class="edit">Edit</a>
  25.         <a @click=${onDelete} href="javascript:void(0)" class="remove">Delete</a>` : nothing}
  26.     </div> ` : nothing}
  27. </div>
  28. </section>
  29. `;
  30.  
  31. export async function showDetails(ctx) {
  32.     const id = ctx.params.id;
  33.     const shoe = await getById(id);
  34.  
  35.     const hasUser = Boolean(ctx.user);
  36.     const isOwner = hasUser && ctx.user._id == shoe._ownerId;
  37.  
  38.     ctx.render(detailsTemplate(shoe, hasUser, isOwner, onDelete));
  39.  
  40.     async function onDelete() {
  41.         const choice = confirm('Are you sure you want to delete this shoe?');
  42.        
  43.         if(choice) {
  44.             await deleteById(id);
  45.             ctx.page.redirect('/')
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement