summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2024-11-25 03:07:05 +0000
committerIT Fixcomart <it@fixcomart.co.id>2024-11-25 03:07:05 +0000
commitf254d47618ddfd2c1e37a51562b9728d62bd8eb6 (patch)
tree8b7d9837fbb0e3029b1b2461ce916330b3744591 /src
parente52f0ee9516d8601ffc92df9c085c2eee763c55c (diff)
parent4d1dd6ad5b22ffb02e4347cf3159f10988fd7c03 (diff)
Merged in CR/redis_v2 (pull request #383)
<iman> redis v2
Diffstat (limited to 'src')
-rw-r--r--src/lib/flashSale/components/FlashSale.jsx36
-rw-r--r--src/pages/api/flashsale-header.js40
-rw-r--r--src/pages/api/search-flashsale.js45
3 files changed, 108 insertions, 13 deletions
diff --git a/src/lib/flashSale/components/FlashSale.jsx b/src/lib/flashSale/components/FlashSale.jsx
index 8be1d7a6..6d90cad7 100644
--- a/src/lib/flashSale/components/FlashSale.jsx
+++ b/src/lib/flashSale/components/FlashSale.jsx
@@ -2,10 +2,8 @@ import Image from 'next/image';
import { useEffect, useState } from 'react';
import CountDown from '@/core/components/elements/CountDown/CountDown';
-import productSearchApi from '@/lib/product/api/productSearchApi';
import ProductSlider from '@/lib/product/components/ProductSlider';
-import flashSaleApi from '../api/flashSaleApi';
import { FlashSaleSkeleton } from '../skeleton/FlashSaleSkeleton';
const FlashSale = () => {
@@ -14,10 +12,14 @@ const FlashSale = () => {
useEffect(() => {
const loadFlashSales = async () => {
- const dataFlashSales = await flashSaleApi();
- setFlashSales(dataFlashSales);
+ const res = await fetch('/api/flashsale-header');
+ const { data } = await res.json();
+ if (data) {
+ setFlashSales(data);
+ }
setIsLoading(false);
};
+
loadFlashSales();
}, []);
@@ -53,7 +55,10 @@ const FlashSale = () => {
height={48}
className='w-full rounded mb-4 block sm:hidden'
/>
- <FlashSaleProduct flashSaleId={flashSale.pricelistId} />
+ <FlashSaleProduct
+ flashSaleId={flashSale.pricelistId}
+ duration={flashSale.duration}
+ />
</div>
</div>
))}
@@ -63,19 +68,24 @@ const FlashSale = () => {
);
};
-const FlashSaleProduct = ({ flashSaleId }) => {
+const FlashSaleProduct = ({ flashSaleId, duration }) => {
const [products, setProducts] = useState(null);
-
useEffect(() => {
+ const data_search = new URLSearchParams({
+ query: `fq=flashsale_id_i:${flashSaleId}&fq=flashsale_price_f:[1 TO *]&limit=500&orderBy=flashsale-price-asc&source=similar`,
+ operation: 'AND',
+ duration: `${duration}`,
+ });
const loadProducts = async () => {
- const dataProducts = await productSearchApi({
- query: `fq=flashsale_id_i:${flashSaleId}&fq=flashsale_price_f:[1 TO *]&limit=500&orderBy=flashsale-price-asc&source=similar`,
- operation: 'AND',
- });
- setProducts(dataProducts.response);
+ const res = await fetch(
+ `/api/search-flashsale?${data_search.toString()}`
+ );
+ const { data } = await res.json();
+ setProducts(data.response);
};
+
loadProducts();
- }, [flashSaleId]);
+ }, []);
return <ProductSlider products={products} />;
};
diff --git a/src/pages/api/flashsale-header.js b/src/pages/api/flashsale-header.js
new file mode 100644
index 00000000..31f8efdd
--- /dev/null
+++ b/src/pages/api/flashsale-header.js
@@ -0,0 +1,40 @@
+import odooApi from '@/core/api/odooApi';
+import { createClient } from 'redis';
+
+const client = createClient();
+
+client.on('error', (err) => console.error('Redis Client Error', err));
+
+const connectRedis = async () => {
+ if (!client.isOpen) {
+ await client.connect();
+ }
+};
+
+export default async function handler(req, res) {
+ try {
+ await connectRedis();
+ const cacheKey = `flashsale_header`;
+ // await client.del(cacheKey);
+ let cachedData = await client.get(cacheKey);
+
+ if (cachedData) {
+ const data = JSON.parse(cachedData);
+ return res.status(200).json({ data });
+ } else {
+ const flashSale = await odooApi('GET', `/api/v1/flashsale/header`);
+
+ await client.set(
+ cacheKey,
+ JSON.stringify(flashSale),
+ 'EX',
+ flashSale.duration
+ );
+ cachedData = await client.get(cacheKey);
+ return res.status(200).json({ data: cachedData });
+ }
+ } catch (error) {
+ console.error('Error interacting with Redis or fetching data:', error);
+ return res.status(500).json({ error: 'Internal Server Error' });
+ }
+}
diff --git a/src/pages/api/search-flashsale.js b/src/pages/api/search-flashsale.js
new file mode 100644
index 00000000..d9e56c83
--- /dev/null
+++ b/src/pages/api/search-flashsale.js
@@ -0,0 +1,45 @@
+import odooApi from '@/core/api/odooApi';
+import { createClient } from 'redis';
+import _ from 'lodash-contrib';
+import axios from 'axios';
+
+const client = createClient();
+
+client.on('error', (err) => console.error('Redis Client Error', err));
+
+const connectRedis = async () => {
+ if (!client.isOpen) {
+ await client.connect();
+ }
+};
+
+export default async function handler(req, res) {
+ const { query, operation, duration } = req.query;
+ try {
+ await connectRedis();
+ const cacheKey = `flashsale_product`;
+ // await client.del(cacheKey);
+ let cachedData = await client.get(cacheKey);
+
+ if (cachedData) {
+ const data = JSON.parse(cachedData);
+ return res.status(200).json({ data });
+ } else {
+ const dataProductSearch = await axios(
+ `${process.env.NEXT_PUBLIC_SELF_HOST}/api/shop/search?${query}&operation=${operation}]`
+ );
+
+ await client.set(
+ cacheKey,
+ JSON.stringify(dataProductSearch.data),
+ 'EX',
+ duration
+ );
+ cachedData = await client.get(cacheKey);
+ return res.status(200).json({ data: cachedData });
+ }
+ } catch (error) {
+ console.error('Error interacting with Redis or fetching data:', error);
+ return res.status(500).json({ error: 'Internal Server Error' });
+ }
+}