|
| 1 | +import React from 'react'; |
| 2 | +import get from 'lodash/get'; |
| 3 | +import { useTheme } from 'providers/Theme'; |
| 4 | +import { useDispatch } from 'react-redux'; |
| 5 | +import SingleLineEditor from 'components/SingleLineEditor'; |
| 6 | +import { updateAuth } from 'providers/ReduxStore/slices/collections'; |
| 7 | +import { saveRequest, sendRequest } from 'providers/ReduxStore/slices/collections/actions'; |
| 8 | +import StyledWrapper from './StyledWrapper'; |
| 9 | +import { inputsConfig } from './inputsConfig'; |
| 10 | +import { clearOauth2Cache } from 'utils/network/index'; |
| 11 | +import toast from 'react-hot-toast'; |
| 12 | + |
| 13 | +const OAuth2Implicit = ({ item, collection }) => { |
| 14 | + const dispatch = useDispatch(); |
| 15 | + const { storedTheme } = useTheme(); |
| 16 | + |
| 17 | + const oAuth = item.draft ? get(item, 'draft.request.auth.oauth2', {}) : get(item, 'request.auth.oauth2', {}); |
| 18 | + |
| 19 | + const handleRun = async () => { |
| 20 | + dispatch(sendRequest(item, collection.uid)); |
| 21 | + }; |
| 22 | + |
| 23 | + const handleSave = () => dispatch(saveRequest(item.uid, collection.uid)); |
| 24 | + |
| 25 | + const { callbackUrl, authorizationUrl, clientId, scope } = oAuth; |
| 26 | + |
| 27 | + const handleChange = (key, value) => { |
| 28 | + dispatch( |
| 29 | + updateAuth({ |
| 30 | + mode: 'oauth2', |
| 31 | + collectionUid: collection.uid, |
| 32 | + itemUid: item.uid, |
| 33 | + content: { |
| 34 | + grantType: 'implicit', |
| 35 | + callbackUrl, |
| 36 | + authorizationUrl, |
| 37 | + clientId, |
| 38 | + scope, |
| 39 | + [key]: value |
| 40 | + } |
| 41 | + }) |
| 42 | + ); |
| 43 | + }; |
| 44 | + |
| 45 | + const handleClearCache = (e) => { |
| 46 | + clearOauth2Cache(collection?.uid) |
| 47 | + .then(() => { |
| 48 | + toast.success('cleared cache successfully'); |
| 49 | + }) |
| 50 | + .catch((err) => { |
| 51 | + toast.error(err.message); |
| 52 | + }); |
| 53 | + }; |
| 54 | + |
| 55 | + return ( |
| 56 | + <StyledWrapper className="mt-2 flex w-full gap-4 flex-col"> |
| 57 | + {inputsConfig.map((input) => { |
| 58 | + const { key, label } = input; |
| 59 | + return ( |
| 60 | + <div className="flex flex-col w-full gap-1" key={`input-${key}`}> |
| 61 | + <label className="block font-medium">{label}</label> |
| 62 | + <div className="single-line-editor-wrapper"> |
| 63 | + <SingleLineEditor |
| 64 | + value={oAuth[key] || ''} |
| 65 | + theme={storedTheme} |
| 66 | + onSave={handleSave} |
| 67 | + onChange={(val) => handleChange(key, val)} |
| 68 | + onRun={handleRun} |
| 69 | + collection={collection} |
| 70 | + /> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + ); |
| 74 | + })} |
| 75 | + <div className="flex flex-row gap-4"> |
| 76 | + <button onClick={handleRun} className="submit btn btn-sm btn-secondary w-fit"> |
| 77 | + Get Access Token |
| 78 | + </button> |
| 79 | + <button onClick={handleClearCache} className="submit btn btn-sm btn-secondary w-fit"> |
| 80 | + Clear Cache |
| 81 | + </button> |
| 82 | + </div> |
| 83 | + </StyledWrapper> |
| 84 | + ); |
| 85 | +}; |
| 86 | + |
| 87 | +export default OAuth2Implicit; |
0 commit comments