-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from conuko/develop
Feature: Authorization Page
- Loading branch information
Showing
4 changed files
with
79 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,77 @@ | ||
import { trpc } from "../utils/trpc"; | ||
import { useState } from "react"; | ||
import { useSession } from "next-auth/react"; | ||
import Link from "next/link"; | ||
|
||
const Form = () => { | ||
const { data: session } = useSession(); | ||
|
||
const { data: subscriptionStatus, isLoading } = | ||
trpc.user.subscriptionStatus.useQuery(); | ||
|
||
const [message, setMessage] = useState(""); | ||
const utils = trpc.useContext(); | ||
|
||
const postMessage = trpc.guestbook.postMessage.useMutation({ | ||
// refetch messages after a message is added | ||
async onSuccess() { | ||
await utils.guestbook.getAll.invalidate(); | ||
}, | ||
}); | ||
|
||
return ( | ||
<form | ||
className="flex gap-2" | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
postMessage.mutate({ | ||
userId: session?.user?.id as string, | ||
name: session?.user?.name as string, | ||
message, | ||
}); | ||
setMessage(""); | ||
}} | ||
> | ||
<input | ||
type="text" | ||
value={message} | ||
placeholder="Your message..." | ||
minLength={2} | ||
maxLength={100} | ||
onChange={(event) => setMessage(event.target.value)} | ||
className="rounded-md border-2 border-zinc-800 bg-neutral-900 px-4 py-2 opacity-50 focus:opacity-100 focus:outline-none" | ||
/> | ||
<button | ||
type="submit" | ||
className="rounded-md border-2 border-zinc-800 p-2 focus:outline-none" | ||
disabled={message.length < 2 || postMessage.isLoading} | ||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
if (!session) { | ||
return <div>Not signed in</div>; | ||
} | ||
|
||
if (subscriptionStatus === "active") { | ||
return ( | ||
<form | ||
className="flex gap-2" | ||
onSubmit={(event) => { | ||
event.preventDefault(); | ||
postMessage.mutate({ | ||
userId: session?.user?.id as string, | ||
name: session?.user?.name as string, | ||
message, | ||
}); | ||
setMessage(""); | ||
}} | ||
> | ||
Submit | ||
</button> | ||
</form> | ||
); | ||
<input | ||
type="text" | ||
value={message} | ||
placeholder="Your message..." | ||
minLength={2} | ||
maxLength={100} | ||
onChange={(event) => setMessage(event.target.value)} | ||
className="rounded-md border-2 border-zinc-800 bg-neutral-900 px-4 py-2 opacity-50 focus:opacity-100 focus:outline-none" | ||
/> | ||
<button | ||
type="submit" | ||
className="rounded-md border-2 border-zinc-800 p-2 focus:outline-none" | ||
disabled={message.length < 2 || postMessage.isLoading} | ||
> | ||
Submit | ||
</button> | ||
</form> | ||
); | ||
} else { | ||
return ( | ||
<div className="flex flex-col gap-2"> | ||
<p className="text-center"> | ||
You need an active{" "} | ||
<Link className="text-yellow-200 hover:text-yellow-400" href="/user"> | ||
subscription | ||
</Link>{" "} | ||
to post or edit messages. | ||
</p> | ||
</div> | ||
); | ||
} | ||
}; | ||
|
||
export default Form; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66fc904
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
guest-and-chat-book – ./
guest-and-chat-book-conuko.vercel.app
guest-and-chat-book-git-main-conuko.vercel.app
guest-and-chat-book.vercel.app