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

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2022-03-19 17:16:30 +01:00
import React from 'react';
2022-03-20 09:32:33 +01:00
import get from 'lodash/get';
import CodeEditor from 'components/CodeEditor';
import { useDispatch } from 'react-redux';
2022-03-22 13:48:20 +01:00
import { updateRequestBody, sendRequest, saveRequest } from 'providers/ReduxStore/slices/collections';
2022-03-19 17:16:30 +01:00
import StyledWrapper from './StyledWrapper';
2022-03-20 09:32:33 +01:00
const RequestBody = ({item, collection}) => {
const dispatch = useDispatch();
const body = item.draft ? get(item, 'draft.request.body') : get(item, 'request.body');
const bodyMode = item.draft ? get(item, 'draft.request.body.mode') : get(item, 'request.body.mode');
2022-03-20 09:32:33 +01:00
const onEdit = (value) => {
dispatch(updateRequestBody({
content: value,
itemUid: item.uid,
collectionUid: collection.uid,
}));
};
2022-03-22 13:48:20 +01:00
const onRun = () => dispatch(sendRequest(item, collection.uid));;
const onSave = () => dispatch(saveRequest(item.uid, collection.uid));
if(['json', 'xml', 'text'].includes(bodyMode)) {
let codeMirrorMode = {
json: 'application/ld+json',
text: 'application/text',
xml: 'application/xml'
};
let bodyContent = {
json: body.json,
text: body.text,
xml: body.xml
};
return(
<StyledWrapper className="w-full">
<CodeEditor
value={bodyContent[bodyMode] || ''}
onEdit={onEdit}
onRun={onRun}
onSave={onSave}
mode={codeMirrorMode[bodyMode]}
/>
</StyledWrapper>
);
}
2022-03-22 13:48:20 +01:00
2022-03-19 17:16:30 +01:00
return(
2022-03-22 13:48:20 +01:00
<StyledWrapper className="w-full">
No Body
2022-03-19 17:16:30 +01:00
</StyledWrapper>
);
};
export default RequestBody;