Spaces:
Running
Running
switch to action
Browse files
app/(public)/projects/page.tsx
CHANGED
|
@@ -1,31 +1,11 @@
|
|
| 1 |
-
import { cookies } from "next/headers";
|
| 2 |
import { redirect } from "next/navigation";
|
| 3 |
|
| 4 |
-
import { apiServer } from "@/lib/api";
|
| 5 |
-
import MY_TOKEN_KEY from "@/lib/get-cookie-name";
|
| 6 |
import { MyProjects } from "@/components/my-projects";
|
|
|
|
| 7 |
|
| 8 |
-
async function getMyProjects() {
|
| 9 |
-
const cookieStore = await cookies();
|
| 10 |
-
const token = cookieStore.get(MY_TOKEN_KEY())?.value;
|
| 11 |
-
if (!token) return { redirectUrl: true, projects: [] };
|
| 12 |
-
try {
|
| 13 |
-
const { data } = await apiServer.get("/me/projects", {
|
| 14 |
-
headers: {
|
| 15 |
-
Authorization: `Bearer ${token}`,
|
| 16 |
-
},
|
| 17 |
-
});
|
| 18 |
-
|
| 19 |
-
return {
|
| 20 |
-
projects: data.projects,
|
| 21 |
-
};
|
| 22 |
-
} catch {
|
| 23 |
-
return { projects: [] };
|
| 24 |
-
}
|
| 25 |
-
}
|
| 26 |
export default async function ProjectsPage() {
|
| 27 |
-
const {
|
| 28 |
-
if (
|
| 29 |
redirect("/");
|
| 30 |
}
|
| 31 |
|
|
|
|
|
|
|
| 1 |
import { redirect } from "next/navigation";
|
| 2 |
|
|
|
|
|
|
|
| 3 |
import { MyProjects } from "@/components/my-projects";
|
| 4 |
+
import { getProjects } from "@/app/actions/projects";
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
export default async function ProjectsPage() {
|
| 7 |
+
const { ok, projects } = await getProjects();
|
| 8 |
+
if (!ok) {
|
| 9 |
redirect("/");
|
| 10 |
}
|
| 11 |
|
app/actions/projects.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use server";
|
| 2 |
+
|
| 3 |
+
import { isAuthenticated } from "@/lib/auth";
|
| 4 |
+
import { NextResponse } from "next/server";
|
| 5 |
+
import dbConnect from "@/lib/mongodb";
|
| 6 |
+
import Project from "@/models/Project";
|
| 7 |
+
import { Project as ProjectType } from "@/types";
|
| 8 |
+
|
| 9 |
+
export async function getProjects(): Promise<{
|
| 10 |
+
ok: boolean;
|
| 11 |
+
projects: ProjectType[];
|
| 12 |
+
}> {
|
| 13 |
+
const user = await isAuthenticated();
|
| 14 |
+
|
| 15 |
+
if (user instanceof NextResponse || !user) {
|
| 16 |
+
return {
|
| 17 |
+
ok: false,
|
| 18 |
+
projects: [],
|
| 19 |
+
};
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
await dbConnect();
|
| 23 |
+
const projects = await Project.find({
|
| 24 |
+
user_id: user?.id,
|
| 25 |
+
})
|
| 26 |
+
.sort({ _createdAt: -1 })
|
| 27 |
+
.limit(100)
|
| 28 |
+
.lean();
|
| 29 |
+
if (!projects) {
|
| 30 |
+
return {
|
| 31 |
+
ok: false,
|
| 32 |
+
projects: [],
|
| 33 |
+
};
|
| 34 |
+
}
|
| 35 |
+
return {
|
| 36 |
+
ok: true,
|
| 37 |
+
projects: JSON.parse(JSON.stringify(projects)) as ProjectType[],
|
| 38 |
+
};
|
| 39 |
+
}
|
app/api/me/projects/[namespace]/[repoId]/route.ts
CHANGED
|
@@ -205,6 +205,7 @@ export async function POST(
|
|
| 205 |
space_id: `${namespace}/${repoId}`,
|
| 206 |
}).lean();
|
| 207 |
if (project) {
|
|
|
|
| 208 |
return NextResponse.json(
|
| 209 |
{
|
| 210 |
ok: false,
|
|
|
|
| 205 |
space_id: `${namespace}/${repoId}`,
|
| 206 |
}).lean();
|
| 207 |
if (project) {
|
| 208 |
+
// redirect to the project page if it already exists
|
| 209 |
return NextResponse.json(
|
| 210 |
{
|
| 211 |
ok: false,
|
components/editor/ask-ai/index.tsx
CHANGED
|
@@ -173,7 +173,6 @@ export function AskAI({
|
|
| 173 |
return;
|
| 174 |
}
|
| 175 |
|
| 176 |
-
console.log("AI response complete", contentResponse);
|
| 177 |
toast.success("AI responded successfully");
|
| 178 |
setPreviousPrompt(prompt);
|
| 179 |
setPrompt("");
|
|
|
|
| 173 |
return;
|
| 174 |
}
|
| 175 |
|
|
|
|
| 176 |
toast.success("AI responded successfully");
|
| 177 |
setPreviousPrompt(prompt);
|
| 178 |
setPrompt("");
|
components/my-projects/load-project.tsx
CHANGED
|
@@ -59,7 +59,6 @@ export const LoadProject = ({
|
|
| 59 |
setIsLoading(true);
|
| 60 |
try {
|
| 61 |
const response = await api.post(`/me/projects/${username}/${namespace}`);
|
| 62 |
-
console.log("response", response);
|
| 63 |
toast.success("Project imported successfully!");
|
| 64 |
setOpen(false);
|
| 65 |
setUrl("");
|
|
|
|
| 59 |
setIsLoading(true);
|
| 60 |
try {
|
| 61 |
const response = await api.post(`/me/projects/${username}/${namespace}`);
|
|
|
|
| 62 |
toast.success("Project imported successfully!");
|
| 63 |
setOpen(false);
|
| 64 |
setUrl("");
|
lib/auth.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import { User } from "@/types";
|
| 2 |
import { NextResponse } from "next/server";
|
| 3 |
-
import { headers } from "next/headers";
|
|
|
|
| 4 |
|
| 5 |
// UserResponse = type User & { token: string };
|
| 6 |
type UserResponse = User & { token: string };
|
|
@@ -8,7 +9,11 @@ type UserResponse = User & { token: string };
|
|
| 8 |
export const isAuthenticated = async (): // req: NextRequest
|
| 9 |
Promise<UserResponse | NextResponse<unknown> | undefined> => {
|
| 10 |
const authHeaders = await headers();
|
| 11 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
if (!token) {
|
| 13 |
return NextResponse.json(
|
| 14 |
{
|
|
|
|
| 1 |
import { User } from "@/types";
|
| 2 |
import { NextResponse } from "next/server";
|
| 3 |
+
import { cookies, headers } from "next/headers";
|
| 4 |
+
import MY_TOKEN_KEY from "./get-cookie-name";
|
| 5 |
|
| 6 |
// UserResponse = type User & { token: string };
|
| 7 |
type UserResponse = User & { token: string };
|
|
|
|
| 9 |
export const isAuthenticated = async (): // req: NextRequest
|
| 10 |
Promise<UserResponse | NextResponse<unknown> | undefined> => {
|
| 11 |
const authHeaders = await headers();
|
| 12 |
+
const cookieStore = await cookies();
|
| 13 |
+
const token = cookieStore.get(MY_TOKEN_KEY())?.value
|
| 14 |
+
? `Bearer ${cookieStore.get(MY_TOKEN_KEY())?.value}`
|
| 15 |
+
: authHeaders.get("Authorization");
|
| 16 |
+
|
| 17 |
if (!token) {
|
| 18 |
return NextResponse.json(
|
| 19 |
{
|