summaryrefslogtreecommitdiff
path: root/src/core/components/layouts/BasicLayout.jsx
blob: d6b96f3670840f857fda790a4cf6632407b696fc (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import dynamic from 'next/dynamic';
import Image from 'next/image';
import { useRouter } from 'next/router';
import { useEffect, useState, useRef } from 'react';

import { useProductContext } from '@/contexts/ProductContext';
import odooApi from '@/core/api/odooApi';
import whatsappUrl from '@/core/utils/whatsappUrl';
import Navbar from '../elements/Navbar/Navbar';
import styles from './BasicLayout.module.css'; // Import modul CSS
import useDevice from '@/core/hooks/useDevice';

const AnimationLayout = dynamic(() => import('./AnimationLayout'), {
  ssr: false,
});
const BasicFooter = dynamic(() => import('../elements/Footer/BasicFooter'), {
  ssr: false,
});

const BasicLayout = ({ children }) => {
  const [templateWA, setTemplateWA] = useState(null);
  const [payloadWA, setPayloadWa] = useState(null);
  const [urlPath, setUrlPath] = useState(null);
  const [highlight, setHighlight] = useState(false);
  const [buttonPosition, setButtonPosition] = useState(null);
  const [wobble, setWobble] = useState(false);

  const { isDesktop, isMobile } = useDevice();

  const router = useRouter();
  const buttonRef = useRef(null);

  const { product } = useProductContext();

  useEffect(() => {
    if (
      router.pathname === '/shop/product/[slug]' ||
      router.pathname === '/shop/product/variant/[slug]'
    ) {
      setPayloadWa({
        name: product?.name,
        manufacture: product?.manufacture.name,
        url: process.env.NEXT_PUBLIC_SELF_HOST + router.asPath,
      });
      setTemplateWA('product');

      setUrlPath(router.asPath);
    }
  }, [product, router]);

  useEffect(() => {
    const handleMouseOut = (event) => {
      const rect = buttonRef.current.getBoundingClientRect();
      if (event.clientY <= 0) {
        setButtonPosition(rect);
        setHighlight(true);
      } else {
        setHighlight(false);
      }
    };

    window.addEventListener('mouseout', handleMouseOut);

    return () => {
      window.removeEventListener('mouseout', handleMouseOut);
    };
  }, []);

  useEffect(() => {
    if (highlight) {
      // Set wobble animation after overlay highlight animation completes
      const timer = setTimeout(() => setWobble(true), 1000); // Adjust timing if needed
      return () => clearTimeout(timer);
    }
  }, [highlight]);

  const recordActivity = async (pathname) => {
    const ONLY_ON_PATH = false;
    const recordedPath = [];
    if (ONLY_ON_PATH && !recordedPath.includes(pathname)) return;

    const ip = await odooApi('GET', '/api/ip-address');
    const data = new URLSearchParams({
      page_title: document.title,
      url: window.location.href,
      ip,
    });

    fetch(`/api/user-activity?${data.toString()}`);
  };

  useEffect(() => {
    recordActivity(router.pathname);
  }, [router.pathname]);

  return (
    <>
      {highlight && buttonPosition && (
        <div
          className={styles['overlay-highlight']}
          style={{
            '--button-x': `${buttonPosition.x + buttonPosition.width / 2}px`,
            '--button-y': `${buttonPosition.y + buttonPosition.height / 2}px`,
            '--button-radius': `${
              Math.max(buttonPosition.width, buttonPosition.height) / 2
            }px`,
          }}
          onAnimationEnd={() => setHighlight(false)}
        />
      )}
      <Navbar />
      <AnimationLayout>
        {children}
        <div className='fixed bottom-16 right-4 sm:bottom-14 sm:right-10 z-50'>
          <div className='flex flex-row items-center'>
            <a
              href={whatsappUrl(templateWA, payloadWA, urlPath)}
              className='flex flex-row items-center'
              rel='noopener noreferrer'
              target='_blank'
            >
              <span
                className={`text-green-300 text-lg font-bold mr-4 ${
                  wobble ? 'animate-wobble' : ''
                }`}
                onAnimationEnd={() => setWobble(false)}
              >
                {isDesktop && 'Whatsapp'}
              </span>
            </a>
            <a
              href={whatsappUrl(templateWA, payloadWA, urlPath)}
              className='elemen-whatsapp p-4 rounded-full bg-[#4FB84A] border border-green-300 flex items-center'
              rel='noopener noreferrer'
              target='_blank'
              ref={buttonRef}
            >
              <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}
              />
            </a>
          </div>
        </div>
      </AnimationLayout>
      <BasicFooter />
    </>
  );
};

export default BasicLayout;