-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: users can now select a different domain (#1050)
- Loading branch information
1 parent
2031365
commit 5afa842
Showing
6 changed files
with
173 additions
and
17 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,102 @@ | ||
import { Card, Chip, Company, styled } from '@takaro/lib-components'; | ||
import { DomainOutputDTO } from '@takaro/apiclient'; | ||
import { createFileRoute, useNavigate } from '@tanstack/react-router'; | ||
import { useUserSetSelectedDomain, userMeQueryOptions } from 'queries/user'; | ||
import { MdDomain as DomainIcon } from 'react-icons/md'; | ||
import { AiOutlineArrowRight as ArrowRightIcon } from 'react-icons/ai'; | ||
|
||
export const TAKARO_DOMAIN_COOKIE_REGEX = /(?:(?:^|.*;\s*)takaro-domain\s*\=\s*([^;]*).*$)|^.*$/; | ||
|
||
const Container = styled.div` | ||
padding: ${({ theme }) => theme.spacing[4]}; | ||
height: 100vh; | ||
`; | ||
|
||
const DomainCardList = styled.div` | ||
display: flex; | ||
align-items: flex-start; | ||
justify-content: center; | ||
flex-direction: column; | ||
margin: auto; | ||
width: 450px; | ||
gap: ${({ theme }) => theme.spacing[2]}; | ||
height: 85vh; | ||
`; | ||
|
||
export const CardBody = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: space-between; | ||
width: 500px; | ||
height: 100px; | ||
`; | ||
|
||
export const Route = createFileRoute('/_auth/domain/select')({ | ||
component: Component, | ||
loader: async ({ context }) => { | ||
return await context.queryClient.ensureQueryData(userMeQueryOptions()); | ||
}, | ||
}); | ||
|
||
function Component() { | ||
const me = Route.useLoaderData(); | ||
const currentDomain = document.cookie.replace(TAKARO_DOMAIN_COOKIE_REGEX, '$1'); | ||
|
||
// Keep current domain at the top | ||
me.domains.sort((a, b) => { | ||
if (a.id === currentDomain) { | ||
return -1; | ||
} | ||
if (b.id === currentDomain) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
|
||
return ( | ||
<Container> | ||
<Company /> | ||
<DomainCardList> | ||
<h2>Select a domain:</h2> | ||
{me.domains.map((domain) => ( | ||
<> | ||
<DomainCard domain={domain} isCurrentDomain={currentDomain === domain.id} /> | ||
</> | ||
))} | ||
</DomainCardList> | ||
</Container> | ||
); | ||
} | ||
|
||
interface DomainCardProps { | ||
domain: DomainOutputDTO; | ||
isCurrentDomain: boolean; | ||
} | ||
|
||
function DomainCard({ domain, isCurrentDomain }: DomainCardProps) { | ||
const navigate = useNavigate(); | ||
const { mutate } = useUserSetSelectedDomain(); | ||
|
||
const handleClick = () => { | ||
if (isCurrentDomain === false) { | ||
mutate({ domainId: domain.id }); | ||
} | ||
navigate({ to: '/dashboard' }); | ||
}; | ||
|
||
return ( | ||
<Card role="link" onClick={handleClick}> | ||
<CardBody> | ||
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}> | ||
<DomainIcon size={30} /> | ||
{isCurrentDomain && <Chip variant="outline" color="primary" label="current domain" />} | ||
</div> | ||
<h2 style={{ display: 'flex', alignItems: 'center' }}> | ||
{domain.name} | ||
<ArrowRightIcon size={18} style={{ marginLeft: '10px' }} /> | ||
</h2> | ||
<div></div> | ||
</CardBody> | ||
</Card> | ||
); | ||
} |
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