-
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.
feat:current api usecase integration
- Loading branch information
1 parent
02bc675
commit 6ade2c3
Showing
18 changed files
with
626 additions
and
21 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
import Skeleton, { SkeletonTheme } from 'react-loading-skeleton' | ||
import 'react-loading-skeleton/dist/skeleton.css' | ||
import { Trans, useTranslation } from 'react-i18next' | ||
import accountStyles from '../Account.module.scss' | ||
import { Button } from '@iqss/dataverse-design-system' | ||
import styles from './ApiTokenSection.module.scss' | ||
|
||
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> | ||
</> | ||
) | ||
} | ||
export default ApiTokenSectionSkeleton |
43 changes: 43 additions & 0 deletions
43
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,43 @@ | ||
import { useState, useEffect, useCallback } from 'react' | ||
import { TokenInfo } from '@/users/domain/models/TokenInfo' | ||
import { ApiTokenInfoRepository } from '@/users/domain/repositories/ApiTokenInfoRepository' | ||
import { getCurrentApiToken } from '@/users/domain/useCases/getCurrentApiToken' | ||
|
||
interface UseGetApiTokenResult { | ||
apiTokenInfo: TokenInfo | ||
isLoading: boolean | ||
error: string | null | ||
} | ||
|
||
export const useGetApiToken = (repository: ApiTokenInfoRepository): UseGetApiTokenResult => { | ||
const [apiTokenInfo, setApiTokenInfo] = useState<TokenInfo>({ | ||
apiToken: '', | ||
expirationDate: '' | ||
}) | ||
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 : 'Failed to fetch API token.' | ||
console.error(errorMessage) | ||
setError(errorMessage) | ||
} finally { | ||
setIsLoading(false) | ||
} | ||
}, [repository]) | ||
|
||
useEffect(() => { | ||
void fetchTokenInfo() | ||
}, [fetchTokenInfo]) | ||
|
||
return { error, apiTokenInfo, isLoading } | ||
} |
47 changes: 47 additions & 0 deletions
47
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,47 @@ | ||
import { useState, useEffect } from 'react' | ||
import { recreateApiToken } from '@/users/domain/useCases/recreateApiToken' | ||
import { TokenInfo } from '@/users/domain/models/TokenInfo' | ||
import { ApiTokenInfoRepository } from '@/users/domain/repositories/ApiTokenInfoRepository' | ||
|
||
interface UseRecreateApiTokenResult { | ||
initiateRecreateToken: () => void | ||
isRecreating: boolean | ||
error: string | null | ||
apiTokenInfo: TokenInfo | null | ||
} | ||
|
||
export const useRecreateApiToken = ( | ||
repository: ApiTokenInfoRepository | ||
): UseRecreateApiTokenResult => { | ||
const [isRecreating, setIsRecreating] = useState<boolean>(false) | ||
const [error, setError] = useState<string | null>(null) | ||
const [apiTokenInfo, setApiTokenInfo] = useState<TokenInfo | null>(null) | ||
const [shouldRecreate, setShouldRecreate] = useState<boolean>(false) | ||
|
||
const initiateRecreateToken = () => { | ||
setShouldRecreate(true) | ||
} | ||
|
||
useEffect(() => { | ||
const recreateToken = async () => { | ||
setIsRecreating(true) | ||
setError(null) | ||
|
||
try { | ||
const newTokenInfo = await recreateApiToken(repository) | ||
setApiTokenInfo(newTokenInfo) | ||
} catch (err) { | ||
console.error('Error recreating token:', err) | ||
setError('Failed to recreate API token.') | ||
} finally { | ||
setIsRecreating(false) | ||
setShouldRecreate(false) | ||
} | ||
} | ||
if (shouldRecreate) { | ||
void recreateToken() | ||
} | ||
}, [shouldRecreate, repository]) | ||
|
||
return { initiateRecreateToken, isRecreating, error, apiTokenInfo } | ||
} |
30 changes: 30 additions & 0 deletions
30
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,30 @@ | ||
import { useState, useCallback } from 'react' | ||
import { revokeApiToken } from '@/users/domain/useCases/revokeApiToken' | ||
import { ApiTokenInfoRepository } from '@/users/domain/repositories/ApiTokenInfoRepository' | ||
|
||
interface UseRevokeApiTokenResult { | ||
revokeToken: () => Promise<void> | ||
isRevoking: boolean | ||
error: string | null | ||
} | ||
|
||
export const useRevokeApiToken = (repository: ApiTokenInfoRepository): UseRevokeApiTokenResult => { | ||
const [isRevoking, setIsRevoking] = useState<boolean>(false) | ||
const [error, setError] = useState<string | null>(null) | ||
|
||
const revokeToken = useCallback(async () => { | ||
setIsRevoking(true) | ||
setError(null) | ||
|
||
try { | ||
await revokeApiToken(repository) | ||
} catch (err) { | ||
console.error('There was an error revoking Api token:', err) | ||
setError('Failed to revoke API token.') | ||
} finally { | ||
setIsRevoking(false) | ||
} | ||
}, [repository]) | ||
|
||
return { revokeToken, isRevoking, error } | ||
} |
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,4 @@ | ||
export interface TokenInfo { | ||
apiToken: string | ||
expirationDate: string | ||
} |
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,7 @@ | ||
import { TokenInfo } from '../.././domain/models/TokenInfo' | ||
|
||
export interface ApiTokenInfoRepository { | ||
getCurrentApiToken(): Promise<TokenInfo> | ||
recreateApiToken(): Promise<TokenInfo> | ||
deleteApiToken(): Promise<void> | ||
} |
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,6 @@ | ||
import { TokenInfo } from '../models/TokenInfo' | ||
import { ApiTokenInfoRepository } from '../repositories/ApiTokenInfoRepository' | ||
|
||
export function getCurrentApiToken(apiTokenRepository: ApiTokenInfoRepository): Promise<TokenInfo> { | ||
return apiTokenRepository.getCurrentApiToken() | ||
} |
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,6 @@ | ||
import { TokenInfo } from '../models/TokenInfo' | ||
import { ApiTokenInfoRepository } from '../repositories/ApiTokenInfoRepository' | ||
|
||
export function recreateApiToken(apiTokenRepository: ApiTokenInfoRepository): Promise<TokenInfo> { | ||
return apiTokenRepository.recreateApiToken() | ||
} |
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,5 @@ | ||
import { ApiTokenInfoRepository } from '../repositories/ApiTokenInfoRepository' | ||
|
||
export function revokeApiToken(apiTokenRepository: ApiTokenInfoRepository): Promise<void> { | ||
return apiTokenRepository.deleteApiToken() | ||
} |
Oops, something went wrong.