Skip to content

Commit 9a75530

Browse files
committed
Added date range for ranking and update loading skelleton of festival page
1 parent 07497c2 commit 9a75530

File tree

8 files changed

+141
-8
lines changed

8 files changed

+141
-8
lines changed

app/[locale]/festival/_components/festival-tabs.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const FestivalTabs = async () => {
3030
<Ranking
3131
highlines_ids={highline_ids || []}
3232
visibleCategories={["cadenas", "distance", "fullLine"]}
33+
startDate={new Date("2024-05-30")}
34+
endDate={new Date("2024-06-07")}
3335
/>
3436
</TabsContent>
3537
<TabsContent className="mt-4" value="highlines">

app/[locale]/festival/page.tsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { Suspense } from "react";
55
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
66

77
import { FestivalTabs } from "./_components/festival-tabs";
8+
import { Loader2 } from "lucide-react";
89

910
type Props = {
1011
params: { locale: string; username: string };
@@ -45,7 +46,13 @@ export default function Festival({
4546
{/* <TabsContent value="schedule">
4647
<div>foo</div>
4748
</TabsContent> */}
48-
<Suspense fallback={<div>loading...</div>}>
49+
<Suspense
50+
fallback={
51+
<div className="mt-12 grid w-full place-items-center">
52+
<Loader2 className="h-20 w-20 animate-spin text-primary"></Loader2>
53+
</div>
54+
}
55+
>
4956
<FestivalTabs />
5057
</Suspense>
5158
</Tabs>

components/Ranking/Cadenas.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import LoadingSkeleton from "./LoadingSkeleton";
99

1010
interface Props {
1111
highlines_ids: string[];
12+
startDate?: Date;
13+
endDate?: Date;
1214
}
1315

1416
const PAGE_SIZE = 5;
1517

16-
function Cadenas({ highlines_ids }: Props) {
18+
function Cadenas({ highlines_ids, startDate, endDate }: Props) {
1719
const supabase = useSupabaseBrowser();
1820

1921
async function fetchCadenas({ pageParam = 1 }) {
2022
const { data, error } = await supabase.rpc("get_total_cadenas", {
2123
highline_ids: highlines_ids,
2224
page_number: pageParam,
2325
page_size: PAGE_SIZE,
26+
start_date: startDate?.toISOString(),
27+
end_date: endDate?.toISOString(),
2428
});
2529
return data;
2630
}

components/Ranking/Distance.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import LoadingSkeleton from "./LoadingSkeleton";
99

1010
interface Props {
1111
highlines_ids: string[];
12+
startDate?: Date;
13+
endDate?: Date;
1214
}
1315

1416
const PAGE_SIZE = 5;
1517

16-
function Distance({ highlines_ids }: Props) {
18+
function Distance({ highlines_ids, startDate, endDate }: Props) {
1719
const supabase = useSupabaseBrowser();
1820

1921
async function fetchEntries({ pageParam = 1 }) {
2022
const { data, error } = await supabase.rpc("get_total_walked", {
2123
highline_ids: highlines_ids,
2224
page_number: pageParam,
2325
page_size: PAGE_SIZE,
26+
start_date: startDate?.toISOString(),
27+
end_date: endDate?.toISOString(),
2428
});
2529
return data;
2630
}

components/Ranking/FullLine.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import LoadingSkeleton from "./LoadingSkeleton";
99

1010
interface Props {
1111
highlines_ids: string[];
12+
startDate?: Date;
13+
endDate?: Date;
1214
}
1315

1416
const PAGE_SIZE = 5;
1517

16-
function FullLine({ highlines_ids }: Props) {
18+
function FullLine({ highlines_ids, startDate, endDate }: Props) {
1719
const supabase = useSupabaseBrowser();
1820

1921
async function fetchFullLine({ pageParam = 1 }) {
2022
const { data } = await supabase.rpc("get_total_full_lines", {
2123
highline_ids: highlines_ids,
2224
page_number: pageParam,
2325
page_size: PAGE_SIZE,
26+
start_date: startDate?.toISOString(),
27+
end_date: endDate?.toISOString(),
2428
});
2529
return data;
2630
}

components/Ranking/index.tsx

+26-4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import Speedline from "./Speedline";
1111
interface Props {
1212
highlines_ids: string[];
1313
visibleCategories?: Category[];
14+
startDate?: Date;
15+
endDate?: Date;
1416
}
1517

1618
export type Category = "speedline" | "distance" | "cadenas" | "fullLine";
@@ -19,17 +21,35 @@ const CategoryRenderer: React.FC<
1921
Props & {
2022
category: Category;
2123
}
22-
> = ({ category, highlines_ids, visibleCategories }) => {
24+
> = ({ category, highlines_ids, visibleCategories, startDate, endDate }) => {
2325
if (!visibleCategories?.includes(category)) return null;
2426
switch (category) {
2527
case "speedline":
2628
return <Speedline highlines_id={highlines_ids[0]} />;
2729
case "distance":
28-
return <Distance highlines_ids={highlines_ids} />;
30+
return (
31+
<Distance
32+
highlines_ids={highlines_ids}
33+
startDate={startDate}
34+
endDate={endDate}
35+
/>
36+
);
2937
case "cadenas":
30-
return <Cadenas highlines_ids={highlines_ids} />;
38+
return (
39+
<Cadenas
40+
highlines_ids={highlines_ids}
41+
startDate={startDate}
42+
endDate={endDate}
43+
/>
44+
);
3145
case "fullLine":
32-
return <FullLine highlines_ids={highlines_ids} />;
46+
return (
47+
<FullLine
48+
highlines_ids={highlines_ids}
49+
startDate={startDate}
50+
endDate={endDate}
51+
/>
52+
);
3353
default:
3454
return null;
3555
}
@@ -38,6 +58,8 @@ const CategoryRenderer: React.FC<
3858
export const Ranking: React.FC<Props> = ({
3959
highlines_ids,
4060
visibleCategories = ["cadenas", "distance", "fullLine", "speedline"], // All categories visible by default,
61+
startDate,
62+
endDate,
4163
}) => {
4264
const { searchParams } = useQueryString();
4365
const selectedCategory =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
set check_function_bodies = off;
2+
3+
DROP FUNCTION IF EXISTS public.get_total_cadenas (
4+
highline_ids UUID[],
5+
page_number INTEGER,
6+
page_size INTEGER
7+
);
8+
9+
CREATE OR REPLACE FUNCTION public.get_total_cadenas(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone)
10+
RETURNS TABLE(instagram text, total_cadenas integer, profile_picture text)
11+
LANGUAGE plpgsql
12+
AS $function$
13+
BEGIN
14+
RETURN QUERY
15+
SELECT e.instagram, SUM(e.cadenas) AS total_cadenas, COALESCE(p.profile_picture, '') AS profile_picture
16+
FROM public.entry e
17+
LEFT JOIN public.profiles p ON e.instagram = p.username
18+
WHERE e.highline_id = ANY(get_total_cadenas.highline_ids)
19+
AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL)
20+
AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL)
21+
GROUP BY e.instagram, p.profile_picture
22+
HAVING SUM(e.cadenas) > 0
23+
ORDER BY total_cadenas DESC
24+
OFFSET (get_total_cadenas.page_number - 1) * get_total_cadenas.page_size
25+
LIMIT get_total_cadenas.page_size;
26+
END;
27+
$function$
28+
;
29+
30+
DROP FUNCTION IF EXISTS public.get_total_full_lines (
31+
highline_ids UUID[],
32+
page_number INTEGER,
33+
page_size INTEGER
34+
);
35+
36+
CREATE OR REPLACE FUNCTION public.get_total_full_lines(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone)
37+
RETURNS TABLE(instagram text, total_full_lines integer, profile_picture text)
38+
LANGUAGE plpgsql
39+
AS $function$
40+
BEGIN
41+
RETURN QUERY
42+
SELECT e.instagram, SUM(e.full_lines) AS total_full_lines, COALESCE(p.profile_picture, '') AS profile_picture
43+
FROM public.entry e
44+
LEFT JOIN public.profiles p ON e.instagram = p.username
45+
WHERE e.highline_id = ANY(get_total_full_lines.highline_ids)
46+
AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL)
47+
AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL)
48+
GROUP BY e.instagram, p.profile_picture
49+
HAVING SUM(e.full_lines) > 0
50+
ORDER BY total_full_lines DESC
51+
OFFSET (get_total_full_lines.page_number - 1) * get_total_full_lines.page_size
52+
LIMIT get_total_full_lines.page_size;
53+
END;
54+
$function$
55+
;
56+
57+
DROP FUNCTION IF EXISTS public.get_total_walked (
58+
highline_ids UUID[],
59+
page_number INTEGER,
60+
page_size INTEGER
61+
);
62+
63+
CREATE OR REPLACE FUNCTION public.get_total_walked(highline_ids uuid[], page_number integer, page_size integer, start_date timestamp with time zone DEFAULT NULL::timestamp with time zone, end_date timestamp with time zone DEFAULT NULL::timestamp with time zone)
64+
RETURNS TABLE(instagram text, total_distance_walked integer, profile_picture text)
65+
LANGUAGE plpgsql
66+
AS $function$
67+
BEGIN
68+
RETURN QUERY
69+
SELECT e.instagram, SUM(e.distance_walked) AS total_distance_walked, COALESCE(p.profile_picture, '') AS profile_picture
70+
FROM public.entry e
71+
LEFT JOIN public.profiles p ON e.instagram = p.username
72+
WHERE e.highline_id = ANY(get_total_walked.highline_ids)
73+
AND (e.created_at >= COALESCE(start_date, '1970-01-01'::timestamp) OR start_date IS NULL)
74+
AND (e.created_at <= COALESCE(end_date, now()) OR end_date IS NULL)
75+
AND e.distance_walked IS NOT NULL
76+
GROUP BY e.instagram, p.profile_picture
77+
ORDER BY total_distance_walked DESC
78+
OFFSET (get_total_walked.page_number - 1) * get_total_walked.page_size
79+
LIMIT page_size;
80+
END;
81+
$function$
82+
;
83+
84+

utils/supabase/database.types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ export type Database = {
264264
highline_ids: string[];
265265
page_number: number;
266266
page_size: number;
267+
start_date?: string;
268+
end_date?: string;
267269
};
268270
Returns: {
269271
instagram: string;
@@ -276,6 +278,8 @@ export type Database = {
276278
highline_ids: string[];
277279
page_number: number;
278280
page_size: number;
281+
start_date?: string;
282+
end_date?: string;
279283
};
280284
Returns: {
281285
instagram: string;
@@ -288,6 +292,8 @@ export type Database = {
288292
highline_ids: string[];
289293
page_number: number;
290294
page_size: number;
295+
start_date?: string;
296+
end_date?: string;
291297
};
292298
Returns: {
293299
instagram: string;

0 commit comments

Comments
 (0)