fix: live trade redirects only updating URL bar
This commit is contained in:
parent
0e68eb8c42
commit
4e58d20e84
3 changed files with 40 additions and 13 deletions
|
|
@ -26,9 +26,10 @@
|
|||
import { getPublicUrl, getTimeframeInSeconds } from '$lib/utils.js';
|
||||
import { websocketController, type PriceUpdate, isConnectedStore } from '$lib/stores/websocket';
|
||||
import SEO from '$lib/components/self/SEO.svelte';
|
||||
import { page } from '$app/state';
|
||||
|
||||
const { data } = $props();
|
||||
const coinSymbol = data.coinSymbol;
|
||||
let coinSymbol = $derived(data.coinSymbol);
|
||||
let coin = $state<any>(null);
|
||||
let loading = $state(true);
|
||||
let chartData = $state<any[]>([]);
|
||||
|
|
@ -63,6 +64,34 @@
|
|||
};
|
||||
});
|
||||
|
||||
// Handle route changes to update coin data
|
||||
$effect(() => {
|
||||
const currentCoinSymbol = page.params.coinSymbol;
|
||||
if (currentCoinSymbol && currentCoinSymbol !== coinSymbol) {
|
||||
loading = true;
|
||||
coin = null;
|
||||
chartData = [];
|
||||
volumeData = [];
|
||||
userHolding = 0;
|
||||
|
||||
// Unsubscribe from the old coin's updates
|
||||
websocketController.unsubscribeFromPriceUpdates(coinSymbol.toUpperCase());
|
||||
|
||||
// Update the data prop which will trigger coinSymbol to update via $derived
|
||||
data.coinSymbol = currentCoinSymbol;
|
||||
|
||||
// Load new coin data and set up new subscriptions
|
||||
loadCoinData().then(() => {
|
||||
loadUserHolding();
|
||||
websocketController.setCoin(currentCoinSymbol.toUpperCase());
|
||||
websocketController.subscribeToPriceUpdates(
|
||||
currentCoinSymbol.toUpperCase(),
|
||||
handlePriceUpdate
|
||||
);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
async function loadCoinData() {
|
||||
try {
|
||||
const response = await fetch(`/api/coin/${coinSymbol}?timeframe=${selectedTimeframe}`);
|
||||
|
|
|
|||
Reference in a new issue