Guest User

Untitled

a guest
Sep 5th, 2024
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TypeScript 3.47 KB | Source Code | 0 0
  1. 'use client'
  2.  
  3. import { getAllContacts } from '@/actions/contact.action'
  4. import { ContactProps } from '@/types'
  5. import React, { useEffect, useState } from 'react'
  6. import {
  7.   Card,
  8.   CardContent,
  9.   CardDescription,
  10.   CardHeader,
  11.   CardTitle,
  12. } from './ui/card'
  13. import { Locale } from '@/i18n.config'
  14. import Link from 'next/link'
  15. import Image from 'next/image'
  16. import { Skeleton } from './ui/skeleton'
  17.  
  18. export default function Contacts({ lang }: { lang: Locale }) {
  19.   const [contacts, setContacts] = useState<ContactProps[]>([])
  20.   const [loading, setLoading] = useState(true)
  21.  
  22.   useEffect(() => {
  23.     async function fetchContacts() {
  24.       setLoading(true)
  25.       try {
  26.         const fetchedContacts = await getAllContacts(lang)
  27.         setContacts(fetchedContacts)
  28.       } catch (error) {
  29.         console.error('Error fetching:', error)
  30.       } finally {
  31.         setLoading(false)
  32.       }
  33.     }
  34.  
  35.     fetchContacts()
  36.   }, [lang])
  37.  
  38.   const visibleContacts = contacts
  39.     .filter((contact) => contact.isVisible)
  40.     .sort((a, b) => b.priority - a.priority)
  41.  
  42.   return (
  43.     <>
  44.       <h2 className="mb-4 text-3xl font-extrabold sm:text-4xl">Kontakty</h2>
  45.  
  46.       {loading && (
  47.         <div className="grid grid-cols-2 gap-4 sm:grid-cols-4 xl:grid-cols-6">
  48.           <ContactSkeleton />
  49.           <ContactSkeleton />
  50.           <ContactSkeleton />
  51.           <ContactSkeleton />
  52.           <ContactSkeleton />
  53.           <ContactSkeleton />
  54.           <ContactSkeleton />
  55.           <ContactSkeleton />
  56.           <ContactSkeleton />
  57.         </div>
  58.       )}
  59.  
  60.       {visibleContacts.length === 0 && !loading && (
  61.         <p className="text-xl font-semibold">Žádné kontakty</p>
  62.       )}
  63.  
  64.       <div className="grid grid-cols-2 gap-4 sm:grid-cols-4 xl:grid-cols-6">
  65.         {visibleContacts.map((contact, i) => (
  66.           <Contact {...contact} key={i} />
  67.         ))}
  68.       </div>
  69.     </>
  70.   )
  71. }
  72.  
  73. function Contact({
  74.   position,
  75.   description,
  76.   email,
  77.   phone,
  78.   isDrylock,
  79. }: ContactProps) {
  80.   return (
  81.     <Card className="relative col-span-2 sm:last:col-start-2 xl:last:col-start-auto">
  82.       {isDrylock && (
  83.         <div>
  84.           <Image
  85.             width={175}
  86.             height={40}
  87.             src={'/drylock.png'}
  88.             alt="drylock"
  89.             className="absolute right-[20px] top-[-12.5px] h-[25px] w-auto rounded-sm border bg-background px-1 py-0.5"
  90.           />
  91.         </div>
  92.       )}
  93.       <CardHeader className="pb-3">
  94.         <CardTitle>{position}</CardTitle>
  95.         <CardDescription>{description}</CardDescription>
  96.       </CardHeader>
  97.       <CardContent>
  98.         <p>
  99.           E-mail:{' '}
  100.           <Link
  101.             href={`mailto:${email}`}
  102.             className="md:transition-colors md:duration-100 md:hover:text-primary"
  103.           >
  104.             {email}
  105.           </Link>
  106.         </p>
  107.         <p>
  108.           Telefon:{' '}
  109.           <Link
  110.             href={`tel:${phone}`}
  111.             className="md:transition-colors md:duration-100 md:hover:text-primary"
  112.           >
  113.             {phone}
  114.           </Link>
  115.         </p>
  116.       </CardContent>
  117.     </Card>
  118.   )
  119. }
  120.  
  121. function ContactSkeleton() {
  122.   return (
  123.     <div className="relative col-span-2 rounded-md border p-6 shadow-sm sm:last:col-start-2 xl:last:col-start-auto">
  124.       <Skeleton className="mb-2 h-4 w-1/2" />
  125.  
  126.       <Skeleton className="mb-4 h-4 w-1/4" />
  127.  
  128.       <Skeleton className="mb-2 h-4 w-5/6" />
  129.       <Skeleton className="h-4 w-2/4" />
  130.     </div>
  131.   )
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment