Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memoised function to prevent multiple api calls #256

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions components/stock-operation-modal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,20 @@ const StockOperationModal = (props) => {
}, []);

const getUserWallet = async () => {
const [apiResponse, setApiResponse] = useState(undefined);
Aryan-9211 marked this conversation as resolved.
Show resolved Hide resolved
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;
Expand Down
5 changes: 5 additions & 0 deletions constants.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
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',
PROFILE: 'https://my.realdevsquad.com/',
};

const NAV_MENU = [
{
name: 'Home',
Expand Down Expand Up @@ -46,6 +50,7 @@ const NAV_MENU = [

const LOGIN_URL =
'https://github.com/login/oauth/authorize?client_id=23c78f66ab7964e5ef97';

export {
BASE_IMAGE_URL,
BASE_API_URL,
Expand Down
6 changes: 6 additions & 0 deletions utils/fetchSelfDetails.js
Original file line number Diff line number Diff line change
@@ -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;
};

Expand Down