Skip to content

Commit

Permalink
fix: get_settings rate limit issue (#84)
Browse files Browse the repository at this point in the history
* fix: get_settings rate limit issue

* fix: apply comments from sourcery
  • Loading branch information
aum-deriv authored Jan 17, 2025
1 parent ecdb88e commit 4442092
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 46 deletions.
20 changes: 9 additions & 11 deletions src/components/CopierDashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { useEffect, useState } from "react";
import { Text, Snackbar, Spinner, SectionMessage } from "@deriv-com/quill-ui";
import PropTypes from "prop-types";
import useCopyTradersList from "../hooks/useCopyTradersList";
import CopyTradingBanner from "./CopyTradingBanner";
import useSettings from "../hooks/useSettings";
import useCopyStart from "../hooks/useCopyStart";
import useCopyStop from "../hooks/useCopyStop";
import AddTraderForm from "./AddTraderForm";
import TraderCard from "./TraderCard";

const CopierDashboard = () => {
const {
settings,
updateSettings,
fetchSettings,
isLoading: isSettingsLoading,
} = useSettings();
const CopierDashboard = ({ settings, updateSettings, fetchSettings }) => {
const showCopierBanner = settings?.allow_copiers === 1;
const { startCopyTrading, processingTrader: copyStartProcessingTrader } =
useCopyStart();
Expand Down Expand Up @@ -107,9 +101,7 @@ const CopierDashboard = () => {
}
};

const isPageLoading = isLoading || isSettingsLoading;

if (isPageLoading) {
if (isLoading) {
return (
<div className="flex justify-center items-center min-h-[200px]">
<Spinner />
Expand Down Expand Up @@ -176,4 +168,10 @@ const CopierDashboard = () => {
);
};

CopierDashboard.propTypes = {
settings: PropTypes.object,
updateSettings: PropTypes.func.isRequired,
fetchSettings: PropTypes.func.isRequired,
};

export default CopierDashboard;
43 changes: 25 additions & 18 deletions src/components/Dashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect } from "react";
import { useState } from "react";
import { SegmentedControlSingleChoice, Skeleton } from "@deriv-com/quill-ui";
import { useAuth } from "../hooks/useAuth.jsx";
import useSettings from "../hooks/useSettings.js";
Expand All @@ -7,20 +7,15 @@ import CopierDashboard from "./CopierDashboard";

const Dashboard = () => {
const { isLoading: authLoading } = useAuth();
const { settings, isLoading: settingsLoading } = useSettings();
const [userType, setUserType] = useState(() => {
// Set initial state based on settings if available
return settings?.allow_copiers ? "trader" : "copier";
});

useEffect(() => {
console.log("Settings updated:", settings);
if (settings) {
const newUserType = settings.allow_copiers ? "trader" : "copier";
console.log("Setting userType to:", newUserType);
setUserType(newUserType);
}
}, [settings]);
const {
settings,
isLoading: settingsLoading,
updateSettings,
fetchSettings,
} = useSettings();
const [userType, setUserType] = useState(
settings?.allow_copiers ? "trader" : "copier"
);

const isLoading = authLoading || settingsLoading;

Expand Down Expand Up @@ -75,11 +70,23 @@ const Dashboard = () => {
</div>
</div>
) : userType === "trader" ? (
<TraderDashboard />
<TraderDashboard
settings={settings}
updateSettings={updateSettings}
fetchSettings={fetchSettings}
/>
) : userType === "copier" ? (
<CopierDashboard />
<CopierDashboard
settings={settings}
updateSettings={updateSettings}
fetchSettings={fetchSettings}
/>
) : (
<CopierDashboard />
<CopierDashboard
settings={settings}
updateSettings={updateSettings}
fetchSettings={fetchSettings}
/>
)}
</div>
</div>
Expand Down
18 changes: 9 additions & 9 deletions src/components/TraderDashboard.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from "react";
import { Spinner, SectionMessage } from "@deriv-com/quill-ui";
import useSettings from "../hooks/useSettings";
import PropTypes from "prop-types";
import useCopyTradersList from "../hooks/useCopyTradersList";
import TraderStatistics from "./TraderStatistics";
import TraderBanner from "./TraderBanner";
Expand All @@ -9,13 +9,7 @@ import Settings from "./Settings";
import TraderDesktopNavigation from "./TraderDesktopNavigation";
import TraderMobileNavigation from "./TraderMobileNavigation";

const TraderDashboard = () => {
const {
settings,
isLoading: settingsLoading,
updateSettings,
fetchSettings,
} = useSettings();
const TraderDashboard = ({ settings, updateSettings, fetchSettings }) => {
const { traders, isLoading: tradersLoading } = useCopyTradersList();
const [selectedMenu, setSelectedMenu] = useState("statistics");

Expand All @@ -28,7 +22,7 @@ const TraderDashboard = () => {
}
};

if (settingsLoading || tradersLoading) {
if (tradersLoading) {
return (
<div className="flex justify-center items-center min-h-[200px]">
<Spinner />
Expand Down Expand Up @@ -83,4 +77,10 @@ const TraderDashboard = () => {
);
};

TraderDashboard.propTypes = {
settings: PropTypes.object,
updateSettings: PropTypes.func.isRequired,
fetchSettings: PropTypes.func.isRequired,
};

export default TraderDashboard;
28 changes: 20 additions & 8 deletions src/hooks/useSettings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from 'react';
import { useState, useEffect, useCallback, useRef } from 'react';
import useWebSocket from './useWebSocket';
import { useAuth } from '../hooks/useAuth.jsx';

Expand All @@ -8,14 +8,17 @@ const useSettings = () => {
const [isLoading, setIsLoading] = useState(true);
const { isAuthorized, isConnected } = useAuth();
const { sendMessage } = useWebSocket();
const hasInitialFetch = useRef(false);
const lastUpdateCall = useRef(null);

const fetchSettings = useCallback(() => {
if (!isConnected || !isAuthorized) {
if (!isConnected || !isAuthorized || hasInitialFetch.current) {
return;
}

setIsLoading(true);
console.log('Fetching user settings');
hasInitialFetch.current = true;
sendMessage(
{ get_settings: 1 },
(response) => {
Expand All @@ -32,18 +35,19 @@ const useSettings = () => {
);
}, [isConnected, isAuthorized, sendMessage]);

// Fetch settings when authorized
// Fetch settings on mount and when connection/auth state changes
useEffect(() => {
if (isConnected && isAuthorized && !settings) {
if (isConnected && isAuthorized) {
fetchSettings();
}
}, [isConnected, isAuthorized, settings, fetchSettings]);
}, [isConnected, isAuthorized, fetchSettings]);

// Reset settings when connection is lost
// Reset settings and fetch flag when connection is lost
useEffect(() => {
if (!isConnected) {
setSettings(null);
setIsLoading(true);
hasInitialFetch.current = false;
}
}, [isConnected]);

Expand All @@ -52,6 +56,13 @@ const useSettings = () => {
throw new Error('Not connected or authorized');
}

// Add debounce to prevent rapid settings updates
const now = Date.now();
if (lastUpdateCall.current && now - lastUpdateCall.current < 1000) {
throw new Error('Please wait before updating settings again');
}
lastUpdateCall.current = now;

return new Promise((resolve, reject) => {
sendMessage(
{ set_settings: 1, ...newSettings },
Expand All @@ -62,8 +73,9 @@ const useSettings = () => {
reject(response.error);
} else {
console.log('Settings updated:', response.set_settings);
setSettings(response.set_settings);
setError(null);
// After successful update, reset fetch flag and get fresh settings
hasInitialFetch.current = false;
fetchSettings();
resolve(response.set_settings);
}
}
Expand Down

0 comments on commit 4442092

Please sign in to comment.