Skip to content

Commit 7f72943

Browse files
committed
chore: shuffle on client
1 parent 9b2fd18 commit 7f72943

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

app/pages/_components/SectionStaff.vue

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<script setup lang="ts">
22
import { HOME_HEADING_ID } from "~/constant";
3-
import { useI18n, useFetch, useBreakpoint, computed } from "#imports";
3+
import { useI18n, useFetch, useBreakpoint, computed, onMounted } from "#imports";
44
import { VFSection } from "#components";
55
66
import StaffGrid from "./StaffGrid.vue";
7+
import type { Staff } from "~~/server/api/staffs/index.get";
78
89
const { t } = useI18n();
910
const bp = useBreakpoint();
1011
11-
const { data: staffList } = await useFetch("/api/staffs");
12+
const { data: staffList } = await useFetch("/api/staffs", { deep: true });
1213
1314
const leaderColumns = computed(() => {
1415
return bp.value === "pc" ? 3 : 2;
@@ -17,6 +18,27 @@ const leaderColumns = computed(() => {
1718
const coreColumns = computed(() => {
1819
return bp.value === "pc" ? 4 : 3;
1920
});
21+
22+
onMounted(() => {
23+
if (!staffList.value) return;
24+
staffList.value.leaders = shuffleNonPinned(staffList.value.leaders);
25+
staffList.value.cores = shuffleNonPinned(staffList.value.cores);
26+
});
27+
function shuffleArray<T>(array: T[]): T[] {
28+
const shuffled = [...array];
29+
for (let i = shuffled.length - 1; i > 0; i--) {
30+
const j = Math.floor(Math.random() * (i + 1));
31+
[shuffled[i]!, shuffled[j]!] = [shuffled[j]!, shuffled[i]!];
32+
}
33+
return shuffled;
34+
}
35+
36+
function shuffleNonPinned(staffArray: Staff[]): Staff[] {
37+
const pinned = staffArray.filter(staff => staff.pinned);
38+
const nonPinned = staffArray.filter(staff => !staff.pinned);
39+
const shuffledNonPinned = shuffleArray(nonPinned);
40+
return [...pinned, ...shuffledNonPinned];
41+
}
2042
</script>
2143

2244
<template>

0 commit comments

Comments
 (0)