-
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.
Merge pull request #25 from espoon-voltti/updates-to-basic-flow
updates to basic flow
- Loading branch information
Showing
22 changed files
with
961 additions
and
539 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, { useContext } from 'react' | ||
import { Navigate } from 'react-router-dom' | ||
|
||
import { UserContext } from './UserContext' | ||
|
||
type AuthMode = 'AUTHENTICATED_ONLY' | 'UNAUTHENTICATED_ONLY' | 'ALL' | ||
|
||
export const AuthGuard = React.memo(function AuthGuard({ | ||
allow, | ||
children | ||
}: { | ||
allow: AuthMode | ||
children: React.JSX.Element | ||
}) { | ||
const { user } = useContext(UserContext) | ||
|
||
switch (allow) { | ||
case 'ALL': | ||
return children | ||
case 'AUTHENTICATED_ONLY': | ||
if (user) { | ||
return children | ||
} else { | ||
return <Navigate replace to="/kirjaudu" /> | ||
} | ||
case 'UNAUTHENTICATED_ONLY': | ||
if (user) { | ||
return <Navigate replace to="/oppivelvolliset" /> | ||
} else { | ||
return children | ||
} | ||
} | ||
}) |
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,40 @@ | ||
import React, { createContext, useMemo, useState } from 'react' | ||
|
||
export interface User { | ||
name: string | ||
} | ||
|
||
export interface UserState { | ||
user: User | null | ||
setLoggedIn: (user: User) => void | ||
setLoggedOut: () => void | ||
} | ||
|
||
export const UserContext = createContext<UserState>({ | ||
user: { | ||
name: 'Tessa Testaaja' | ||
}, | ||
setLoggedIn: () => undefined, | ||
setLoggedOut: () => undefined | ||
}) | ||
|
||
export const UserContextProvider = React.memo(function UserContextProvider({ | ||
children | ||
}: { | ||
children: React.JSX.Element | ||
}) { | ||
const [user, setUser] = useState<User | null>({ | ||
name: 'Tessa Testaaja' | ||
}) | ||
|
||
const value = useMemo( | ||
() => ({ | ||
user, | ||
setLoggedIn: setUser, | ||
setLoggedOut: () => setUser(null) | ||
}), | ||
[user, setUser] | ||
) | ||
|
||
return <UserContext.Provider value={value}>{children}</UserContext.Provider> | ||
}) |
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 React, { useContext } from 'react' | ||
|
||
import { FlexRowWithGaps } from '../shared/layout' | ||
|
||
import { UserContext } from './UserContext' | ||
|
||
export const UserHeader = React.memo(function UserHeader() { | ||
const { user, setLoggedOut } = useContext(UserContext) | ||
|
||
if (!user) return null | ||
|
||
return ( | ||
<FlexRowWithGaps> | ||
<span>{user.name}</span> | ||
<a href="#" onClick={setLoggedOut}> | ||
Kirjaudu ulos | ||
</a> | ||
</FlexRowWithGaps> | ||
) | ||
}) |
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,13 @@ | ||
export type JsonOf<T> = T extends string | number | boolean | null | undefined | ||
? T | ||
: T extends Date | ||
? string | ||
: T extends Map<string, infer U> | ||
? { [key: string]: JsonOf<U> } | ||
: T extends Set<infer U> | ||
? JsonOf<U>[] | ||
: T extends (infer U)[] | ||
? JsonOf<U>[] | ||
: T extends object // eslint-disable-line @typescript-eslint/ban-types | ||
? { [P in keyof T]: JsonOf<T[P]> } | ||
: never |
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,29 @@ | ||
import React, { useContext } from 'react' | ||
import styled from 'styled-components' | ||
|
||
import { UserContext } from '../auth/UserContext' | ||
import { FlexColWithGaps } from '../shared/layout' | ||
import { H2 } from '../shared/typography' | ||
|
||
const Wrapper = styled.div` | ||
width: 100%; | ||
height: 600px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
` | ||
|
||
export const LoginPage = React.memo(function LoginPage() { | ||
const { setLoggedIn } = useContext(UserContext) | ||
|
||
return ( | ||
<Wrapper> | ||
<FlexColWithGaps $gapSize="L"> | ||
<H2>Kirjaudu sisään Espoo-AD:lla</H2> | ||
<button onClick={() => setLoggedIn({ name: 'Tessa Testaaja' })}> | ||
Kirjaudu sisään | ||
</button> | ||
</FlexColWithGaps> | ||
</Wrapper> | ||
) | ||
}) |
Oops, something went wrong.