bruno/renderer/components/RequestPane/RequestHeaders/index.js

120 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-03-19 14:03:16 +01:00
import React from 'react';
import get from 'lodash/get';
import cloneDeep from 'lodash/cloneDeep';
import { IconTrash } from '@tabler/icons';
2022-03-19 14:03:16 +01:00
import { useDispatch } from 'react-redux';
import { requestChanged } from 'providers/ReduxStore/slices/tabs';
import { addRequestHeader, updateRequestHeader } from 'providers/ReduxStore/slices/collections';
2021-12-31 17:22:22 +01:00
import StyledWrapper from './StyledWrapper';
2022-03-19 14:03:16 +01:00
const RequestHeaders = ({item, collection}) => {
const dispatch = useDispatch();
const headers = item.draft ? get(item, 'draft.request.headers') : get(item, 'request.headers');
2021-12-31 17:22:22 +01:00
const addHeader = () => {
2022-03-19 14:03:16 +01:00
dispatch(requestChanged({
itemUid: item.uid,
collectionUid: collection.uid
}));
dispatch(addRequestHeader({
itemUid: item.uid,
collectionUid: collection.uid,
}));
2021-12-31 17:22:22 +01:00
};
2022-03-19 14:03:16 +01:00
const handleHeaderValueChange = (e, _header, type) => {
const header = cloneDeep(_header);
switch(type) {
case 'name' : {
header.name = e.target.value;
break;
}
case 'value' : {
header.value = e.target.value;
break;
}
case 'description' : {
header.description = e.target.value;
break;
}
}
dispatch(requestChanged({
itemUid: item.uid,
collectionUid: collection.uid
}));
dispatch(updateRequestHeader({
header: header,
itemUid: item.uid,
collectionUid: collection.uid,
}));
2021-12-31 17:22:22 +01:00
};
const handleRemoveHeader = (index) => {
};
return (
<StyledWrapper className="mt-4">
<table>
<thead>
<tr>
<td>Key</td>
<td>Value</td>
<td>Description</td>
2021-12-31 17:22:22 +01:00
<td></td>
</tr>
</thead>
<tbody>
2021-12-31 18:55:53 +01:00
{headers && headers.length ? headers.map((header, index) => {
2021-12-31 17:22:22 +01:00
return (
2021-12-31 17:58:40 +01:00
<tr key={header.uid}>
2021-12-31 17:22:22 +01:00
<td>
<input
type="text"
2022-01-23 12:44:46 +01:00
autoComplete="off"
2022-03-19 14:03:16 +01:00
value={header.name}
className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'name')}
2021-12-31 17:22:22 +01:00
/>
</td>
<td>
<input
type="text"
2022-01-23 12:44:46 +01:00
autoComplete="off"
2022-03-19 14:03:16 +01:00
value={header.value}
className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'value')}
2021-12-31 17:22:22 +01:00
/>
</td>
<td>
<input
type="text"
2022-01-23 12:44:46 +01:00
autoComplete="off"
2022-03-19 14:03:16 +01:00
value={header.description}
className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'description')}
2021-12-31 17:22:22 +01:00
/>
</td>
<td>
<div className="flex items-center">
<input
type="checkbox"
className="mr-3"
2022-03-19 14:03:16 +01:00
checked={header.enabled}
className="mousetrap"
onChange={(e) => handleHeaderValueChange(e, header, 'enabled')}
/>
<button onClick={() => handleRemoveHeader(index)}>
<IconTrash strokeWidth={1.5} size={20}/>
</button>
</div>
2021-12-31 17:22:22 +01:00
</td>
</tr>
2021-12-31 17:58:40 +01:00
);
2021-12-31 18:55:53 +01:00
}) : null}
2021-12-31 17:22:22 +01:00
</tbody>
</table>
<button className="btn-add-header" onClick={addHeader}>+ Add Header</button>
</StyledWrapper>
)
};
export default RequestHeaders;