Advertisement
bebo231312312321

Untitled

Jul 29th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. import { html } from "../../node_modules/lit-html/lit-html.js"
  3. import { getBook, getApplication, getUserApplication } from "../data/applications.js"
  4. import { deleteOffer, getById } from "../data/offers.js"
  5. import { getUserData } from "../util.js"
  6. //TODO replace with actual template
  7.  
  8. const detailsTemplate = (fact,onDelete,onLike) => html`
  9.        <section id="details">
  10.           <div id="details-wrapper">
  11.             <img id="details-img" src="${fact.imageUrl}" alt="example1" />
  12.             <p id="details-category">${fact.category}</p>
  13.             <div id="info-wrapper">
  14.               <div id="details-description">
  15.                 <p id="description">${fact.description}</p>
  16.                    <p id ="more-info">${fact.moreInfo}</p>
  17.               </div>
  18.  
  19.               <h3>Likes:<span id="likes">${fact.likes}</span></h3>
  20.  
  21.           <div id="action-buttons">
  22.           ${fact.canEdit ? html`<a href="/catalog/${fact._id}/edit" id="edit-btn">Edit</a>
  23.             <a @click=${onDelete} href="javascript:void(0)" id="delete-btn">Delete</a>`: null}
  24.  
  25.  
  26.             ${fact.canLike ? html`<a @click=${onLike}  href="javascript:void(0)" id="like-btn">Like</a>`:null}
  27.  
  28.           </div>
  29.             </div>
  30.         </div>
  31.       </section>`
  32. //
  33. export async function detailsPage(ctx) {
  34.     const id = ctx.params.id
  35.     const userData = getUserData()
  36.    
  37.     const request = [
  38.         getById(id),
  39.         getApplication(id)
  40.     ]
  41.  
  42.         if(userData){
  43.             request.push(getUserApplication(id, userData._id))
  44.         }
  45.         const [fact, likes, hasLikes] = await Promise.all(request)
  46.         fact.likes= likes
  47.  
  48.     if(userData && userData._id == fact._ownerId){
  49.       fact.canEdit = true
  50.     }
  51.     if(userData && userData._id !== fact._ownerId && hasLikes==0){
  52.       fact.canLike=true
  53.     }
  54.  
  55.     async function onDelete(){
  56.         const choice = confirm('Are you sure you want to delete простотия')
  57.         if(choice){
  58.             await deleteOffer(id)
  59.             ctx.page.redirect('/catalog')
  60.         }
  61.     }
  62.     async function onLike(){
  63.         await getBook(id)
  64.         ctx.page.redirect(`/catalog/${id}`)
  65.     }
  66.  
  67.     ctx.render(detailsTemplate(fact,onDelete,onLike))
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement