-
Notifications
You must be signed in to change notification settings - Fork 633
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Michał Miszczyszyn <[email protected]>
- Loading branch information
1 parent
834feee
commit 1d13c57
Showing
11 changed files
with
159 additions
and
104 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 |
---|---|---|
@@ -1,36 +1,19 @@ | ||
"use client"; | ||
|
||
import { useQuery } from "urql"; | ||
import { useSaleorAuthContext } from "@saleor/auth-sdk/react"; | ||
import Link from "next/link"; | ||
import { useRouter } from "next/navigation"; | ||
import { CurrentUserDocument, type CurrentUserQuery } from "@/gql/graphql"; | ||
import { LoginForm } from "@/ui/components/LoginForm"; | ||
import { UserCard } from "@/ui/components/UserCard"; | ||
|
||
export const LoginComponent = () => { | ||
const { signOut } = useSaleorAuthContext(); | ||
|
||
const router = useRouter(); | ||
const [{ data }] = useQuery<CurrentUserQuery>({ | ||
query: CurrentUserDocument.toString(), | ||
}); | ||
|
||
return data?.me ? ( | ||
<> | ||
<UserCard user={data.me} /> | ||
<div className="mt-4 flex space-x-2"> | ||
<Link href="/orders" className="h-full rounded bg-neutral-100 px-4 py-2 hover:bg-neutral-200"> | ||
My orders | ||
</Link> | ||
<button | ||
onClick={() => signOut()} | ||
className="rounded bg-neutral-800 px-4 py-2 text-neutral-200 hover:bg-neutral-700" | ||
type="button" | ||
> | ||
Log Out | ||
</button> | ||
</div> | ||
</> | ||
) : ( | ||
<LoginForm /> | ||
); | ||
if (data?.me) { | ||
router.push("/"); | ||
} | ||
|
||
return <LoginForm />; | ||
}; |
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,12 +1,5 @@ | ||
query CurrentUser { | ||
me { | ||
id | ||
firstName | ||
lastName | ||
avatar { | ||
url | ||
alt | ||
} | ||
...UserDetails | ||
} | ||
} |
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,10 @@ | ||
fragment UserDetails on User { | ||
id | ||
firstName | ||
lastName | ||
avatar { | ||
url | ||
alt | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,81 @@ | ||
"use client"; | ||
|
||
import { Fragment } from "react"; | ||
import Link from "next/link"; | ||
import clsx from "clsx"; | ||
import { useQuery } from "urql"; | ||
import { UserIcon } from "lucide-react"; | ||
import { Menu, Transition } from "@headlessui/react"; | ||
import { useSaleorAuthContext } from "@saleor/auth-sdk/react"; | ||
import { UserAvatar } from "./components/UserAvatar"; | ||
import { UserInfo } from "./components/UserInfo"; | ||
import { CurrentUserDocument, type CurrentUserQuery } from "@/gql/graphql"; | ||
|
||
export function UserMenu() { | ||
const { signOut } = useSaleorAuthContext(); | ||
const [{ data }] = useQuery<CurrentUserQuery>({ | ||
query: CurrentUserDocument.toString(), | ||
}); | ||
|
||
if (data?.me) { | ||
return ( | ||
<Menu as="div" className="relative ml-3"> | ||
<Menu.Button className="relative flex rounded-full bg-neutral-200 text-sm focus:outline-none focus:ring-2 focus:ring-white focus:ring-offset-2 focus:ring-offset-neutral-800"> | ||
<span className="sr-only">Open user menu</span> | ||
<UserAvatar user={data.me} /> | ||
</Menu.Button> | ||
<Transition | ||
as={Fragment} | ||
enter="transition ease-out duration-100" | ||
enterFrom="transform opacity-0 scale-95" | ||
enterTo="transform opacity-100 scale-100" | ||
leave="transition ease-in duration-75" | ||
leaveFrom="transform opacity-100 scale-100" | ||
leaveTo="transform opacity-0 scale-95" | ||
> | ||
<Menu.Items className="absolute right-0 z-10 mt-2 w-48 origin-top-right divide-y divide-neutral-200 bg-white py-1 text-start shadow ring-1 ring-neutral-200 ring-opacity-5 focus:outline-none"> | ||
<UserInfo user={data.me} /> | ||
<div className="flex flex-col px-1 py-1"> | ||
<Menu.Item> | ||
{({ active }) => ( | ||
<Link | ||
href="/orders" | ||
className={clsx( | ||
active && "bg-neutral-100", | ||
"block px-4 py-2 text-sm text-sm font-medium text-neutral-500 hover:text-neutral-700", | ||
)} | ||
> | ||
My orders | ||
</Link> | ||
)} | ||
</Menu.Item> | ||
</div> | ||
<div className="flex flex-col px-1 py-1"> | ||
<Menu.Item> | ||
{({ active }) => ( | ||
<button | ||
type="button" | ||
onClick={() => signOut()} | ||
className={clsx( | ||
active && "bg-neutral-100", | ||
"block px-4 py-2 text-start text-sm text-sm font-medium text-neutral-500 hover:text-neutral-700", | ||
)} | ||
> | ||
Log Out | ||
</button> | ||
)} | ||
</Menu.Item> | ||
</div> | ||
</Menu.Items> | ||
</Transition> | ||
</Menu> | ||
); | ||
} else { | ||
return ( | ||
<Link href="/login" className="h-6 w-6 flex-shrink-0"> | ||
<UserIcon className="h-6 w-6 shrink-0" aria-hidden="true" /> | ||
<span className="sr-only">Log in</span> | ||
</Link> | ||
); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/ui/components/nav/components/UserMenu/components/UserAvatar.tsx
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,35 @@ | ||
import Image from "next/image"; | ||
import { type UserDetailsFragment } from "@/gql/graphql"; | ||
|
||
type Props = { | ||
user: UserDetailsFragment; | ||
}; | ||
|
||
export const UserAvatar = ({ user }: Props) => { | ||
const label = | ||
user.firstName && user.lastName | ||
? `${user.firstName.slice(0, 1)}${user.lastName.slice(0, 1)}` | ||
: user.email.slice(0, 2); | ||
|
||
if (user.avatar) { | ||
return ( | ||
<Image | ||
className="h-8 w-8 rounded-full border" | ||
aria-hidden="true" | ||
src={user.avatar.url} | ||
width={24} | ||
height={24} | ||
alt="" | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<span | ||
className="flex h-8 w-8 items-center justify-center rounded-full border bg-white text-center text-xs font-bold uppercase" | ||
aria-hidden="true" | ||
> | ||
{label} | ||
</span> | ||
); | ||
}; |
16 changes: 16 additions & 0 deletions
16
src/ui/components/nav/components/UserMenu/components/UserInfo.tsx
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,16 @@ | ||
import { type UserDetailsFragment } from "@/gql/graphql"; | ||
|
||
type Props = { | ||
user: UserDetailsFragment; | ||
}; | ||
|
||
export const UserInfo = ({ user }: Props) => { | ||
const userName = user.firstName && user.lastName ? `${user.firstName} ${user.lastName}` : null; | ||
|
||
return ( | ||
<p className="truncate px-5 py-2 text-xs text-neutral-700"> | ||
{userName && <span className="mb-0.5 block truncate font-bold">{userName}</span>} | ||
{user.email} | ||
</p> | ||
); | ||
}; |