Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dataroom settings changes #439

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions components/ui/form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { InputHTMLAttributes, ReactNode, useMemo, useState } from "react";

import { cn } from "@/lib/utils";

import { Button } from "./button";
import {
Card,
CardContent,
CardDescription,
CardFooter,
CardHeader,
CardTitle,
} from "./card";
import { Input } from "./input";

export function Form({
title,
description,
inputAttrs,
helpText,
buttonText = "Save Changes",
disabledTooltip,
handleSubmit,
}: {
title: string;
description: string;
inputAttrs: InputHTMLAttributes<HTMLInputElement>;
helpText?: string | ReactNode;
buttonText?: string;
disabledTooltip?: string | ReactNode;
handleSubmit: (data: any) => Promise<any>;
}) {
const [value, setValue] = useState(inputAttrs.defaultValue);
const [saving, setSaving] = useState(false);
const saveDisabled = useMemo(() => {
return saving || !value || value === inputAttrs.defaultValue;
}, [saving, value, inputAttrs.defaultValue]);

return (
<form
onSubmit={async (e) => {
e.preventDefault();
setSaving(true);
await handleSubmit({
[inputAttrs.name as string]: value,
});
setSaving(false);
}}
className="rounded-lg bg-background"
>
<Card>
<CardHeader>
<CardTitle>{title}</CardTitle>
<CardDescription>{description}</CardDescription>
</CardHeader>
<CardContent>
{typeof inputAttrs.defaultValue === "string" ? (
<Input
{...inputAttrs}
type={inputAttrs.type || "text"}
required
disabled={disabledTooltip ? true : false}
onChange={(e) => setValue(e.target.value)}
className={cn(
"w-full max-w-md focus:border-gray-500 focus:outline-none focus:ring-gray-500 ",
{
"cursor-not-allowed bg-gray-100 text-gray-400":
disabledTooltip,
},
)}
data-1p-ignore
/>
) : (
<div className="h-[2.35rem] w-full max-w-md animate-pulse rounded-md bg-gray-200" />
)}
</CardContent>
<CardFooter className="flex items-center justify-between rounded-b-lg border-t bg-muted px-6 py-3">
<p className="text-sm text-muted-foreground transition-colors">
{helpText || ""}
</p>

<div className="shrink-0">
<Button loading={saving} disabled={saveDisabled}>
{buttonText}
</Button>
</div>
</CardFooter>
</Card>
</form>
);
}
47 changes: 46 additions & 1 deletion pages/api/teams/[teamId]/datarooms/[id]/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,61 @@ export default async function handle(
const dataroom = await prisma.dataroom.findUnique({
where: {
id: dataroomId,
teamId,
},
});

return res.status(200).json(dataroom);
} catch (error) {
errorhandler(error, res);
}
} else if (req.method === "PATCH") {
// PATCH /api/teams/:teamId/datarooms/:id
const session = await getServerSession(req, res, authOptions);
if (!session) {
return res.status(401).end("Unauthorized");
}

const { teamId, id: dataroomId } = req.query as {
teamId: string;
id: string;
};
const { name } = req.body as { name: string };

const userId = (session.user as CustomUser).id;

try {
// Check if the user is part of the team
const team = await prisma.team.findUnique({
where: {
id: teamId,
users: {
some: {
userId: userId,
},
},
},
});

if (!team) {
return res.status(401).end("Unauthorized");
}

const dataroom = await prisma.dataroom.update({
where: {
id: dataroomId,
teamId,
},
data: { name },
});

return res.status(200).json(dataroom);
} catch (error) {
errorhandler(error, res);
}
} else {
// We only allow GET requests
res.setHeader("Allow", ["GET"]);
res.setHeader("Allow", ["GET", "PATCH"]);
return res.status(405).end(`Method ${req.method} Not Allowed`);
}
}
Loading