Skip to content

Commit 247815a

Browse files
committed
made some changes
1 parent e62eed5 commit 247815a

File tree

7 files changed

+70
-78
lines changed

7 files changed

+70
-78
lines changed

src/app/shard/[shard-id]/page.jsx

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,34 @@
11
import { redirect } from "next/navigation";
22
import SandpackEditor from "@/src/components/editor/SandpackEditor";
33
import { auth, currentUser } from "@clerk/nextjs/server";
4-
import { fetchShardById, handleFailureCase } from "@/src/lib/actions";
4+
import { fetchShardById, handleFailureCase, logFailureCb } from "@/src/lib/actions";
55

66
export default async function NewShardPage({ params }) {
77
const { userId } = await auth();
88
const user = await currentUser();
99
const shardId = params["shard-id"];
1010
console.log("Shard id: ", shardId);
1111

12-
if (!userId) {
13-
console.log("user not present");
12+
if (!userId || !shardId) {
13+
console.log("user or shard not present");
1414
redirect("/");
1515
}
1616

1717
const out = await fetchShardById(userId, shardId);
18-
handleFailureCase()
19-
// let shardDetails = await db.query.shards.findFirst({
20-
// where: (shards) =>
21-
// and(
22-
// eq(shards.id, shardId),
23-
// eq(shards.type, "public"),
24-
// eq(shards.mode, "normal"),
25-
// ),
26-
// with: {
27-
// files: true,
28-
// dependencies: true,
29-
// },
30-
// });
31-
if (!shardDetails) {
32-
console.log("shard id not valid");
33-
redirect("/");
34-
}
18+
handleFailureCase(out, ["shard"], {src: "fetchShardById()", redirectUri: "/"}, logFailureCb);
19+
let shardDetails = out.data.shard;
3520
console.log("Shard details: ", shardDetails);
36-
3721
const { templateType, userId: creator, id } = shardDetails;
3822

3923
if (userId !== creator) {
4024
console.log("shard is private or collaborative");
4125
redirect("/");
4226
}
4327

44-
return (
45-
<>
46-
<SandpackEditor
28+
return <SandpackEditor
4729
shardDetails={JSON.stringify(shardDetails)}
4830
template={templateType}
4931
shard={true}
5032
id={id}
5133
/>
52-
</>
53-
);
5434
}

src/app/shard/template/[template]/page.jsx

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { redirect } from "next/navigation";
22
import { templates } from "@/src/utils";
33
import SandpackEditor from "@/src/components/editor/SandpackEditor";
44
import { auth, currentUser } from "@clerk/nextjs/server";
5+
import { create, createShard, handleFailureCase, logFailureCb } from "@/src/lib/actions";
56

67
const page = async ({ params }) => {
78
const template = params.template;
@@ -20,30 +21,16 @@ const page = async ({ params }) => {
2021
}
2122

2223
console.log("Session user: ", user.username);
23-
let shardDetails = null;
24-
25-
try {
26-
// let ans = await db
27-
// .insert(shards)
28-
// .values({
29-
// userId: userId,
30-
// templateType: template,
31-
// createdAt: new Date(),
32-
// updatedAt: new Date(),
33-
// })
34-
// .returning();
35-
console.log("ans: ", ans);
36-
if (ans.length == 0) {
37-
redirect("/");
38-
}
39-
shardDetails = ans[0];
40-
} catch (error) {
41-
console.log(error);
42-
}
24+
const out = await createShard(userId, {
25+
templateType: template,
26+
mode: "normal",
27+
type: "public"
28+
})
4329

30+
handleFailureCase(out, ["shard"], {src: "createShard()", redirectUri: "/"}, logFailureCb);
31+
let shardDetails = out.data.shard;
4432
shardDetails["files"] = [];
4533
shardDetails["dependencies"] = [];
46-
console.log("shard details: ", shardDetails);
4734

4835
return (
4936
<div>

src/app/try-editor/[template]/page.jsx

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
import SandpackEditor from "@/src/components/editor/SandpackEditor";
22
import { templates } from "@/src/utils";
3+
34
import { redirect } from "next/navigation";
45
export const generateMetadata = ({ params }) => {
56
let template = params.template;
67
if (!templates.includes(template)) {
7-
// TODO: Give Error Info. to user using modal or alert.
88
console.log("Template not valid: ", template);
99
console.log("Supported Options: ", templates.join(", "));
1010
redirect("/");
1111
}
12-
13-
// My implementation:
14-
// template = template.toLowerCase();
15-
// template = template.split("");
16-
// template[0] = template[0].toUpperCase();
17-
// template = template.join("");
18-
19-
// ChatGPT:
2012
template = template.charAt(0).toUpperCase() + template.slice(1).toLowerCase();
2113
return {
2214
title: `${template} Template`,

src/app/your-work/page.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import Work from "@/src/components/dashboard/Work";
22

3-
// server component
43
export default function YourWorkPage() {
54
return (
65
<>

src/components/dashboard/Work.jsx

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@ import { redirect } from "next/navigation";
33
import { makeFilesAndDependenciesUIStateLike } from "@/src/utils";
44
import { CommentContextProvider } from "@/src/context/CommentContext";
55
import { auth, currentUser } from "@clerk/nextjs/server";
6+
import { fetchShards, handleFailureCase, throwFailureCb } from "@/src/lib/actions";
67

7-
const fetchShards = async (userId) => {
8+
const fetchShardsByUserId = async (userId) => {
89
try {
9-
// const shards = await db.query.shards.findMany({
10-
// where: (shards) => eq(shards.userId, userId),
11-
// with: {
12-
// files: true,
13-
// dependencies: true,
14-
// likes: true,
15-
// },
16-
// });
1710

18-
console.log("shards: ", shards);
19-
return shards;
11+
const out = await fetchShards(userId);
12+
handleFailureCase(out, ["shards"], {src: "fetchShards()"}, throwFailureCb);
13+
return out.data.shards;
2014
} catch (error) {
2115
console.log("error in fetching shards: ", error);
2216
return null;
@@ -31,9 +25,8 @@ async function Work() {
3125
redirect("/signin");
3226
}
3327

34-
const shards = await fetchShards(userId);
28+
const shards = await fetchShardsByUserId(userId);
3529
if (!shards) {
36-
console.log("hello");
3730
redirect("/");
3831
}
3932
console.log("Shards: ", shards);

src/components/room/RoomListPage.jsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ import { Suspense } from "react";
33
import { redirect } from "next/navigation";
44
import { auth } from "@clerk/nextjs/server";
55
import NextTopLoader from "nextjs-toploader";
6-
import { fetchAllRooms } from "@/src/lib/actions";
6+
import { fetchAllRooms, handleFailureCase, throwFailureCb } from "@/src/lib/actions";
77

88
const fetchRooms = async (userId) => {
99
try {
1010
const out = await fetchAllRooms(userId, 10, 0);
11-
if(!out || typeof out !== "object" || out.error || !out.data || !("rooms" in out.data)) {
12-
let errorMsg = "result data not valid";
13-
if(out.error) errorMsg = out.error.message;
14-
throw new Error(errorMsg)
15-
}
11+
handleFailureCase(out, ["rooms"], {
12+
src: "fetchRooms()"
13+
}, throwFailureCb);
1614

1715
return out.data.rooms;
1816

src/lib/actions.js

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
"use server";
12
import { HttpMethod } from "../utils/enums";
3+
import { redirect } from "next/navigation";
24

35
const apiOrigin = process.env.NEXT_PUBLIC_BACKEND_URL;
46
const apiVersion = "v1";
@@ -28,7 +30,8 @@ let backendEndpoint = `${apiOrigin}/api/${apiVersion}`;
2830

2931
export function logFailureCb(out, metadata) {
3032
if (out.error) console.log("error message: ", out.error.message)
31-
console.log("unexpected error happened while invoking" + `${metadata.src}: ` + out)
33+
console.log("unexpected error happened while invoking" + `${metadata.src}: ` + out);
34+
redirect(metadata.redirectUri)
3235
}
3336

3437
export function handleFailureCase(out, successDataFields, metadata, failureCb) {
@@ -76,6 +79,44 @@ export async function saveUserMetadata(userId) {
7679
}
7780

7881
/************************************SHARD ROUTES *******************************/
82+
83+
export async function createShard(userId, content) {
84+
try {
85+
const res = await fetch(`${backendEndpoint}/shards`,{
86+
method: HttpMethod.Post,
87+
body: jsonify({
88+
templateType: content.templateType,
89+
mode: content.mode,
90+
type: content.type
91+
}),
92+
headers: protectedRouteHeaders(userId, true)
93+
});
94+
return res.json();
95+
96+
} catch (error) {
97+
console.log("error occured in getComments", error)
98+
return null;
99+
}
100+
}
101+
102+
export async function fetchShards(userId) {
103+
//protected route
104+
let url = new URL(`${backendEndpoint}/shards`);
105+
url.searchParams.append("limit", limit);
106+
url.searchParams.append("offset", offset);
107+
try {
108+
const res = await fetch(url.toString(), {
109+
method: HttpMethod.Get,
110+
headers: protectedRouteHeaders(userId)
111+
});
112+
return res.json();
113+
114+
} catch (error) {
115+
console.log("error occured in fetchShards", error)
116+
return null;
117+
}
118+
}
119+
79120
export async function saveShard(userId, shardId, content) {
80121
// protected route
81122
let url = `${backendEndpoint}/shards/${shardId}`;
@@ -219,6 +260,7 @@ export async function dislikeShard(userId,shardId) {
219260

220261

221262
/**************************COMMENT ROUTE************************/
263+
222264
export async function deleteComment(userId, commentId, content) {
223265
try {
224266
const res = await fetch(`${backendEndpoint}/comments/${commentId}`, {
@@ -286,4 +328,5 @@ export async function createNewRoom(userId, content) {
286328
console.log("error occured in getComments", error)
287329
return null;
288330
}
289-
}
331+
}
332+

0 commit comments

Comments
 (0)