-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(licensed-users): add licensed users download link (#2091)
* feat(licensed-users): add licensed users download link Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * feat(licensed-users): rename link, apply css styles to it Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> * feat(licensed-users): fix unit tests Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com> --------- Signed-off-by: Oleksandr Andriienko <oandriie@redhat.com>
- Loading branch information
1 parent
384434d
commit b3506ff
Showing
8 changed files
with
214 additions
and
1 deletion.
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,61 @@ | ||
import { | ||
ConfigApi, | ||
createApiRef, | ||
IdentityApi, | ||
} from '@backstage/core-plugin-api'; | ||
|
||
export type LicensedUsersAPI = { | ||
isLicensePluginEnabled(): Promise<boolean>; | ||
downloadStatistics: () => Promise<Response>; | ||
}; | ||
|
||
// @public | ||
export const licensedUsersApiRef = createApiRef<LicensedUsersAPI>({ | ||
id: 'plugin.licensed-users-info.service', | ||
}); | ||
|
||
export type Options = { | ||
configApi: ConfigApi; | ||
identityApi: IdentityApi; | ||
}; | ||
|
||
export class LicensedUsersAPIClient implements LicensedUsersAPI { | ||
// @ts-ignore | ||
private readonly configApi: ConfigApi; | ||
private readonly identityApi: IdentityApi; | ||
|
||
constructor(options: Options) { | ||
this.configApi = options.configApi; | ||
this.identityApi = options.identityApi; | ||
} | ||
async isLicensePluginEnabled(): Promise<boolean> { | ||
const { token: idToken } = await this.identityApi.getCredentials(); | ||
const backendUrl = this.configApi.getString('backend.baseUrl'); | ||
const jsonResponse = await fetch( | ||
`${backendUrl}/api/licensed-users-info/health`, | ||
{ | ||
headers: { | ||
...(idToken && { Authorization: `Bearer ${idToken}` }), | ||
}, | ||
}, | ||
); | ||
|
||
return jsonResponse.ok; | ||
} | ||
|
||
async downloadStatistics(): Promise<Response> { | ||
const { token: idToken } = await this.identityApi.getCredentials(); | ||
const backendUrl = this.configApi.getString('backend.baseUrl'); | ||
const response = await fetch( | ||
`${backendUrl}/api/licensed-users-info/users`, | ||
{ | ||
method: 'GET', | ||
headers: { | ||
...(idToken && { Authorization: `Bearer ${idToken}` }), | ||
'Content-Type': 'text/csv', | ||
}, | ||
}, | ||
); | ||
return response; | ||
} | ||
} |
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,66 @@ | ||
import React from 'react'; | ||
|
||
import { useApi } from '@backstage/core-plugin-api'; | ||
|
||
import { makeStyles } from '@material-ui/core'; | ||
|
||
import { licensedUsersApiRef } from '../api/LicensedUsersClient'; | ||
|
||
const useStyles = makeStyles(theme => ({ | ||
linkStyle: { | ||
color: theme.palette.link, | ||
textDecoration: 'underline', | ||
}, | ||
})); | ||
|
||
function DownloadCSVLink() { | ||
const classes = useStyles(); | ||
const licensedUsersClient = useApi(licensedUsersApiRef); | ||
const handleDownload = async ( | ||
event: React.MouseEvent<HTMLAnchorElement, MouseEvent>, | ||
) => { | ||
event.preventDefault(); // Prevent the default link behavior | ||
|
||
try { | ||
const response = await licensedUsersClient.downloadStatistics(); | ||
|
||
if (response.ok) { | ||
// Get the CSV data as a string | ||
const csvData = await response.text(); | ||
|
||
// Create a Blob from the CSV data | ||
const blob = new Blob([csvData], { type: 'text/csv' }); | ||
const url = window.URL.createObjectURL(blob); | ||
|
||
// Create a temporary link to trigger the download | ||
const a = document.createElement('a'); | ||
a.href = url; | ||
a.download = 'licensed-users.csv'; | ||
document.body.appendChild(a); | ||
a.click(); | ||
|
||
// Clean up the temporary link and object URL | ||
document.body.removeChild(a); | ||
window.URL.revokeObjectURL(url); | ||
} else { | ||
throw new Error( | ||
`Failed to download the csv file with list licensed users ${response.statusText}`, | ||
); | ||
} | ||
} catch (error) { | ||
throw new Error(`Error during the download: ${error}`); | ||
} | ||
}; | ||
|
||
return ( | ||
<a | ||
href="/download-csv" | ||
onClick={handleDownload} | ||
className={classes.linkStyle} | ||
> | ||
Download User List | ||
</a> | ||
); | ||
} | ||
|
||
export default DownloadCSVLink; |
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,24 @@ | ||
import { useAsync } from 'react-use'; | ||
|
||
import { useApi } from '@backstage/core-plugin-api'; | ||
|
||
import { licensedUsersApiRef } from '../api/LicensedUsersClient'; | ||
|
||
export const useCheckIfLicensePluginEnabled = (): { | ||
loading: boolean; | ||
isEnabled: boolean | undefined; | ||
licenseCheckError: Error; | ||
} => { | ||
const licensedUsersClient = useApi(licensedUsersApiRef); | ||
const { | ||
value: isEnabled, | ||
loading, | ||
error: licenseCheckError, | ||
} = useAsync(async () => await licensedUsersClient.isLicensePluginEnabled()); | ||
|
||
return { | ||
loading, | ||
isEnabled, | ||
licenseCheckError: licenseCheckError as 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