import React, { useState, useMemo, useCallback } from 'react'; import { useDispatch } from 'react-redux'; import { useTheme } from 'providers/Theme'; import get from 'lodash/get'; import { moveResponseExampleParam, setResponseExampleParams } from 'providers/ReduxStore/slices/collections'; import EditableTable from 'components/EditableTable'; import SingleLineEditor from 'components/SingleLineEditor'; import BulkEditor from 'components/BulkEditor'; import InfoTip from 'components/InfoTip'; import StyledWrapper from './StyledWrapper'; const ResponseExampleParams = ({ editMode, item, collection, exampleUid }) => { const dispatch = useDispatch(); const { storedTheme } = useTheme(); const [isBulkEditMode, setIsBulkEditMode] = useState(false); const params = useMemo(() => { return item.draft ? get(item, 'draft.examples', []).find((e) => e.uid === exampleUid)?.request?.params || [] : get(item, 'examples', []).find((e) => e.uid === exampleUid)?.request?.params || []; }, [item, exampleUid]); const queryParams = params.filter((param) => param.type === 'query'); const pathParams = params.filter((param) => param.type === 'path'); const handleQueryParamsChange = useCallback((updatedQueryParams) => { if (!editMode) { return; } // Merge updated query params with path params const allParams = [...updatedQueryParams, ...pathParams]; dispatch(setResponseExampleParams({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, params: allParams })); }, [editMode, dispatch, item.uid, collection.uid, exampleUid, pathParams]); const handleQueryParamDrag = useCallback(({ updateReorderedItem }) => { if (!editMode) { return; } dispatch(moveResponseExampleParam({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, updateReorderedItem })); }, [editMode, dispatch, item.uid, collection.uid, exampleUid]); const handlePathParamsChange = useCallback((updatedPathParams) => { if (!editMode) { return; } // Merge updated path params with query params const allParams = [...queryParams, ...updatedPathParams]; dispatch(setResponseExampleParams({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, params: allParams })); }, [editMode, dispatch, item.uid, collection.uid, exampleUid, queryParams]); const toggleBulkEditMode = () => { setIsBulkEditMode(!isBulkEditMode); }; const handleBulkParamsChange = (newParams) => { if (!editMode) { return; } // Merge bulk edited query params with path params const allParams = [...newParams, ...pathParams]; dispatch(setResponseExampleParams({ itemUid: item.uid, collectionUid: collection.uid, exampleUid: exampleUid, params: allParams })); }; if (isBulkEditMode && editMode) { return ( ); } const queryColumns = [ { key: 'name', name: 'Name', isKeyField: true, placeholder: 'Name', width: '40%', readOnly: !editMode, render: ({ row, value, onChange, isLastEmptyRow }) => ( {}} onChange={onChange} onRun={() => {}} collection={collection} variablesAutocomplete={true} readOnly={!editMode} placeholder={isLastEmptyRow ? 'Name' : ''} /> ) }, { key: 'value', name: 'Value', placeholder: 'Value', width: '60%', readOnly: !editMode, render: ({ row, value, onChange, isLastEmptyRow }) => ( {}} onChange={onChange} onRun={() => {}} collection={collection} variablesAutocomplete={true} readOnly={!editMode} placeholder={isLastEmptyRow ? 'Value' : ''} /> ) } ]; const pathColumns = [ { key: 'name', name: 'Name', readOnly: true, width: '40%' }, { key: 'value', name: 'Value', placeholder: 'Value', width: '60%', readOnly: !editMode, render: ({ row, value, onChange, isLastEmptyRow }) => ( {}} onChange={onChange} onRun={() => {}} collection={collection} variablesAutocomplete={true} readOnly={!editMode} placeholder={isLastEmptyRow ? 'Value' : ''} /> ) } ]; const defaultQueryRow = { name: '', value: '', enabled: true, type: 'query' }; if (queryParams.length === 0 && pathParams.length === 0 && !editMode) { return null; } return (
Query parameters
{editMode && (
)} {pathParams && pathParams.length > 0 && ( <>
Path parameters
Path variables are automatically added whenever the :name template is used in the URL.
For example: https://example.com/v1/users/:id
)}
); }; export default ResponseExampleParams;