-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Handle missing auth and improve error handling"
This reverts commit 76103b6.
- Loading branch information
Showing
15 changed files
with
102 additions
and
136 deletions.
There are no files selected for viewing
75 changes: 53 additions & 22 deletions
75
frontend/src/components/autosave-progress/autosave-progress.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 |
---|---|---|
@@ -1,45 +1,76 @@ | ||
import { useTranslation } from '@app/language/use-translation'; | ||
import { CheckmarkIcon } from '@navikt/aksel-icons'; | ||
import { Tooltip } from '@navikt/ds-react'; | ||
import { formatDate, isToday, parseISO } from 'date-fns'; | ||
import { CheckmarkIcon, XMarkOctagonIcon } from '@navikt/aksel-icons'; | ||
import { Popover } from '@navikt/ds-react'; | ||
import { formatDate, formatISO, isToday } from 'date-fns'; | ||
import { useRef, useState } from 'react'; | ||
import { styled } from 'styled-components'; | ||
|
||
interface Props { | ||
lastSaved: string; | ||
isLoading: boolean; | ||
isError: boolean; | ||
isSuccess: boolean; | ||
lastSaved: Date; | ||
} | ||
|
||
const AutosaveContainer = styled.div` | ||
display: flex; | ||
justify-content: flex-start; | ||
align-items: center; | ||
text-align: right; | ||
column-gap: 4px; | ||
margin-top: 4px; | ||
color: var(--a-text-subtle); | ||
`; | ||
|
||
export const AutosaveProgressIndicator = ({ lastSaved }: Props) => { | ||
const AutosaveContent = styled.div` | ||
cursor: pointer; | ||
> svg { | ||
margin-right: 5px; | ||
} | ||
display: flex; | ||
flex-flow: row wrap; | ||
align-items: center; | ||
`; | ||
|
||
export const AutosaveProgressIndicator = (props: Props) => { | ||
const { skjema } = useTranslation(); | ||
const { tooltip: popover, saved } = skjema.begrunnelse.autosave; | ||
const { popover, saved, failed } = skjema.begrunnelse.autosave; | ||
const anchor = useRef<HTMLDivElement>(null); | ||
const [isOpen, setIsOpen] = useState(false); | ||
|
||
return ( | ||
<Tooltip content={popover}> | ||
<AutosaveContainer> | ||
<CheckmarkIcon aria-hidden /> | ||
<span> | ||
{saved} {getDate(lastSaved)} | ||
</span> | ||
</AutosaveContainer> | ||
</Tooltip> | ||
<AutosaveContainer style={{ color: props.isError ? 'var(--a-text-danger)' : 'var(--a-text-subtle)' }}> | ||
<AutosaveContent ref={anchor} onClick={() => setIsOpen(!isOpen)}> | ||
{getContent(props, saved, failed)} | ||
</AutosaveContent> | ||
<Popover open={isOpen} anchorEl={anchor.current} onClose={() => setIsOpen(false)} placement="top-end"> | ||
<Popover.Content>{popover}</Popover.Content> | ||
</Popover> | ||
</AutosaveContainer> | ||
); | ||
}; | ||
|
||
const getDate = (date: string) => { | ||
const parsed = parseISO(date); | ||
const getContent = (status: Props, saved: string, failed: string) => { | ||
if (status.isError) { | ||
return ( | ||
<> | ||
<XMarkOctagonIcon aria-hidden /> | ||
{failed} | ||
</> | ||
); | ||
} | ||
|
||
return ( | ||
<> | ||
<CheckmarkIcon aria-hidden /> | ||
<span> | ||
{saved} {getDate(status.lastSaved)} | ||
</span> | ||
</> | ||
); | ||
}; | ||
|
||
if (isToday(parsed)) { | ||
return <time dateTime={date}>{formatDate(parsed, 'HH:mm:ss')}</time>; | ||
const getDate = (date: Date) => { | ||
if (isToday(date)) { | ||
return <time dateTime={formatISO(date)}>{formatDate(date, 'HH:mm:ss')}</time>; | ||
} | ||
|
||
return <time dateTime={date}>{formatDate(parsed, 'dd.MM.yyyy HH:mm:ss')}</time>; | ||
return <time dateTime={formatISO(date)}>{formatDate(date, 'dd.MM.yyyy HH:mm:ss')}</time>; | ||
}; |
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
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 |
---|---|---|
@@ -1,53 +1,14 @@ | ||
import { useGetSessionQuery, useGetUserQuery } from '@app/redux-api/user/api'; | ||
import type { IUser } from '@app/redux-api/user/types'; | ||
import { type SkipToken, skipToken } from '@reduxjs/toolkit/query'; | ||
|
||
interface LoadingAuth { | ||
isAuthenticated: undefined; | ||
isLoadingAuth: true; | ||
} | ||
export const useIsAuthenticated = (skip?: SkipToken) => { | ||
const { data, ...rest } = useGetSessionQuery(skip, { refetchOnFocus: true, refetchOnReconnect: true }); | ||
|
||
interface LoadedAuth { | ||
isAuthenticated: boolean; | ||
isLoadingAuth: false; | ||
} | ||
|
||
type AuthResult = LoadingAuth | LoadedAuth; | ||
|
||
export const useIsAuthenticated = (skip?: SkipToken): AuthResult => { | ||
const { isAuthenticated, isLoading } = useGetSessionQuery(skip, { | ||
refetchOnFocus: true, | ||
refetchOnReconnect: true, | ||
selectFromResult: ({ data, isLoading }) => ({ isLoading, isAuthenticated: data?.session.active }), | ||
}); | ||
|
||
if (isLoading || isAuthenticated === undefined) { | ||
return { isAuthenticated: undefined, isLoadingAuth: true }; | ||
} | ||
|
||
return { isAuthenticated, isLoadingAuth: false }; | ||
return { ...rest, data: data?.session.active ?? false }; | ||
}; | ||
|
||
interface LoadedUser { | ||
user: IUser; | ||
isLoadingUser: false; | ||
} | ||
|
||
interface LoadingUser { | ||
user: undefined; | ||
isLoadingUser: true; | ||
} | ||
|
||
type UserResult = LoadedUser | LoadingUser; | ||
|
||
export const useUser = (): UserResult => { | ||
const { isAuthenticated } = useIsAuthenticated(); | ||
|
||
const { data: user } = useGetUserQuery(isAuthenticated === true ? undefined : skipToken); | ||
|
||
if (user === undefined) { | ||
return { user: undefined, isLoadingUser: true }; | ||
} | ||
export const useUser = () => { | ||
const { data } = useIsAuthenticated(); | ||
|
||
return { user, isLoadingUser: false }; | ||
return useGetUserQuery(data === true ? undefined : skipToken); | ||
}; |
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
Oops, something went wrong.