improve share logic

This commit is contained in:
Ishaan Dey 2024-05-01 08:20:08 -04:00
parent 1066638e92
commit a084ecd6c7
6 changed files with 23 additions and 9 deletions

View File

@ -29,6 +29,13 @@
"when": 1714541233589, "when": 1714541233589,
"tag": "0003_pale_overlord", "tag": "0003_pale_overlord",
"breakpoints": true "breakpoints": true
},
{
"idx": 4,
"version": "5",
"when": 1714565073180,
"tag": "0004_cuddly_wolf_cub",
"breakpoints": true
} }
] ]
} }

View File

@ -110,7 +110,7 @@ export default {
}, },
}); });
if (!sb) return; 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({ const user = await db.query.user.findFirst({
where: (user, { eq }) => eq(user.email, email), where: (user, { eq }) => eq(user.email, email),
with: { with: {
sandbox: true,
usersToSandboxes: true, usersToSandboxes: true,
}, },
}); });
@ -136,11 +137,15 @@ export default {
return new Response("No user associated with email.", { status: 400 }); 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)) { if (user.usersToSandboxes.find((uts) => uts.sandboxId === sandboxId)) {
return new Response("User already has access.", { status: 400 }); 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; return success;
} else if (method === "DELETE") { } else if (method === "DELETE") {

View File

@ -1,6 +1,6 @@
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"; import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2"; import { createId } from "@paralleldrive/cuid2";
import { relations } from "drizzle-orm"; import { relations, sql } from "drizzle-orm";
export const user = sqliteTable("user", { export const user = sqliteTable("user", {
id: text("id") id: text("id")
@ -49,6 +49,7 @@ export const usersToSandboxes = sqliteTable("users_to_sandboxes", {
sandboxId: text("sandboxId") sandboxId: text("sandboxId")
.notNull() .notNull()
.references(() => sandbox.id), .references(() => sandbox.id),
sharedOn: integer("sharedOn", { mode: "timestamp_ms" }),
}); });
export const usersToSandboxesRelations = relations(usersToSandboxes, ({ one }) => ({ export const usersToSandboxesRelations = relations(usersToSandboxes, ({ one }) => ({

View File

@ -31,7 +31,6 @@ export default async function AppAuthLayout({
console.log(res) console.log(res)
} else { } else {
// user already exists in db // user already exists in db
console.log("user exists already.", dbUserJSON)
} }
return <>{children}</> return <>{children}</>

View File

@ -76,15 +76,15 @@
} }
.gradient-button-bg { .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 { .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 { .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 { .gradient-project-card-bg {

View File

@ -33,7 +33,7 @@ export default function DashboardSharedWithMe({
<TableRow className="hover:bg-background"> <TableRow className="hover:bg-background">
<TableHead>Sandbox Name</TableHead> <TableHead>Sandbox Name</TableHead>
<TableHead>Shared By</TableHead> <TableHead>Shared By</TableHead>
<TableHead>Opened</TableHead> <TableHead>Sent On</TableHead>
<TableHead className="text-right"></TableHead> <TableHead className="text-right"></TableHead>
</TableRow> </TableRow>
</TableHeader> </TableHeader>
@ -62,7 +62,9 @@ export default function DashboardSharedWithMe({
{sandbox.author} {sandbox.author}
</div> </div>
</TableCell> </TableCell>
<TableCell>{sandbox.sharedOn.toLocaleDateString()}</TableCell> <TableCell>
{new Date(sandbox.sharedOn).toLocaleDateString()}
</TableCell>
<TableCell className="text-right"> <TableCell className="text-right">
<Button> <Button>
Open <ChevronRight className="w-4 h-4 ml-2" /> Open <ChevronRight className="w-4 h-4 ml-2" />