Skip to content

Commit

Permalink
chore: disable comments unless user is signed in (denoland#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbhrznr authored Sep 5, 2023
1 parent b290ffa commit b7af728
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions routes/items/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import Head from "@/components/Head.tsx";
import type { State } from "@/middleware/session.ts";
import CommentsList from "@/islands/CommentsList.tsx";

function CommentInput(props: { itemId: string }) {
function CommentInput(props: { isSignedIn: boolean; itemId: string }) {
return (
<form method="post" action="/api/comments">
<input type="hidden" name="item_id" value={props.itemId} />
<textarea
class={`${INPUT_STYLES} w-full`}
disabled={!props.isSignedIn}
type="text"
name="text"
required
/>
<button type="submit" class={BUTTON_STYLES}>Comment</button>
<button class={BUTTON_STYLES} disabled={!props.isSignedIn} type="submit">
{props.isSignedIn ? "Comment" : "Sign in to comment"}
</button>
</form>
);
}
Expand All @@ -30,11 +33,13 @@ export default async function ItemsItemPage(
const item = await getItem(itemId);
if (item === null) return await ctx.renderNotFound();

const isSignedIn = ctx.state.sessionUser !== undefined;
let isVoted = false;
if (ctx.state.sessionUser !== undefined) {

if (isSignedIn) {
const areVoted = await getAreVotedByUser(
[item],
ctx.state.sessionUser.login,
ctx.state.sessionUser!.login,
);
isVoted = areVoted[0];
}
Expand All @@ -47,7 +52,7 @@ export default async function ItemsItemPage(
item={item}
isVoted={isVoted}
/>
<CommentInput itemId={ctx.params.id} />
<CommentInput isSignedIn={isSignedIn} itemId={ctx.params.id} />
<CommentsList itemId={ctx.params.id} />
</main>
</>
Expand Down
2 changes: 1 addition & 1 deletion utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const SITE_DESCRIPTION = "Discover new Deno projects. Share your own.";
* 2. Writing custom components which offer no additional functionality beyond styling
*/
export const BUTTON_STYLES =
"px-4 py-2 bg-primary text-white rounded-lg border-1 border-primary transition duration-300 disabled:(opacity-50 cursor-not-allowed) hover:(bg-transparent text-primary)";
"px-4 py-2 bg-primary text-white rounded-lg border-1 border-primary transition duration-300 disabled:(opacity-50 cursor-not-allowed) hover:enabled:(bg-transparent text-primary)";
export const INPUT_STYLES =
"px-4 py-2 bg-transparent rounded rounded-lg outline-none border-1 border-gray-300 hover:border-black transition duration-300 disabled:(opacity-50 cursor-not-allowed) dark:(hover:border-white)";
export const SITE_BAR_STYLES = "flex justify-between p-4 gap-4";
Expand Down

0 comments on commit b7af728

Please sign in to comment.