-
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.
- Loading branch information
Showing
7 changed files
with
206 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"use server"; | ||
|
||
import { getSelf } from "@/lib/auth-service"; | ||
import { db } from "@/lib/db"; | ||
import { User } from "@prisma/client"; | ||
import { revalidatePath } from "next/cache"; | ||
|
||
export const updateUser = async (values: Partial<User>) => { | ||
const self = await getSelf(); | ||
|
||
const validData = { | ||
bio: values.bio, | ||
}; | ||
|
||
const user = await db.user.update({ | ||
where: { id: self.id }, | ||
data: { ...validData }, | ||
}); | ||
|
||
revalidatePath(`/${self.username}`); | ||
revalidatePath(`/u/${self.username}`); | ||
|
||
return user; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"use client"; | ||
|
||
import React from "react"; | ||
import { VerifiedMark } from "../verified-mark"; | ||
import { BioModal } from "./bio-modal"; | ||
|
||
interface AboutCardProps { | ||
hostName: string; | ||
hostIdentity: string; | ||
viewerIdentity: string; | ||
bio: string | null; | ||
followedByCount: number; | ||
} | ||
|
||
export const AboutCard = ({ | ||
hostName, | ||
hostIdentity, | ||
viewerIdentity, | ||
bio, | ||
followedByCount, | ||
}: AboutCardProps) => { | ||
const hostAsViewer = `host-${hostIdentity}`; | ||
const isHost = viewerIdentity === hostAsViewer; | ||
|
||
const followedByLabel = followedByCount === 1 ? "Follower" : "Followers"; | ||
|
||
return ( | ||
<div className="px-4"> | ||
<div className="group rounded-xl bg-background p-6 lg:p-10 flex flex-col gap-y-3"> | ||
<div className="flex items-center justify-between"> | ||
<div className="flex items-center gap-x-2 font-semibold text-lg lg:text-2xl"> | ||
About {hostName} | ||
<VerifiedMark /> | ||
</div> | ||
{isHost && <BioModal initialValue={bio} />} | ||
</div> | ||
<div className="text-sm text-muted-foreground"> | ||
<span className="text-semibold text-primary">{followedByCount}</span>{" "} | ||
{followedByLabel} | ||
</div> | ||
<p className="text-sm"> | ||
{bio || "This user prefers to keep an air of mystery about them."} | ||
</p> | ||
</div> | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use client"; | ||
|
||
import { updateUser } from "@/actions/user"; | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
Dialog, | ||
DialogClose, | ||
DialogContent, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from "@/components/ui/dialog"; | ||
import React, { ElementRef, useRef, useState, useTransition } from "react"; | ||
import { toast } from "sonner"; | ||
import { Textarea } from "../ui/textarea"; | ||
|
||
interface BioModalProps { | ||
initialValue: string; | ||
} | ||
|
||
export const BioModal = ({ initialValue }: BioModalProps) => { | ||
const closeRef = useRef<ElementRef<"button">>(null); | ||
|
||
const [isPending, startTransition] = useTransition(); | ||
const [value, setValue] = useState(initialValue || ""); | ||
|
||
const onSubmit = (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
startTransition(() => { | ||
updateUser({ bio: value }) | ||
.then(() => { | ||
toast.success("Your Bio has successfully updated!"); | ||
closeRef.current?.click(); | ||
}) | ||
.catch(() => toast.error("Something went wrong")); | ||
}); | ||
}; | ||
|
||
return ( | ||
<Dialog> | ||
<DialogTrigger asChild> | ||
<Button variant="link" size="sm" className="ml-auto"> | ||
Edit | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Edit user bio</DialogTitle> | ||
</DialogHeader> | ||
<form onSubmit={onSubmit} className="space-y-4"> | ||
<Textarea | ||
placeholder="Tell your viewers about yourself..." | ||
onChange={(e) => setValue(e.target.value)} | ||
value={value} | ||
disabled={isPending} | ||
className="resize-none" | ||
/> | ||
<div className="flex justify-between"> | ||
<DialogClose ref={closeRef} asChild> | ||
<Button type="button" variant="ghost"> | ||
Cancel | ||
</Button> | ||
</DialogClose> | ||
<Button disabled={isPending} type="submit" variant="primary"> | ||
Save | ||
</Button> | ||
</div> | ||
</form> | ||
</DialogContent> | ||
</Dialog> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import * as React from "react" | ||
|
||
import { cn } from "@/lib/utils" | ||
|
||
export interface TextareaProps | ||
extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {} | ||
|
||
const Textarea = React.forwardRef<HTMLTextAreaElement, TextareaProps>( | ||
({ className, ...props }, ref) => { | ||
return ( | ||
<textarea | ||
className={cn( | ||
"flex min-h-[80px] w-full rounded-md border border-input bg-background px-3 py-2 text-sm ring-offset-background placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-50", | ||
className | ||
)} | ||
ref={ref} | ||
{...props} | ||
/> | ||
) | ||
} | ||
) | ||
Textarea.displayName = "Textarea" | ||
|
||
export { Textarea } |
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