-
Notifications
You must be signed in to change notification settings - Fork 16
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 #531 from IQSS/api-token-usecase-integration
API Token use cases integration for account page
- Loading branch information
Showing
23 changed files
with
805 additions
and
22 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
49 changes: 49 additions & 0 deletions
49
src/sections/account/api-token-section/ApiTokenSectionSkeleton.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,49 @@ | ||
import { Trans, useTranslation } from 'react-i18next' | ||
import Skeleton, { SkeletonTheme } from 'react-loading-skeleton' | ||
import { Button } from '@iqss/dataverse-design-system' | ||
import accountStyles from '../Account.module.scss' | ||
import styles from './ApiTokenSection.module.scss' | ||
|
||
export const ApiTokenSectionSkeleton = () => { | ||
const { t } = useTranslation('account', { keyPrefix: 'apiToken' }) | ||
|
||
return ( | ||
<> | ||
<p className={accountStyles['helper-text']}> | ||
<Trans | ||
t={t} | ||
i18nKey="helperText" | ||
components={{ | ||
anchor: ( | ||
<a | ||
href="http://guides.dataverse.org/en/latest/api" | ||
target="_blank" | ||
rel="noreferrer" | ||
/> | ||
) | ||
}} | ||
/> | ||
</p> | ||
<SkeletonTheme> | ||
<div data-testid="loadingSkeleton"> | ||
<p className={styles['exp-date']}> | ||
{t('expirationDate')}{' '} | ||
<time data-testid="expiration-date"> | ||
<Skeleton width={100} /> | ||
</time> | ||
</p> | ||
<div className={styles['api-token']}> | ||
<code data-testid="api-token"> | ||
<Skeleton width={350} /> | ||
</code> | ||
</div> | ||
<div className={styles['btns-wrapper']} role="group"> | ||
<Button variant="secondary">{t('copyToClipboard')}</Button> | ||
<Button variant="secondary">{t('recreateToken')}</Button> | ||
<Button variant="secondary">{t('revokeToken')}</Button> | ||
</div> | ||
</div> | ||
</SkeletonTheme> | ||
</> | ||
) | ||
} |
49 changes: 49 additions & 0 deletions
49
src/sections/account/api-token-section/useGetCurrentApiToken.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,49 @@ | ||
import { useState, useEffect, useCallback } from 'react' | ||
import { TokenInfo } from '@/users/domain/models/TokenInfo' | ||
import { UserRepository } from '@/users/domain/repositories/UserRepository' | ||
import { getCurrentApiToken } from '@/users/domain/useCases/getCurrentApiToken' | ||
|
||
interface UseGetApiTokenResult { | ||
apiTokenInfo: TokenInfo | ||
isLoading: boolean | ||
error: string | null | ||
} | ||
|
||
export const useGetApiToken = (repository: UserRepository): UseGetApiTokenResult => { | ||
const [apiTokenInfo, setApiTokenInfo] = useState<TokenInfo>({ | ||
apiToken: '', | ||
expirationDate: new Date(0) | ||
}) | ||
const [isLoading, setIsLoading] = useState<boolean>(true) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const fetchTokenInfo = useCallback(async () => { | ||
try { | ||
setIsLoading(true) | ||
const tokenInfo = await getCurrentApiToken(repository) | ||
setApiTokenInfo({ | ||
apiToken: tokenInfo.apiToken, | ||
expirationDate: tokenInfo.expirationDate | ||
}) | ||
setError(null) | ||
} catch (err) { | ||
const errorMessage = | ||
err instanceof Error && err.message | ||
? err.message | ||
: 'Something went wrong getting the current api token. Try again later.' | ||
setError( | ||
errorMessage === | ||
'There was an error when reading the resource. Reason was: [404] Token not found.' | ||
? null | ||
: errorMessage | ||
) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [repository]) | ||
|
||
useEffect(() => { | ||
void fetchTokenInfo() | ||
}, [fetchTokenInfo]) | ||
return { isLoading, error, apiTokenInfo } | ||
} |
36 changes: 36 additions & 0 deletions
36
src/sections/account/api-token-section/useRecreateApiToken.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,36 @@ | ||
import { useState } from 'react' | ||
import { recreateApiToken } from '@/users/domain/useCases/recreateApiToken' | ||
import { TokenInfo } from '@/users/domain/models/TokenInfo' | ||
import { UserRepository } from '@/users/domain/repositories/UserRepository' | ||
|
||
interface UseRecreateApiTokenResult { | ||
recreateToken: () => Promise<void> | ||
isRecreating: boolean | ||
error: string | null | ||
apiTokenInfo: TokenInfo | null | ||
} | ||
|
||
export const useRecreateApiToken = (repository: UserRepository): UseRecreateApiTokenResult => { | ||
const [isRecreating, setIsRecreating] = useState<boolean>(false) | ||
const [error, setError] = useState<string | null>(null) | ||
const [apiTokenInfo, setApiTokenInfo] = useState<TokenInfo | null>(null) | ||
|
||
const recreateToken = async () => { | ||
setIsRecreating(true) | ||
setError(null) | ||
try { | ||
const newTokenInfo = await recreateApiToken(repository) | ||
setApiTokenInfo(newTokenInfo) | ||
} catch (err) { | ||
const errorMessage = | ||
err instanceof Error && err.message | ||
? err.message | ||
: 'Something went wrong creating the api token. Try again later.' | ||
setError(errorMessage) | ||
} finally { | ||
setIsRecreating(false) | ||
} | ||
} | ||
|
||
return { recreateToken, isRecreating, error, apiTokenInfo } | ||
} |
32 changes: 32 additions & 0 deletions
32
src/sections/account/api-token-section/useRevokeApiToken.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,32 @@ | ||
import { useState } from 'react' | ||
import { revokeApiToken } from '@/users/domain/useCases/revokeApiToken' | ||
import { UserRepository } from '@/users/domain/repositories/UserRepository' | ||
|
||
interface UseRevokeApiTokenResult { | ||
revokeToken: () => Promise<void> | ||
isRevoking: boolean | ||
error: string | null | ||
} | ||
|
||
export const useRevokeApiToken = (repository: UserRepository): UseRevokeApiTokenResult => { | ||
const [isRevoking, setIsRevoking] = useState<boolean>(false) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const revokeToken = async () => { | ||
setIsRevoking(true) | ||
setError(null) | ||
try { | ||
await revokeApiToken(repository) | ||
} catch (err) { | ||
const errorMessage = | ||
err instanceof Error && err.message | ||
? err.message | ||
: 'Something went wrong revoking the api token. Try again later.' | ||
setError(errorMessage) | ||
} finally { | ||
setIsRevoking(false) | ||
} | ||
} | ||
|
||
return { revokeToken, isRevoking, error } | ||
} |
Oops, something went wrong.