fix: load profile data dynamically
This commit is contained in:
parent
a47f9654a6
commit
8cba222fe2
1 changed files with 19 additions and 2 deletions
|
|
@ -24,17 +24,33 @@
|
||||||
import { USER_DATA } from '$lib/stores/user-data';
|
import { USER_DATA } from '$lib/stores/user-data';
|
||||||
|
|
||||||
let { data } = $props();
|
let { data } = $props();
|
||||||
const username = data.username;
|
let username = $derived(data.username);
|
||||||
|
|
||||||
let profileData = $state<UserProfileData | null>(null);
|
let profileData = $state<UserProfileData | null>(null);
|
||||||
let recentTransactions = $state<any[]>([]);
|
let recentTransactions = $state<any[]>([]);
|
||||||
let loading = $state(true);
|
let loading = $state(true);
|
||||||
|
|
||||||
|
let previousUsername = $state<string | null>(null);
|
||||||
|
|
||||||
let isOwnProfile = $derived(
|
let isOwnProfile = $derived(
|
||||||
$USER_DATA && profileData?.profile && $USER_DATA.username === profileData.profile.username
|
$USER_DATA && profileData?.profile && $USER_DATA.username === profileData.profile.username
|
||||||
);
|
);
|
||||||
|
|
||||||
onMount(async () => {
|
onMount(async () => {
|
||||||
await fetchProfileData();
|
await fetchProfileData();
|
||||||
|
previousUsername = username;
|
||||||
|
});
|
||||||
|
|
||||||
|
$effect(() => {
|
||||||
|
if (username && previousUsername && username !== previousUsername) {
|
||||||
|
loading = true;
|
||||||
|
profileData = null;
|
||||||
|
recentTransactions = [];
|
||||||
|
|
||||||
|
fetchProfileData().then(() => {
|
||||||
|
previousUsername = username;
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
$effect(() => {
|
$effect(() => {
|
||||||
|
|
@ -42,12 +58,12 @@
|
||||||
fetchTransactions();
|
fetchTransactions();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
async function fetchProfileData() {
|
async function fetchProfileData() {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`/api/user/${username}`);
|
const response = await fetch(`/api/user/${username}`);
|
||||||
if (response.ok) {
|
if (response.ok) {
|
||||||
profileData = await response.json();
|
profileData = await response.json();
|
||||||
|
|
||||||
recentTransactions = profileData?.recentTransactions || [];
|
recentTransactions = profileData?.recentTransactions || [];
|
||||||
} else {
|
} else {
|
||||||
toast.error('Failed to load profile data');
|
toast.error('Failed to load profile data');
|
||||||
|
|
@ -170,6 +186,7 @@
|
||||||
render: (value: any) => formatDate(value)
|
render: (value: any) => formatDate(value)
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
const transactionsColumns = [
|
const transactionsColumns = [
|
||||||
{
|
{
|
||||||
key: 'type',
|
key: 'type',
|
||||||
|
|
|
||||||
Reference in a new issue