| 1 |
import { useState, useCallback } from 'react' |
| 2 |
import { getSessionToken, settings, Client } from './lib/allauth' |
| 3 |
|
| 4 |
function APICard (props) { |
| 5 |
return ( |
| 6 |
<div className='card'> |
| 7 |
<div className='card-body'> |
| 8 |
<h5 className='card-title'>{props.title}</h5> |
| 9 |
<a href={props.docs}>API documentation</a> |
| 10 |
<div className='mb-3'> |
| 11 |
<label>Status</label> |
| 12 |
<input className='form-control' value={props.response.status} readOnly /> |
| 13 |
</div> |
| 14 |
<pre className='overflow-x-scroll'>{JSON.stringify(props.response.data, undefined, 4)}</pre> |
| 15 |
</div> |
| 16 |
</div> |
| 17 |
) |
| 18 |
} |
| 19 |
|
| 20 |
export default function Calculator () { |
| 21 |
const [x, setX] = useState('') |
| 22 |
const [y, setY] = useState('') |
| 23 |
const [drfResponse, setDRFResponse] = useState({ status: '', data: '' }) |
| 24 |
const [ninjaResponse, setNinjaResponse] = useState({ status: '', data: '' }) |
| 25 |
|
| 26 |
const fetchResult = useCallback(async (url, x, y, setResponse) => { |
| 27 |
const params = { x, y } |
| 28 |
const query = new URLSearchParams(params) |
| 29 |
const sessionToken = getSessionToken() |
| 30 |
const options = { headers: {} } |
| 31 |
if (settings.client === Client.APP && sessionToken) { |
| 32 |
options.headers['X-Session-Token'] = sessionToken |
| 33 |
} |
| 34 |
if (settings.withCredentials) { |
| 35 |
options.credentials = 'include' |
| 36 |
} |
| 37 |
const response = await window.fetch(`${url}?${query.toString()}`, options) |
| 38 |
|
| 39 |
const data = await response.json() |
| 40 |
setResponse({ status: response.status, data }) |
| 41 |
}, []) |
| 42 |
|
| 43 |
const onCalculate = useCallback((e) => { |
| 44 |
e.preventDefault() |
| 45 |
let baseUrl = '' |
| 46 |
try { |
| 47 |
baseUrl = new URL(settings.baseUrl).origin |
| 48 |
} catch {} |
| 49 |
fetchResult(baseUrl + '/drf/api/add/', x, y, setDRFResponse) |
| 50 |
fetchResult(baseUrl + '/ninja/api/add', x, y, setNinjaResponse) |
| 51 |
return false |
| 52 |
}, [x, y, setNinjaResponse, setDRFResponse]) |
| 53 |
|
| 54 |
return ( |
| 55 |
<div> |
| 56 |
<h2>Calculator</h2> |
| 57 |
<form> |
| 58 |
<fieldset> |
| 59 |
<div className='mb-3'> |
| 60 |
<label>𝓍</label> |
| 61 |
<input className='form-control' value={x} onChange={(e) => setX(e.target.value)} type='number' /> |
| 62 |
</div> |
| 63 |
<div className='mb-3'> |
| 64 |
<label>𝓎</label> |
| 65 |
<input className='form-control' value={y} onChange={(e) => setY(e.target.value)} type='number' /> |
| 66 |
</div> |
| 67 |
<button onClick={onCalculate} className='btn btn-primary'>Add these inputs</button> |
| 68 |
</fieldset> |
| 69 |
|
| 70 |
<div className='row'> |
| 71 |
<div className='col-6'> |
| 72 |
<APICard title='Ninja' docs='/ninja/api/docs' response={ninjaResponse} /> |
| 73 |
</div> |
| 74 |
<div className='col-6'> |
| 75 |
<APICard title='Django REST Framework' docs='/drf/api/schema/redoc/' response={drfResponse} /> |
| 76 |
</div> |
| 77 |
</div> |
| 78 |
</form> |
| 79 |
</div> |
| 80 |
) |
| 81 |
} |