Spaces:
Running
Running
if existing project redirect to it
Browse files
app/actions/projects.ts
CHANGED
|
@@ -37,3 +37,27 @@ export async function getProjects(): Promise<{
|
|
| 37 |
projects: JSON.parse(JSON.stringify(projects)) as ProjectType[],
|
| 38 |
};
|
| 39 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
projects: JSON.parse(JSON.stringify(projects)) as ProjectType[],
|
| 38 |
};
|
| 39 |
}
|
| 40 |
+
|
| 41 |
+
export async function getProject(
|
| 42 |
+
namespace: string,
|
| 43 |
+
repoId: string
|
| 44 |
+
): Promise<ProjectType | null> {
|
| 45 |
+
const user = await isAuthenticated();
|
| 46 |
+
|
| 47 |
+
if (user instanceof NextResponse || !user) {
|
| 48 |
+
return null;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
await dbConnect();
|
| 52 |
+
const project = await Project.findOne({
|
| 53 |
+
user_id: user.id,
|
| 54 |
+
namespace,
|
| 55 |
+
repoId,
|
| 56 |
+
}).lean();
|
| 57 |
+
|
| 58 |
+
if (!project) {
|
| 59 |
+
return null;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
return JSON.parse(JSON.stringify(project)) as ProjectType;
|
| 63 |
+
}
|
app/api/me/projects/[namespace]/[repoId]/route.ts
CHANGED
|
@@ -210,6 +210,7 @@ export async function POST(
|
|
| 210 |
{
|
| 211 |
ok: false,
|
| 212 |
error: "Project already exists",
|
|
|
|
| 213 |
},
|
| 214 |
{ status: 400 }
|
| 215 |
);
|
|
|
|
| 210 |
{
|
| 211 |
ok: false,
|
| 212 |
error: "Project already exists",
|
| 213 |
+
redirect: `/projects/${namespace}/${repoId}`,
|
| 214 |
},
|
| 215 |
{ status: 400 }
|
| 216 |
);
|
components/my-projects/load-project.tsx
CHANGED
|
@@ -16,6 +16,7 @@ import { toast } from "sonner";
|
|
| 16 |
import { api } from "@/lib/api";
|
| 17 |
import { useUser } from "@/hooks/useUser";
|
| 18 |
import { LoginModal } from "../login-modal";
|
|
|
|
| 19 |
|
| 20 |
export const LoadProject = ({
|
| 21 |
fullXsBtn = false,
|
|
@@ -25,6 +26,7 @@ export const LoadProject = ({
|
|
| 25 |
onSuccess: (project: Project) => void;
|
| 26 |
}) => {
|
| 27 |
const { user } = useUser();
|
|
|
|
| 28 |
|
| 29 |
const [openLoginModal, setOpenLoginModal] = useState(false);
|
| 30 |
const [open, setOpen] = useState(false);
|
|
@@ -65,6 +67,9 @@ export const LoadProject = ({
|
|
| 65 |
onSuccess(response.data.project);
|
| 66 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
| 67 |
} catch (error: any) {
|
|
|
|
|
|
|
|
|
|
| 68 |
toast.error(
|
| 69 |
error?.response?.data?.error ?? "Failed to import the project."
|
| 70 |
);
|
|
|
|
| 16 |
import { api } from "@/lib/api";
|
| 17 |
import { useUser } from "@/hooks/useUser";
|
| 18 |
import { LoginModal } from "../login-modal";
|
| 19 |
+
import { useRouter } from "next/navigation";
|
| 20 |
|
| 21 |
export const LoadProject = ({
|
| 22 |
fullXsBtn = false,
|
|
|
|
| 26 |
onSuccess: (project: Project) => void;
|
| 27 |
}) => {
|
| 28 |
const { user } = useUser();
|
| 29 |
+
const router = useRouter();
|
| 30 |
|
| 31 |
const [openLoginModal, setOpenLoginModal] = useState(false);
|
| 32 |
const [open, setOpen] = useState(false);
|
|
|
|
| 67 |
onSuccess(response.data.project);
|
| 68 |
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
| 69 |
} catch (error: any) {
|
| 70 |
+
if (error?.response?.data?.redirect) {
|
| 71 |
+
return router.push(error.response.data.redirect);
|
| 72 |
+
}
|
| 73 |
toast.error(
|
| 74 |
error?.response?.data?.error ?? "Failed to import the project."
|
| 75 |
);
|