Skip to content

Commit 9a186d8

Browse files
committed
feat(resources): update resource types and improve data fetching, add capitalize utility function
1 parent d37c29a commit 9a186d8

File tree

6 files changed

+44
-23
lines changed

6 files changed

+44
-23
lines changed

src/actions/resources.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,34 @@
11
"use server";
22

3+
import type { Resource } from "@/types";
4+
35
import { createClient } from "@/lib/supabase/server";
46

57
export const getResources = async () => {
68
const supabase = await createClient();
79
const { data, error } = await supabase.from("resources").select(`
8-
*,
10+
id,
11+
name,
12+
slug,
13+
image,
14+
tags,
915
category:categories (id, name)
1016
`);
1117

1218
if (error) throw error;
1319
return data;
1420
};
1521

16-
export const getResourceBySlug = async (slug: string) => {
22+
export const getResourceBySlug = async (
23+
slug: string,
24+
): Promise<Resource | null> => {
1725
const supabase = await createClient();
1826
const { data, error } = await supabase
1927
.from("resources")
2028
.select("*")
2129
.eq("slug", slug)
2230
.single();
23-
if (error) throw error;
31+
32+
if (error) throw new Error(error.message);
2433
return data;
2534
};

src/app/(public)/auth/callback/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { NextResponse } from "next/server";
22
import { createClient } from "@/lib/supabase/server";
3+
import { HOME_PATH } from "@/config/constants";
34

45
export async function GET(request: Request) {
56
const { searchParams, origin } = new URL(request.url);
67
const code = searchParams.get("code");
7-
const next = searchParams.get("next") ?? "/";
8+
const next = searchParams.get("next") ?? HOME_PATH;
89

910
if (code) {
1011
const supabase = await createClient();

src/app/not-found.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
import { useEffect } from "react";
44
import { useRouter } from "next/navigation";
55
import { motion } from "framer-motion";
6+
import { HOME_PATH } from "@/config/constants";
67

78
export default function NotFoundPage() {
89
const router = useRouter();
910

1011
useEffect(() => {
1112
const timer = setTimeout(() => {
12-
router.push("/");
13+
router.push(HOME_PATH);
1314
}, 5000);
1415

1516
return () => clearTimeout(timer);

src/components/resources/resource-list.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
"use client";
22

3-
import type { ResourceResponse } from "@/types";
3+
import type { Resource } from "@/types";
44

55
import { ResourceCard } from "@/components/resources";
66

77
type Props = {
8-
resources: ResourceResponse[];
8+
resources: Resource[];
99
};
1010

1111
export default function ResourceList({ resources }: Props) {

src/lib/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function capitalize(str: string) {
2+
return str.charAt(0).toUpperCase() + str.slice(1);
3+
}

src/types/index.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1-
// Api Responses
2-
export type ResourceResponse = {
3-
id: string;
4-
title: string;
5-
slug: string;
6-
description: string;
7-
image: string;
8-
category: string;
9-
tags: string[];
10-
author: {
11-
id: string;
12-
name: string;
13-
};
14-
created_at: string;
15-
};
1+
// Supabase Types
162

17-
export type UserResponse = {
3+
export type User = {
184
id: string;
195
email?: string;
206
user_metadata: {
@@ -23,3 +9,24 @@ export type UserResponse = {
239
avatar_url: string;
2410
};
2511
};
12+
13+
export type Category = {
14+
id: string;
15+
name: string;
16+
slug: string;
17+
created_at: string;
18+
};
19+
20+
export type Resource = {
21+
id: string;
22+
name: string;
23+
slug: string;
24+
url: string;
25+
description: string;
26+
image: string;
27+
tags: string[];
28+
author: string;
29+
category: Omit<Category, "id" | "created_at">;
30+
status: "pending" | "approved" | "rejected";
31+
created_at: string;
32+
};

0 commit comments

Comments
 (0)