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

119 lines
3.7 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';
2022-03-19 14:43:16 +01:00
import { addRequestHeader, updateRequestHeader, deleteRequestHeader } 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(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;
}
2022-03-22 13:48:20 +01:00
case 'enabled' : {
header.enabled = e.target.checked;
break;
}
2022-03-19 14:03:16 +01:00
}
dispatch(updateRequestHeader({
header: header,
itemUid: item.uid,
2022-03-19 14:43:16 +01:00
collectionUid: collection.uid
2022-03-19 14:03:16 +01:00
}));
2021-12-31 17:22:22 +01:00
};
2022-03-19 14:43:16 +01:00
const handleRemoveHeader = (header) => {
dispatch(deleteRequestHeader({
headerUid: header.uid,
itemUid: item.uid,
collectionUid: collection.uid
}));
2021-12-31 17:22:22 +01:00
};
return (
2022-03-22 13:48:20 +01:00
<StyledWrapper className="w-full">
2021-12-31 17:22:22 +01:00
<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-03-22 13:48:20 +01:00
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false"
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-03-22 13:48:20 +01:00
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false"
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-03-22 13:48:20 +01:00
autoComplete="off" autoCorrect="off" autoCapitalize="off" spellCheck="false"
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"
2022-03-19 14:03:16 +01:00
checked={header.enabled}
2022-03-19 14:43:16 +01:00
className="mr-3 mousetrap"
2022-03-19 14:03:16 +01:00
onChange={(e) => handleHeaderValueChange(e, header, 'enabled')}
/>
2022-03-19 14:43:16 +01:00
<button onClick={() => handleRemoveHeader(header)}>
<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>
2022-03-22 13:48:20 +01:00
<button className="btn-add-header select-none" onClick={addHeader}>+ Add Header</button>
2021-12-31 17:22:22 +01:00
</StyledWrapper>
)
};
export default RequestHeaders;