From 0754c3a5792e41537beabb3c56d35024122cc2e8 Mon Sep 17 00:00:00 2001 From: Aryan Date: Wed, 16 Mar 2022 12:33:09 +0530 Subject: [PATCH] memoised function to prevent multiple api calls --- components/stock-operation-modal/index.js | 9 +++++++++ constants.js | 5 +++++ utils/fetchSelfDetails.js | 6 ++++++ 3 files changed, 20 insertions(+) diff --git a/components/stock-operation-modal/index.js b/components/stock-operation-modal/index.js index 60fc708..ed04d76 100644 --- a/components/stock-operation-modal/index.js +++ b/components/stock-operation-modal/index.js @@ -38,11 +38,20 @@ const StockOperationModal = (props) => { }, []); const getUserWallet = async () => { + const [apiResponse, setApiResponse] = useState(undefined); + if (apiResponse) { + const { + currencies: { dinero }, + } = wallet; + setUserMoney(dinero); + return; + } const response = await fetchData(`${BASE_API_URL}/wallet`, 'GET', { credentials: 'include', }); const { wallet } = await response.json(); if (Object.keys(wallet).length === 0) return setUserMoney(0); + setApiResponse(wallet); const { currencies: { dinero }, } = wallet; diff --git a/constants.js b/constants.js index 964781f..ca8107f 100644 --- a/constants.js +++ b/constants.js @@ -1,12 +1,15 @@ const BASE_IMAGE_URL = 'https://raw.githubusercontent.com/Real-Dev-Squad/website-static/main/members'; + const BASE_API_URL = process.env.NEXT_PUBLIC_BASE_API_URL; const USER_DATA_URL = `${BASE_API_URL}/users/self`; const WALLET_URL = `${BASE_API_URL}/wallet`; + const CURRENCIES = { NEELAM: 'neelam', DINERO: 'dinero', }; + const PATHS = { HOME: 'https://www.realdevsquad.com', WELCOME: 'https://welcome.realdevsquad.com/', @@ -16,8 +19,10 @@ const PATHS = { STATUS: 'https://status.realdevsquad.com/', PROFILE: 'https://my.realdevsquad.com/', }; + const LOGIN_URL = 'https://github.com/login/oauth/authorize?client_id=23c78f66ab7964e5ef97'; + export { BASE_IMAGE_URL, BASE_API_URL, diff --git a/utils/fetchSelfDetails.js b/utils/fetchSelfDetails.js index 7b7e44f..f543fe8 100644 --- a/utils/fetchSelfDetails.js +++ b/utils/fetchSelfDetails.js @@ -1,11 +1,17 @@ +import React, { useState } from 'react'; import fetchData from './fetchData'; const BASE_API_URL = process.env.NEXT_PUBLIC_BASE_API_URL; const fetchSelfDetails = async () => { + const [apiResponse, setApiResponse] = useState(undefined); + if (apiResponse) { + return apiResponse; + } const userData = await fetchData(`${BASE_API_URL}/users/self`, 'GET', { credentials: 'include', }); + setApiResponse(userData); return userData; };