summaryrefslogtreecommitdiff
path: root/src-migrate/modules/page-content/index.tsx
blob: 54ee0a045f470b3bb23fa803baf61c9e07be1709 (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
import { useEffect, useMemo, useState } from 'react';
import { useQuery } from 'react-query';
import { PageContentProps } from '~/types/pageContent';
import { getPageContent } from '~/services/pageContent';

type Props = {
  path: string;
};

const PageContent = ({ path }: Props) => {
  const [localData, setData] = useState<PageContentProps>();
  const [shouldFetch, setShouldFetch] = useState(false);
  const [isLoading, setIsLoading] = useState(false);

  useEffect(() => {
    const fetchData = async () => {
      setIsLoading(true);
      const res = await fetch(`/api/page-content?path=${path}`);
      const { data } = await res.json();
      if (data) {
        setData(data);
      }
      setIsLoading(false);
    };

    fetchData();
  }, []);

  const parsedContent = useMemo<string>(() => {
    if (!localData) return '';
    return localData.content.replaceAll(
      'src="/web/image',
      `src="${process.env.NEXT_PUBLIC_ODOO_API_HOST}/web/image`
    );
  }, [localData]);
  if (isLoading) return <PageContentSkeleton />;

  return <div dangerouslySetInnerHTML={{ __html: parsedContent || '' }}></div>;
};

const PageContentSkeleton = () => (
  <div className='animate-pulse grid gap-y-4'>
    <div className='w-full h-10 bg-gray-300 rounded' />
    <div className='h-2' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-8/12 h-4 bg-gray-300 rounded' />
    <div className='h-2' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-full h-4 bg-gray-300 rounded' />
    <div className='w-1/2 h-4 bg-gray-300 rounded' />
  </div>
);

export default PageContent;