summaryrefslogtreecommitdiff
path: root/src/core/components/layouts/BasicLayout.jsx
blob: 266223d88377d32712856fc55399ce7fab9a4d84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import dynamic from 'next/dynamic'
import BasicFooter from '../elements/Footer/BasicFooter'
import Image from 'next/image'
import whatsappUrl from '@/core/utils/whatsappUrl'
import { useEffect, useState } from 'react'
import axios from 'axios'
import odooApi from '@/core/api/odooApi'
import { useRouter } from 'next/router'
import productApi from '@/lib/product/api/productApi'
import { getAuth, setAuth } from '@/core/utils/auth'
import { createSlug, getIdFromSlug } from '@/core/utils/slug'
import { useSession } from 'next-auth/react'
import { setCookie } from 'cookies-next'

const Navbar = dynamic(() => import('../elements/Navbar/Navbar'))
const AnimationLayout = dynamic(() => import('./AnimationLayout'))

const BasicLayout = ({ children }) => {
  const [templateWA, setTemplateWA] = useState(null)
  const [payloadWA, setPayloadWa] = useState(null)
  const [urlPath, setUrlPath] = useState(null)

  const router = useRouter()

  useEffect(() => {
    const getIP = async () => {
      const ip = await odooApi('GET', '/api/ip-address')
      const data = {
        page_title: document.title,
        url: window.location.href,
        ip: ip
      }
      axios.get(`/api/user-activity?page_title=${data.page_title}&url=${data.url}&ip=${data.ip}`)
    }
    getIP()
    if (router.pathname === '/shop/product/[slug]') {
      const authToken = getAuth().token

      const { slug } = router.query
      const getProduct = async () => {
        let product = await productApi({ id: getIdFromSlug(slug), headers: { Token: authToken } })
        setPayloadWa({
          name: product[0]?.name,
          manufacture: product[0]?.manufacture.name,
          url: createSlug('/shop/product/', product[0]?.name, product[0]?.id, true)
        })
      }
      getProduct()
      setTemplateWA('product')

      setUrlPath(router.asPath)
    }
  }, [])
  return (
    <>
      <Navbar />
      <AnimationLayout>
        {children}
        <div className='fixed bottom-4 right-4 sm:bottom-14 sm:right-10 z-50'>
          <a
            href={whatsappUrl(templateWA, payloadWA, urlPath)}
            className='py-2 pl-3 pr-4 rounded-full bg-[#4FB84A] border border-green-300 flex items-center'
            rel='noopener noreferrer'
            target='_blank'
          >
            <Image
              src='/images/socials/WHATSAPP.svg'
              alt='Whatsapp'
              className='block sm:hidden'
              width={36}
              height={36}
            />
            <Image
              src='/images/socials/WHATSAPP.svg'
              alt='Whatsapp'
              className='hidden sm:block'
              width={44}
              height={44}
            />
            <span className='text-white font-bold ml-1.5'>Whatsapp</span>
          </a>
        </div>
      </AnimationLayout>
      <BasicFooter />
    </>
  )
}

export default BasicLayout