-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refat(api): decouple api logic from pages
- Loading branch information
1 parent
007f61f
commit 862f1b5
Showing
18 changed files
with
420 additions
and
243 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,22 @@ | ||
import React from 'react'; | ||
import { AlertProps } from '@patternfly/react-core'; | ||
|
||
const useAlerts = () => { | ||
const [alert, setAlerts] = React.useState<Partial<AlertProps>[]>([]); | ||
|
||
const addAlert = (title: string, variant: AlertProps['variant'], key: React.Key) => { | ||
setAlerts(prevAlerts => [...prevAlerts, { title, variant, key }]); | ||
}; | ||
|
||
const removeAlert = (key: React.Key) => { | ||
setAlerts(prevAlerts => [...prevAlerts.filter(alert => alert.key !== key)]); | ||
}; | ||
|
||
return { | ||
removeAlert, | ||
addAlert, | ||
alerts: alert | ||
}; | ||
}; | ||
|
||
export default useAlerts; |
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,40 @@ | ||
import { API_CREDS_LIST_QUERY } from 'src/constants/apiConstants'; | ||
import apiHandler from 'src/services/apiUtils'; | ||
import { CredentialType } from 'src/types'; | ||
import apiService from '../../services/apiService'; | ||
import useApiConfig from '../apiService/apiConfig'; | ||
import usePendingState from '../common/usePendingState'; | ||
|
||
const useCredentialApi = () => { | ||
const { pendingItem: pendingDeleteCredential, setPendingItem: setPendingDeleteCredential } = | ||
usePendingState<CredentialType>(); | ||
const selectedItems = []; | ||
const { addAlert, queryClient } = useApiConfig(); | ||
|
||
const onDeleteCredential = (credential: CredentialType) => { | ||
apiHandler( | ||
apiService.delete, | ||
`https://0.0.0.0:9443/api/v1/credentials/${credential.id}/`, | ||
null, | ||
`Credential "${credential.name}" deleted successfully`, | ||
`Error removing credential ${credential.name}.`, | ||
[API_CREDS_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
).finally(() => setPendingDeleteCredential(undefined)); | ||
}; | ||
|
||
const onDeleteSelectedCredentials = () => { | ||
const itemsToDelete = Object.values(selectedItems).filter(val => val !== null); | ||
// add logic | ||
console.log('Deleting selected credentials:', itemsToDelete); | ||
}; | ||
return { | ||
onDeleteCredential, | ||
onDeleteSelectedCredentials, | ||
pendingDeleteCredential, | ||
setPendingDeleteCredential | ||
}; | ||
}; | ||
|
||
export default useCredentialApi; |
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,55 @@ | ||
import React from 'react'; | ||
import { API_SCANS_LIST_QUERY } from 'src/constants/apiConstants'; | ||
import apiHandler from 'src/services/apiUtils'; | ||
import { ScanType } from 'src/types'; | ||
import apiService from '../../services/apiService'; | ||
import useApiConfig from '../apiService/apiConfig'; | ||
import usePendingState from '../common/usePendingState'; | ||
|
||
const useScanApi = () => { | ||
const { addAlert, queryClient } = useApiConfig(); | ||
const { pendingItem: pendingDeleteScan, setPendingItem: setPendingDeleteScan } = | ||
usePendingState<ScanType>(); | ||
const [scanSelected, setScanSelected] = React.useState<ScanType>(); | ||
|
||
const onDeleteScan = (scan: ScanType) => { | ||
apiHandler( | ||
apiService.delete, | ||
`https://0.0.0.0:9443/api/v1/scans/${scan.id}/`, | ||
null, | ||
`Scan "${scan.id}" deleted successfully`, | ||
`Error removing scan ${scan.id}`, | ||
[API_SCANS_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
).finally(() => setPendingDeleteScan(undefined)); | ||
}; | ||
|
||
const onRunScan = payload => { | ||
apiHandler( | ||
apiService.post, | ||
process.env.REACT_APP_SCANS_SERVICE, | ||
payload, | ||
`${payload.name} started to scan`, | ||
`Error starting scan.`, | ||
[API_SCANS_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
); | ||
}; | ||
|
||
// const onDeleteSelectedItems = () => { | ||
// // add logic | ||
// const selectedItems = []; | ||
// console.log('Deleting selected credentials:', selectedItems); | ||
// }; | ||
return { | ||
onDeleteScan, | ||
onRunScan, | ||
pendingDeleteScan, | ||
setPendingDeleteScan, | ||
scanSelected, | ||
setScanSelected | ||
}; | ||
}; | ||
export default useScanApi; |
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,141 @@ | ||
import React from 'react'; | ||
import { API_SOURCES_LIST_QUERY } from 'src/constants/apiConstants'; | ||
import apiHandler from 'src/services/apiUtils'; | ||
import { ConnectionType, SourceType } from 'src/types'; | ||
import apiService from '../../services/apiService'; | ||
import useApiConfig from '../apiService/apiConfig'; | ||
import usePendingState from '../common/usePendingState'; | ||
|
||
const useSourceApi = () => { | ||
const [scanSelected, setScanSelected] = React.useState<SourceType[]>(); | ||
const [addSourceModal, setAddSourceModal] = React.useState<string>(); | ||
const [sourceBeingEdited, setSourceBeingEdited] = React.useState<SourceType>(); | ||
const { pendingItem: pendingDeleteSource, setPendingItem: setPendingDeleteSource } = | ||
usePendingState<SourceType>(); | ||
const [connectionsData, setConnectionsData] = React.useState<{ | ||
successful: ConnectionType[]; | ||
failure: ConnectionType[]; | ||
unreachable: ConnectionType[]; | ||
}>({ successful: [], failure: [], unreachable: [] }); | ||
const [connectionsSelected, setConnectionsSelected] = React.useState<SourceType>(); | ||
const emptyConnectionData = { successful: [], failure: [], unreachable: [] }; | ||
const { addAlert, queryClient } = useApiConfig(); | ||
|
||
const onRunScan = payload => { | ||
apiHandler( | ||
apiService.post, | ||
`https://0.0.0.0:9443/api/v1/scans/`, | ||
payload, | ||
`${payload.name} started to scan`, | ||
`Error starting scan.`, | ||
[API_SOURCES_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
); | ||
}; | ||
|
||
const onAddSource = (payload: SourceType) => { | ||
apiHandler( | ||
apiService.post, | ||
`https://0.0.0.0:9443/api/v1/sources/?scan=true`, | ||
payload, | ||
`${payload.name} added successfully`, | ||
null, | ||
[API_SOURCES_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
); | ||
}; | ||
|
||
const onSubmitEditedSource = (payload: SourceType) => { | ||
apiHandler( | ||
apiService.put, | ||
`https://0.0.0.0:9443/api/v1/sources/${payload.id}/`, | ||
payload, | ||
`${payload.name} updated successfully`, | ||
null, | ||
[API_SOURCES_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
); | ||
}; | ||
|
||
const onDeleteSource = (source: SourceType) => { | ||
apiHandler( | ||
apiService.delete, | ||
`https://0.0.0.0:9443/api/v1/sources/${source.id}/`, | ||
null, | ||
`Source "${source.name}" deleted successfully`, | ||
`Error removing source ${source.name}.`, | ||
[API_SOURCES_LIST_QUERY], | ||
addAlert, | ||
queryClient | ||
).finally(() => setPendingDeleteSource(undefined)); | ||
}; | ||
|
||
const showConnections = (source: SourceType) => { | ||
apiService | ||
.get( | ||
`https://0.0.0.0:9443/api/v1/jobs/${source.connection.id}/connection/?page=1&page_size=1000&ordering=name&source_type=${source.id}` | ||
) | ||
.then(res => { | ||
setConnectionsData({ | ||
successful: res.data.results.filter((c: { status: string }) => c.status === 'success'), | ||
failure: res.data.results.filter((c: { status: string }) => c.status === 'failure'), | ||
unreachable: res.data.results.filter( | ||
(c: { status: string }) => !['success', 'failure'].includes(c.status) | ||
) | ||
}); | ||
}) | ||
.catch(err => console.error(err)); | ||
setConnectionsSelected(source); | ||
}; | ||
|
||
const onEditSource = (source: SourceType) => { | ||
setSourceBeingEdited(source); | ||
}; | ||
|
||
const onDeleteSelectedSources = () => { | ||
// add logic | ||
const selectedItems = []; | ||
console.log('Deleting selected credentials:', selectedItems); | ||
}; | ||
|
||
const onCloseConnections = () => { | ||
setConnectionsSelected(undefined); | ||
setConnectionsData(emptyConnectionData); | ||
}; | ||
|
||
const onScanSources = items => { | ||
setScanSelected(items); | ||
}; | ||
|
||
const onScanSource = (source: SourceType) => { | ||
setScanSelected([source]); | ||
}; | ||
|
||
return { | ||
onRunScan, | ||
onAddSource, | ||
onSubmitEditedSource, | ||
onDeleteSource, | ||
showConnections, | ||
onEditSource, | ||
onScanSources, | ||
scanSelected, | ||
setScanSelected, | ||
onDeleteSelectedSources, | ||
onCloseConnections, | ||
onScanSource, | ||
addSourceModal, | ||
setAddSourceModal, | ||
sourceBeingEdited, | ||
setSourceBeingEdited, | ||
pendingDeleteSource, | ||
setPendingDeleteSource, | ||
connectionsData, | ||
connectionsSelected | ||
}; | ||
}; | ||
|
||
export default useSourceApi; |
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,10 @@ | ||
import useAlerts from 'src/common/alertsHelper'; | ||
import useQueryClientConfig from 'src/services/queryClientConfig'; | ||
|
||
const useApiConfig = () => { | ||
const { addAlert } = useAlerts(); | ||
const { queryClient } = useQueryClientConfig(); | ||
|
||
return { addAlert, queryClient }; | ||
}; | ||
export default useApiConfig; |
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,8 @@ | ||
import React from 'react'; | ||
|
||
const usePendingState = <T>() => { | ||
const [pendingItem, setPendingItem] = React.useState<T>(); | ||
|
||
return { pendingItem, setPendingItem }; | ||
}; | ||
export default usePendingState; |
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.