-
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.
Merge pull request #206 from kbase/URO-206
URO-206 getting started with orcidlink integration
- Loading branch information
Showing
13 changed files
with
431 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { baseApi } from '.'; | ||
import { jsonRpcService } from './utils/serviceHelpers'; | ||
|
||
// orcidlink system types | ||
|
||
export interface ORCIDAuthPublic { | ||
expires_in: number; | ||
name: string; | ||
orcid: string; | ||
scope: string; | ||
} | ||
|
||
export interface LinkRecordPublic { | ||
created_at: number; | ||
expires_at: number; | ||
retires_at: number; | ||
username: string; | ||
orcid_auth: ORCIDAuthPublic; | ||
} | ||
|
||
// Method types | ||
|
||
export interface StatusResult { | ||
status: string; | ||
current_time: number; | ||
start_time: number; | ||
} | ||
|
||
export interface InfoResult { | ||
'service-description': { | ||
name: string; | ||
title: string; | ||
version: string; | ||
}; | ||
} | ||
|
||
// is-linked | ||
|
||
export interface IsLinkedParams { | ||
username: string; | ||
} | ||
|
||
export type IsLinkedResult = boolean; | ||
|
||
// owner-link | ||
export interface OwnerLinkParams { | ||
username: string; | ||
} | ||
|
||
export type OwnerLinkResult = LinkRecordPublic; | ||
|
||
// It is mostly a JSONRPC 2.0 service, although the oauth flow is rest-ish. | ||
const orcidlinkService = jsonRpcService({ | ||
url: '/services/orcidlink/api/v1', | ||
version: '2.0', | ||
}); | ||
|
||
/** | ||
* orcidlink service api | ||
*/ | ||
export const orcidlinkAPI = baseApi | ||
.enhanceEndpoints({ addTagTypes: ['ORCIDLink'] }) | ||
.injectEndpoints({ | ||
endpoints: ({ query }) => ({ | ||
orcidlinkStatus: query<StatusResult, {}>({ | ||
query: () => { | ||
return orcidlinkService({ | ||
method: 'status', | ||
}); | ||
}, | ||
}), | ||
orcidlinkIsLinked: query<IsLinkedResult, IsLinkedParams>({ | ||
query: ({ username }) => { | ||
return orcidlinkService({ | ||
method: 'is-linked', | ||
params: { | ||
username, | ||
}, | ||
}); | ||
}, | ||
}), | ||
orcidlinkOwnerLink: query<OwnerLinkResult, OwnerLinkParams>({ | ||
query: ({ username }) => { | ||
return orcidlinkService({ | ||
method: 'owner-link', | ||
params: { | ||
username, | ||
}, | ||
}); | ||
}, | ||
}), | ||
}), | ||
}); |
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,36 @@ | ||
import Alert from '@mui/material/Alert'; | ||
import { SerializedError } from '@reduxjs/toolkit'; | ||
import { KBaseBaseQueryError } from '../../common/api/utils/common'; | ||
import styles from './orcidlink.module.scss'; | ||
|
||
export interface ErrorMessageProps { | ||
error: KBaseBaseQueryError | SerializedError; | ||
} | ||
|
||
export default function ErrorMessage({ error }: ErrorMessageProps) { | ||
const message = (() => { | ||
if ('status' in error) { | ||
switch (error.status) { | ||
case 'JSONRPC_ERROR': | ||
return error.data.error.message; | ||
case 'FETCH_ERROR': | ||
return 'Fetch Error'; | ||
case 'CUSTOM_ERROR': | ||
return error.error; | ||
case 'PARSING_ERROR': | ||
return error.error; | ||
case 'TIMEOUT_ERROR': | ||
return error.error; | ||
} | ||
} else { | ||
return error.message || 'Unknown Error'; | ||
} | ||
})(); | ||
return ( | ||
<div className={styles['error-message']}> | ||
<Alert severity="error" title="Error"> | ||
{message} | ||
</Alert> | ||
</div> | ||
); | ||
} |
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,73 @@ | ||
import { Alert, AlertTitle, CircularProgress } from '@mui/material'; | ||
import { SerializedError } from '@reduxjs/toolkit'; | ||
import { orcidlinkAPI } from '../../common/api/orcidlinkAPI'; | ||
import { KBaseBaseQueryError } from '../../common/api/utils/common'; | ||
import { useAppSelector } from '../../common/hooks'; | ||
import { authUsername } from '../auth/authSlice'; | ||
import { usePageTitle } from '../layout/layoutSlice'; | ||
import ErrorMessage from './ErrorMessage'; | ||
import Linked from './Linked'; | ||
import styles from './orcidlink.module.scss'; | ||
import Unlinked from './Unlinked'; | ||
|
||
export default function Home() { | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const username = useAppSelector(authUsername)!; | ||
|
||
function renderLoading(title: string, description: string) { | ||
return ( | ||
<div className={styles.loading}> | ||
<Alert icon={<CircularProgress size="1rem" />}> | ||
<AlertTitle> | ||
<span className={styles['loading-title']}>{title}</span> | ||
</AlertTitle> | ||
<p>{description}</p> | ||
</Alert> | ||
</div> | ||
); | ||
} | ||
|
||
function renderError(error: KBaseBaseQueryError | SerializedError) { | ||
return <ErrorMessage error={error} />; | ||
} | ||
|
||
usePageTitle('KBase ORCID Link'); | ||
|
||
const { | ||
data: isLInked, | ||
error, | ||
isLoading, | ||
isError, | ||
isFetching, | ||
isSuccess, | ||
isUninitialized, | ||
} = orcidlinkAPI.useOrcidlinkIsLinkedQuery({ username }); | ||
|
||
if (isUninitialized) { | ||
return renderLoading('Uninitialized...', 'Loading the ORCID Link App...'); | ||
} else if (isLoading) { | ||
return renderLoading('Loading...', 'Loading the ORCID Link App...'); | ||
} else if (isFetching) { | ||
return renderLoading('Fetching...', 'Loading the ORCID Link App...'); | ||
} else if (isError) { | ||
return renderError(error); | ||
} else if (isSuccess) { | ||
if (isLInked) { | ||
return ( | ||
<div className={styles.box}> | ||
<Linked /> | ||
</div> | ||
); | ||
} | ||
return ( | ||
<div className={styles.box}> | ||
<Unlinked /> | ||
</div> | ||
); | ||
} else { | ||
return renderError({ | ||
status: 'CUSTOM_ERROR', | ||
error: 'Unknown State', | ||
}); | ||
} | ||
} |
Oops, something went wrong.