Skip to content

Commit f6de5f3

Browse files
committed
small code refactoring and "loading..."-state when fetching takes long
1 parent 84aca15 commit f6de5f3

1 file changed

Lines changed: 75 additions & 41 deletions

File tree

website/src/pages/FriendlistView.vue

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
<Navbar />
33
<div class="friendlist-page">
44
<FriendSearchBar v-model:search="search" />
5-
<div v-if="search" class="search-results-section">
6-
<h3 class="result-headline">Search Results</h3>
5+
<div v-if="loading" class="loading-indicator">
6+
Loading...
7+
</div>
8+
<template v-else>
9+
<div v-if="search" class="search-results-section">
10+
<h3 class="result-headline">Search Results</h3>
11+
<div class="friend-grid">
12+
<FriendCard
13+
v-for="user in filteredUserResults"
14+
:key="user.id"
15+
:friend="user"
16+
:isFriend="false"
17+
@add-friend="addFriend"
18+
/>
19+
</div>
20+
</div>
721
<div class="friend-grid">
822
<FriendCard
9-
v-for="user in filteredUserResults"
10-
:key="user.id"
11-
:friend="user"
12-
:isFriend="false"
13-
@add-friend="addFriend"
23+
v-for="friend in filteredFriends"
24+
:key="friend.id"
25+
:friend="friend"
26+
:isFriend="true"
27+
@unfollow="unfollowFriend"
1428
/>
1529
</div>
16-
</div>
17-
<div class="friend-grid">
18-
<FriendCard
19-
v-for="friend in filteredFriends"
20-
:key="friend.id"
21-
:friend="friend"
22-
:isFriend="true"
23-
@unfollow="unfollowFriend"
24-
/>
25-
</div>
30+
</template>
2631
</div>
2732
</template>
2833

@@ -33,37 +38,61 @@ import FriendSearchBar from '@/components/FriendSearchBar.vue';
3338
import FriendCard from '@/components/FriendCard.vue';
3439
import backendApi from '@/api/backend-api';
3540
41+
interface User {
42+
id: string;
43+
username: string;
44+
email: string;
45+
rocketPoints: number;
46+
steps?: number;
47+
image?: string;
48+
}
49+
3650
const search = ref('');
37-
const friends = ref<any[]>([]);
38-
const allUsers = ref<any[]>([]);
51+
const friends = ref<User[]>([]);
52+
const allUsers = ref<User[]>([]);
3953
4054
const fetchFriends = async () => {
41-
const res = await backendApi.getFriends();
42-
friends.value = res.data.map((f: any) => ({
43-
id: f.id,
44-
username: f.username,
45-
email: f.email,
46-
rocketPoints: f.rocket_points,
47-
steps: f.steps,
48-
image: f.image_data ? `data:image/png;base64,${f.image_data}` : undefined,
49-
}));
55+
try {
56+
const res = await backendApi.getFriends();
57+
friends.value = res.data.map((f: any) => ({
58+
id: f.id,
59+
username: f.username,
60+
email: f.email,
61+
rocketPoints: f.rocket_points,
62+
steps: f.steps,
63+
image: f.image_data ? `data:image/png;base64,${f.image_data}` : undefined,
64+
})) as User[];
65+
} catch (e) {
66+
console.error('Failed to fetch friends:', e);
67+
friends.value = [];
68+
}
69+
5070
};
5171
5272
const fetchAllUsers = async () => {
53-
const res = await backendApi.getAllUsers();
54-
allUsers.value = res.data.map((u: any) => ({
55-
id: u.id,
56-
username: u.username,
57-
email: u.email,
58-
rocketPoints: u.rocket_points,
59-
steps: u.steps,
60-
image: u.image_data ? `data:image/png;base64,${u.image_data}` : undefined,
61-
}));
73+
try {
74+
const res = await backendApi.getAllUsers();
75+
allUsers.value = res.data.map((u: any) => ({
76+
id: u.id,
77+
username: u.username,
78+
email: u.email,
79+
rocketPoints: u.rocket_points,
80+
steps: u.steps,
81+
image: u.image_data ? `data:image/png;base64,${u.image_data}` : undefined,
82+
}));
83+
} catch (e) {
84+
console.error('Failed to fetch all users:', e);
85+
allUsers.value = [];
86+
}
87+
6288
};
6389
64-
onMounted(() => {
65-
fetchFriends();
66-
fetchAllUsers();
90+
const loading = ref(true);
91+
92+
onMounted(async () => {
93+
loading.value = true;
94+
await Promise.all([fetchFriends(), fetchAllUsers()]);
95+
loading.value = false;
6796
});
6897
6998
const filteredFriends = computed(() =>
@@ -86,7 +115,7 @@ const unfollowFriend = async (id: string) => {
86115
friends.value = friends.value.filter(f => f.id !== id);
87116
};
88117
89-
const addFriend = async (user: any) => {
118+
const addFriend = async (user: User) => {
90119
try {
91120
await backendApi.addFriend(user.username);
92121
await fetchFriends();
@@ -121,4 +150,9 @@ const addFriend = async (user: any) => {
121150
.search-results-section {
122151
margin-bottom: 6rem;
123152
}
153+
.loading-indicator {
154+
text-align: center;
155+
font-size: 1.2rem;
156+
margin: 2rem 0;
157+
}
124158
</style>

0 commit comments

Comments
 (0)