forked from lukevella/rallly
-
Notifications
You must be signed in to change notification settings - Fork 0
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
18 changed files
with
441 additions
and
38 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
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
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,20 @@ | ||
import * as React from "react"; | ||
|
||
export interface EmptyStateProps { | ||
icon: React.ComponentType<{ className?: string }>; | ||
text: React.ReactNode; | ||
} | ||
|
||
export const EmptyState: React.VoidFunctionComponent<EmptyStateProps> = ({ | ||
icon: Icon, | ||
text, | ||
}) => { | ||
return ( | ||
<div className="flex h-full items-center justify-center py-12"> | ||
<div className="text-center font-medium text-slate-500/50"> | ||
<Icon className="mb-2 inline-block h-12 w-12" /> | ||
<div>{text}</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,97 @@ | ||
import { formatRelative } from "date-fns"; | ||
import Link from "next/link"; | ||
import { useRouter } from "next/router"; | ||
import { useTranslation } from "next-i18next"; | ||
import * as React from "react"; | ||
|
||
import Calendar from "@/components/icons/calendar.svg"; | ||
import Pencil from "@/components/icons/pencil.svg"; | ||
import User from "@/components/icons/user.svg"; | ||
|
||
import { trpc } from "../utils/trpc"; | ||
import { EmptyState } from "./empty-state"; | ||
import { UserDetails } from "./profile/user-details"; | ||
import { useSession } from "./session"; | ||
|
||
export const Profile: React.VoidFunctionComponent = () => { | ||
const { user } = useSession(); | ||
|
||
const { t } = useTranslation("app"); | ||
const { data: userPolls } = trpc.useQuery(["user.getPolls"]); | ||
|
||
const router = useRouter(); | ||
const createdPolls = userPolls?.polls; | ||
|
||
React.useEffect(() => { | ||
if (!user) { | ||
router.replace("/new"); | ||
} | ||
}, [user, router]); | ||
|
||
if (!user || user.isGuest) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="mx-auto max-w-3xl py-4 lg:mx-0"> | ||
<div className="mb-4 flex items-center px-4"> | ||
<div className="mr-4 inline-flex h-14 w-14 items-center justify-center rounded-lg bg-indigo-50"> | ||
<User className="h-7 text-indigo-500" /> | ||
</div> | ||
<div> | ||
<div | ||
data-testid="user-name" | ||
className="mb-0 text-xl font-medium leading-tight" | ||
> | ||
{user.shortName} | ||
</div> | ||
<div className="text-slate-500"> | ||
{user.isGuest ? "Guest" : "User"} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<UserDetails userId={user.id} name={user.name} email={user.email} /> | ||
{createdPolls ? ( | ||
<div className="card p-0"> | ||
<div className="flex items-center justify-between border-b p-4 shadow-sm"> | ||
<div className="text-lg text-slate-700">{t("yourPolls")}</div> | ||
<Link href="/new"> | ||
<a className="btn-default"> | ||
<Pencil className="mr-1 h-5" /> | ||
{t("newPoll")} | ||
</a> | ||
</Link> | ||
</div> | ||
{createdPolls.length > 0 ? ( | ||
<div className="w-full sm:table sm:border-collapse"> | ||
<div className="divide-y sm:table-row-group"> | ||
{createdPolls.map((poll, i) => ( | ||
<div className="p-4 sm:table-row sm:p-0" key={i}> | ||
<div className="sm:table-cell sm:p-4"> | ||
<div> | ||
<div className="flex"> | ||
<Calendar className="mr-2 mt-[1px] h-5 text-indigo-500" /> | ||
<Link href={`/p/${poll.links[0].urlId}`}> | ||
<a className="text-slate-700 hover:text-indigo-500 hover:no-underline"> | ||
<div>{poll.title}</div> | ||
</a> | ||
</Link> | ||
</div> | ||
<div className="ml-7 text-sm text-slate-500"> | ||
{formatRelative(poll.createdAt, new Date())} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
))} | ||
</div> | ||
</div> | ||
) : ( | ||
<EmptyState icon={Pencil} text="No polls created" /> | ||
)} | ||
</div> | ||
) : null} | ||
</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,109 @@ | ||
import { motion } from "framer-motion"; | ||
import { useTranslation } from "next-i18next"; | ||
import * as React from "react"; | ||
import { useForm } from "react-hook-form"; | ||
|
||
import { requiredString, validEmail } from "../../utils/form-validation"; | ||
import { trpc } from "../../utils/trpc"; | ||
import { Button } from "../button"; | ||
import { useSession } from "../session"; | ||
import { TextInput } from "../text-input"; | ||
|
||
export interface UserDetailsProps { | ||
userId: string; | ||
name: string; | ||
email: string; | ||
} | ||
|
||
const MotionButton = motion(Button); | ||
|
||
export const UserDetails: React.VoidFunctionComponent<UserDetailsProps> = ({ | ||
userId, | ||
name, | ||
email, | ||
}) => { | ||
const { t } = useTranslation("app"); | ||
const { register, formState, handleSubmit, reset } = useForm<{ | ||
name: string; | ||
email: string; | ||
}>({ | ||
defaultValues: { name, email }, | ||
}); | ||
|
||
const { refresh } = useSession(); | ||
|
||
const changeName = trpc.useMutation("user.changeName", { | ||
onSuccess: () => { | ||
refresh(); | ||
}, | ||
}); | ||
|
||
const { dirtyFields } = formState; | ||
return ( | ||
<form | ||
onSubmit={handleSubmit(async (data) => { | ||
if (dirtyFields.name) { | ||
await changeName.mutateAsync({ userId, name: data.name }); | ||
} | ||
reset(data); | ||
})} | ||
className="card mb-4 p-0" | ||
> | ||
<div className="flex items-center justify-between border-b p-4 shadow-sm"> | ||
<div className="text-lg text-slate-700 ">{t("yourDetails")}</div> | ||
<MotionButton | ||
variants={{ | ||
hidden: { opacity: 0, x: 10 }, | ||
visible: { opacity: 1, x: 0 }, | ||
}} | ||
transition={{ duration: 0.1 }} | ||
initial="hidden" | ||
animate={formState.isDirty ? "visible" : "hidden"} | ||
htmlType="submit" | ||
loading={formState.isSubmitting} | ||
type="primary" | ||
> | ||
{t("save")} | ||
</MotionButton> | ||
</div> | ||
<div className="divide-y"> | ||
<div className="flex p-4 pr-8"> | ||
<label htmlFor="name" className="w-1/3 text-slate-500"> | ||
{t("name")} | ||
</label> | ||
<div className="w-2/3"> | ||
<TextInput | ||
id="name" | ||
className="input w-full" | ||
placeholder="Jessie" | ||
readOnly={formState.isSubmitting} | ||
error={!!formState.errors.name} | ||
{...register("name", { | ||
validate: requiredString, | ||
})} | ||
/> | ||
{formState.errors.name ? ( | ||
<div className="mt-1 text-sm text-rose-500"> | ||
{t("requiredNameError")} | ||
</div> | ||
) : null} | ||
</div> | ||
</div> | ||
<div className="flex p-4 pr-8"> | ||
<label htmlFor="random-8904" className="w-1/3 text-slate-500"> | ||
{t("email")} | ||
</label> | ||
<div className="w-2/3"> | ||
<TextInput | ||
id="random-8904" | ||
className="input w-full" | ||
placeholder="[email protected]" | ||
disabled={true} | ||
{...register("email", { validate: validEmail })} | ||
/> | ||
</div> | ||
</div> | ||
</div> | ||
</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
Oops, something went wrong.