-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update credentials ui * hook for api calls * update local accounts state * error state
- Loading branch information
Showing
9 changed files
with
2,617 additions
and
56 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
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,35 @@ | ||
import axios from 'axios'; | ||
import { REVERT_BASE_API_URL } from '../../constants'; | ||
import { LOCALSTORAGE_KEYS } from '../localstorage'; | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: REVERT_BASE_API_URL, | ||
}); | ||
|
||
axiosInstance.interceptors.request.use( | ||
async (config) => { | ||
const token = localStorage.getItem(LOCALSTORAGE_KEYS.privateToken); | ||
if (token) { | ||
config.headers['x-revert-api-token'] = token; | ||
} | ||
config.headers['Content-Type'] = 'application/json'; | ||
return config; | ||
}, | ||
async (error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
axiosInstance.interceptors.response.use( | ||
async (response) => { | ||
return response; | ||
}, | ||
async (error) => { | ||
if (error.response && error.response.data) { | ||
return Promise.reject(error.response.data); | ||
} | ||
return Promise.reject(error.message); | ||
} | ||
); | ||
|
||
export default axiosInstance; |
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,26 @@ | ||
import React from 'react'; | ||
import axiosInstance from '../axios'; | ||
|
||
const useApi = () => { | ||
const [data, setData] = React.useState(); | ||
const [loading, setLoading] = React.useState(false); | ||
const [status, setStatus] = React.useState<number>(); | ||
|
||
const fetch = async ({ url, method, payload }) => { | ||
setLoading(true); | ||
try { | ||
const result = await axiosInstance({ url, method, data: payload }); | ||
setData(result.data); | ||
setStatus(result.status); | ||
} catch (err: any) { | ||
setData(err?.response?.data); | ||
setStatus(err?.response?.status || 500); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return { data, loading, status, fetch }; | ||
}; | ||
|
||
export { useApi }; |
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,3 @@ | ||
export const LOCALSTORAGE_KEYS = { | ||
privateToken: 'privateToken', | ||
}; |
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,165 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { Box as MuiBox, Button, Chip as MuiChip } from '@mui/material'; | ||
import { LoadingButton as MuiLoadingButton } from '@mui/lab'; | ||
|
||
import { useApi } from '../data/hooks'; | ||
|
||
const Chip = styled(MuiChip)` | ||
cursor: pointer; | ||
&:hover { | ||
border-color: #000; | ||
} | ||
`; | ||
|
||
const Row = styled.div` | ||
display: flex; | ||
align-items: flex-start; | ||
justify-content: space-between; | ||
width: 100%; | ||
padding: 10px; | ||
`; | ||
|
||
const Input = styled.input<{ error?: boolean }>` | ||
color: #444; | ||
width: 60%; | ||
border-bottom: 1px solid ${(props) => (!!props.error ? 'red' : '#444')}; | ||
outline: none; | ||
`; | ||
|
||
const Box = styled(MuiBox)` | ||
display: flex; | ||
flex-direction: column; | ||
background-color: #fff; | ||
width: 500px; | ||
border-radius: 20px; | ||
color: #444; | ||
position: absolute; | ||
top: 40%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
box-shadow: 24px; | ||
padding: 15px; | ||
`; | ||
|
||
const ScopesContainer = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-end; | ||
width: 70%; | ||
`; | ||
|
||
const Stack = styled.div` | ||
display: flex; | ||
justify-content: flex-end; | ||
gap: 5px; | ||
flex-wrap: wrap; | ||
`; | ||
|
||
const LoadingButton = styled(MuiLoadingButton)` | ||
background-color: #000; | ||
&.Mui-disabled { | ||
background-color: #000; | ||
} | ||
`; | ||
|
||
const EditCredentials: React.FC<{ app: any; handleClose: () => void; setAccount: any }> = ({ | ||
app, | ||
handleClose, | ||
setAccount, | ||
}) => { | ||
const [clientId, setClientId] = React.useState<string>(app.app_client_id); | ||
const [clientSecret, setClientSecret] = React.useState<string>(app.app_client_secret); | ||
const [scopes, setScopes] = React.useState<string[]>(app.scope); | ||
const [newScope, setNewScope] = React.useState<string>(''); | ||
|
||
const { data, loading, status, fetch } = useApi(); | ||
|
||
const handleAddNewScope = (e) => { | ||
if (e.key === 'Enter') { | ||
setScopes((ss) => [...ss, ...newScope.split(',').map((s) => s.trim())]); | ||
setNewScope(''); | ||
} | ||
}; | ||
|
||
const handleSubmit = async () => { | ||
await fetch({ | ||
url: '/internal/account/credentials', | ||
method: 'POST', | ||
payload: { clientId, clientSecret, scopes, tpId: app.tp_id }, | ||
}); | ||
}; | ||
|
||
React.useEffect(() => { | ||
if (status === 200) { | ||
handleClose(); | ||
// Update account locally to avoid additional api call | ||
setAccount((account) => { | ||
return { | ||
...account, | ||
apps: [...account.apps.filter((a) => a.tp_id !== app.tp_id), data], | ||
}; | ||
}); | ||
} | ||
}, [status, handleClose, setAccount, data, app]); | ||
|
||
return ( | ||
<Box> | ||
<Row> | ||
<span className="font-bold">Client ID: </span> | ||
<Input | ||
value={clientId} | ||
onChange={(ev) => setClientId((ev.target.value || '').trim())} | ||
error={!clientId} | ||
/> | ||
</Row> | ||
<Row> | ||
<span className="font-bold">Client Secret: </span> | ||
<Input | ||
value={clientSecret} | ||
onChange={(ev) => setClientSecret((ev.target.value || '').trim())} | ||
error={!clientSecret} | ||
/> | ||
</Row> | ||
<Row> | ||
<span className="font-bold">Scopes: </span> | ||
{/* <p className="break-words">{scopes}</p> */} | ||
<ScopesContainer> | ||
<Stack> | ||
{scopes.map((scope, i) => ( | ||
<Chip | ||
label={scope} | ||
key={i} | ||
variant="outlined" | ||
color="primary" | ||
style={{ color: '#fff', background: '#000' }} | ||
onDelete={(ev) => setScopes((ss) => [...ss.filter((s) => s !== scope)])} | ||
/> | ||
))} | ||
</Stack> | ||
<Input | ||
value={newScope} | ||
onChange={(ev) => setNewScope((ev.target.value || '').trim())} | ||
onKeyDown={handleAddNewScope} | ||
/> | ||
</ScopesContainer> | ||
</Row> | ||
<div style={{ width: '100%', display: 'flex', justifyContent: 'flex-end', gap: '15px' }}> | ||
<Button onClick={handleClose} style={{ color: '#000' }}> | ||
Close | ||
</Button> | ||
<LoadingButton | ||
variant="contained" | ||
onClick={handleSubmit} | ||
loading={loading} | ||
disabled={!clientId || !clientSecret} | ||
> | ||
Submit | ||
</LoadingButton> | ||
</div> | ||
</Box> | ||
); | ||
}; | ||
|
||
export default EditCredentials; |
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.