From a084ecd6c7fca38bc354002b37c65cdea23caf65 Mon Sep 17 00:00:00 2001 From: Ishaan Dey Date: Wed, 1 May 2024 08:20:08 -0400 Subject: [PATCH] improve share logic --- backend/database/drizzle/meta/_journal.json | 7 +++++++ backend/database/src/index.ts | 9 +++++++-- backend/database/src/schema.ts | 3 ++- frontend/app/(app)/layout.tsx | 1 - frontend/app/globals.css | 6 +++--- frontend/components/dashboard/shared.tsx | 6 ++++-- 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/backend/database/drizzle/meta/_journal.json b/backend/database/drizzle/meta/_journal.json index f56fe30..38d79b6 100644 --- a/backend/database/drizzle/meta/_journal.json +++ b/backend/database/drizzle/meta/_journal.json @@ -29,6 +29,13 @@ "when": 1714541233589, "tag": "0003_pale_overlord", "breakpoints": true + }, + { + "idx": 4, + "version": "5", + "when": 1714565073180, + "tag": "0004_cuddly_wolf_cub", + "breakpoints": true } ] } \ No newline at end of file diff --git a/backend/database/src/index.ts b/backend/database/src/index.ts index 9bfb009..993ce41 100644 --- a/backend/database/src/index.ts +++ b/backend/database/src/index.ts @@ -110,7 +110,7 @@ export default { }, }); if (!sb) return; - return { id: sb.id, name: sb.name, type: sb.type, author: sb.author.name, sharedOn: Date.now() }; + return { id: sb.id, name: sb.name, type: sb.type, author: sb.author.name, sharedOn: r.sharedOn }; }) ); @@ -128,6 +128,7 @@ export default { const user = await db.query.user.findFirst({ where: (user, { eq }) => eq(user.email, email), with: { + sandbox: true, usersToSandboxes: true, }, }); @@ -136,11 +137,15 @@ export default { return new Response("No user associated with email.", { status: 400 }); } + if (user.sandbox.find((sb) => sb.id === sandboxId)) { + return new Response("Cannot share with yourself!", { status: 400 }); + } + if (user.usersToSandboxes.find((uts) => uts.sandboxId === sandboxId)) { return new Response("User already has access.", { status: 400 }); } - await db.insert(usersToSandboxes).values({ userId: user.id, sandboxId }).get(); + await db.insert(usersToSandboxes).values({ userId: user.id, sandboxId, sharedOn: new Date() }).get(); return success; } else if (method === "DELETE") { diff --git a/backend/database/src/schema.ts b/backend/database/src/schema.ts index 0b10294..279a3b9 100644 --- a/backend/database/src/schema.ts +++ b/backend/database/src/schema.ts @@ -1,6 +1,6 @@ import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; import { createId } from "@paralleldrive/cuid2"; -import { relations } from "drizzle-orm"; +import { relations, sql } from "drizzle-orm"; export const user = sqliteTable("user", { id: text("id") @@ -49,6 +49,7 @@ export const usersToSandboxes = sqliteTable("users_to_sandboxes", { sandboxId: text("sandboxId") .notNull() .references(() => sandbox.id), + sharedOn: integer("sharedOn", { mode: "timestamp_ms" }), }); export const usersToSandboxesRelations = relations(usersToSandboxes, ({ one }) => ({ diff --git a/frontend/app/(app)/layout.tsx b/frontend/app/(app)/layout.tsx index 8b33c07..a191f40 100644 --- a/frontend/app/(app)/layout.tsx +++ b/frontend/app/(app)/layout.tsx @@ -31,7 +31,6 @@ export default async function AppAuthLayout({ console.log(res) } else { // user already exists in db - console.log("user exists already.", dbUserJSON) } return <>{children} diff --git a/frontend/app/globals.css b/frontend/app/globals.css index 8a2dcec..e48e9aa 100644 --- a/frontend/app/globals.css +++ b/frontend/app/globals.css @@ -76,15 +76,15 @@ } .gradient-button-bg { - background: radial-gradient(circle at top, #a5b4fc -10%, #3730a3 30%); /* violet 300 -> 800 */ + background: radial-gradient(circle at top, #a5b4fc 0%, #3730a3 50%); /* violet 300 -> 800 */ } .gradient-button { - background: radial-gradient(circle at bottom, #312e81 -20%, hsl(0 0% 3.9%) 50%); /* violet 900 -> bg */ + background: radial-gradient(circle at bottom, #312e81 -10%, hsl(0 0% 3.9%) 50%); /* violet 900 -> bg */ } .gradient-button-bg > div:hover { - background: radial-gradient(circle at bottom, #312e81 -20%, hsl(0 0% 3.9%) 100%); /* violet 900 -> bg */ + background: radial-gradient(circle at bottom, #312e81 -10%, hsl(0 0% 3.9%) 80%); /* violet 900 -> bg */ } .gradient-project-card-bg { diff --git a/frontend/components/dashboard/shared.tsx b/frontend/components/dashboard/shared.tsx index 84d76c6..1700f51 100644 --- a/frontend/components/dashboard/shared.tsx +++ b/frontend/components/dashboard/shared.tsx @@ -33,7 +33,7 @@ export default function DashboardSharedWithMe({ Sandbox Name Shared By - Opened + Sent On @@ -62,7 +62,9 @@ export default function DashboardSharedWithMe({ {sandbox.author} - {sandbox.sharedOn.toLocaleDateString()} + + {new Date(sandbox.sharedOn).toLocaleDateString()} +