diff --git a/src/components/CallbackSamples/CallbackReqSamples.tsx b/src/components/CallbackSamples/CallbackReqSamples.tsx new file mode 100644 index 00000000..5b444e1f --- /dev/null +++ b/src/components/CallbackSamples/CallbackReqSamples.tsx @@ -0,0 +1,31 @@ +import * as React from 'react'; + +import { DropdownProps } from '../../common-elements'; +import { PayloadSamples } from '../PayloadSamples/PayloadSamples'; +import { OperationModel } from '../../services/models'; +import { XPayloadSample } from '../../services/models/Operation'; +import { isPayloadSample } from '../../services'; +import { ReqSamplesWrapper } from './styled.elements'; + +export interface PayloadSamplesProps { + callback: OperationModel; + renderDropdown: (props: DropdownProps) => JSX.Element; +} + +export class CallbackReqSamples extends React.Component { + render() { + const payloadSample = this.props.callback.codeSamples.find(sample => + isPayloadSample(sample), + ) as XPayloadSample | undefined; + + return ( + <> + {payloadSample ? ( + + + + ) : null} + + ); + } +} diff --git a/src/components/CallbackSamples/CallbackSamples.tsx b/src/components/CallbackSamples/CallbackSamples.tsx index d7b0e4f1..5bebad4a 100644 --- a/src/components/CallbackSamples/CallbackSamples.tsx +++ b/src/components/CallbackSamples/CallbackSamples.tsx @@ -1,12 +1,15 @@ import { observer } from 'mobx-react'; import * as React from 'react'; -import { RightPanelHeader, Tab, TabList, TabPanel, Tabs } from '../../common-elements'; -import { isPayloadSample, RedocNormalizedOptions } from '../../services'; +import { RightPanelHeader } from '../../common-elements'; +import { RedocNormalizedOptions } from '../../services'; import { CallbackModel } from '../../services/models'; import { OptionsContext } from '../OptionsProvider'; -import { PayloadSamples } from '../PayloadSamples/PayloadSamples'; -import { SourceCodeWithCopy } from '../SourceCode/SourceCode'; +import { CallbacksSwitch } from '../CallbacksSwitch/CallbacksSwitch'; +import { DropdownOrLabel } from '../DropdownOrLabel/DropdownOrLabel'; +import { InvertedSimpleDropdown, MimeLabel } from '../PayloadSamples/styled.elements'; +import { CallbackReqSamples } from './CallbackReqSamples'; +import { SamplesWrapper } from './styled.elements'; export interface CallbackSamplesProps { callbacks: CallbackModel[]; @@ -17,66 +20,45 @@ export class CallbackSamples extends React.Component { static contextType = OptionsContext; context: RedocNormalizedOptions; + private renderDropdown = props => { + return ; + }; + render() { const { callbacks } = this.props; + const operations = callbacks + .map(callback => callback.operations.map(operation => operation)) + .reduce((a, b) => a.concat(b), []); + // Sums number of code samples per operation per callback - const numSamples = callbacks.reduce( - (callbackSum, callback) => - callbackSum + - callback.operations.reduce( - (sampleSum, operation) => sampleSum + operation.codeSamples.length, - 0, - ), + const numSamples = operations.reduce( + (sampleSum, operation) => sampleSum + operation.codeSamples.length, 0, ); const hasSamples = numSamples > 0; - const hideTabList = numSamples === 1 ? this.context.hideSingleRequestSampleTab : false; - - const renderTabs = () => { - return callbacks.map(callback => { - return callback.operations.map(operation => { - return operation.codeSamples.map(sample => { - return ( - - {operation.name} {sample.label !== undefined ? sample.label : sample.lang} - - ); - }); - }); - }); - }; - - const renderTabPanels = () => { - return callbacks.map(callback => { - return callback.operations.map(operation => { - return operation.codeSamples.map(sample => { - return ( - - {isPayloadSample(sample) ? ( -
- -
- ) : ( - - )} -
- ); - }); - }); - }); - }; return ( (hasSamples && (
- Callback samples + Callback request samples - - - {renderTabPanels()} - + + + {callback => ( + + )} + +
)) || null diff --git a/src/components/CallbackSamples/styled.elements.ts b/src/components/CallbackSamples/styled.elements.ts new file mode 100644 index 00000000..13eec1bf --- /dev/null +++ b/src/components/CallbackSamples/styled.elements.ts @@ -0,0 +1,10 @@ +import styled from '../../styled-components'; + +export const SamplesWrapper = styled.div` + background: ${({ theme }) => theme.codeSample.backgroundColor}; + padding: ${props => props.theme.spacing.unit * 4}px; +`; + +export const ReqSamplesWrapper = styled.div` + margin-top: 15px; +`; diff --git a/src/components/Callbacks/CallbacksList.tsx b/src/components/Callbacks/CallbacksList.tsx index 82597898..ce68c652 100644 --- a/src/components/Callbacks/CallbacksList.tsx +++ b/src/components/Callbacks/CallbacksList.tsx @@ -28,8 +28,8 @@ export class CallbacksList extends React.PureComponent {
Callbacks {callbacks.map(callback => { - return callback.operations.map(operation => { - return ; + return callback.operations.map((operation, index) => { + return ; }); })}
diff --git a/src/components/CallbacksSwitch/CallbacksSwitch.tsx b/src/components/CallbacksSwitch/CallbacksSwitch.tsx new file mode 100644 index 00000000..2b0a2b23 --- /dev/null +++ b/src/components/CallbacksSwitch/CallbacksSwitch.tsx @@ -0,0 +1,75 @@ +import { observer } from 'mobx-react'; +import * as React from 'react'; + +import { DropdownProps } from '../../common-elements/dropdown'; +import { DropdownLabel, DropdownWrapper } from '../PayloadSamples/styled.elements'; +import { OperationModel } from '../../services/models'; + +export interface CallbacksSwitchProps { + callbacks?: OperationModel[]; + withLabel?: boolean; + + renderDropdown: (props: DropdownProps) => JSX.Element; + children: (activeCallback: OperationModel) => JSX.Element; +} + +export interface CallbacksSwitchState { + activeItemIdx: number; +} + +@observer +export class CallbacksSwitch extends React.Component { + constructor(props) { + super(props); + this.state = { + activeItemIdx: 0, + }; + } + + switchCallback = ({ value }) => { + if (this.props.callbacks) { + this.setState({ + activeItemIdx: parseInt(value, 10), + }); + } + }; + + render() { + const { callbacks } = this.props; + + if (!callbacks || !callbacks.length) { + return null; + } + + const options = callbacks.map((callback, idx) => { + return { + label: `[${callback.httpVerb.toUpperCase()}] ${callback.name}`, + value: idx.toString(), + }; + }); + + const Wrapper = ({ children }) => + this.props.withLabel ? ( + + Callback + {children} + + ) : ( + children + ); + + return ( + <> + + {this.props.renderDropdown({ + value: options[this.state.activeItemIdx], + options, + onChange: this.switchCallback, + })} + + + {this.props.children(callbacks[this.state.activeItemIdx])} + + ); + } +} diff --git a/src/services/models/Operation.ts b/src/services/models/Operation.ts index 86f47a09..b5cf136c 100644 --- a/src/services/models/Operation.ts +++ b/src/services/models/Operation.ts @@ -32,7 +32,7 @@ import { MediaContentModel } from './MediaContent'; import { RequestBodyModel } from './RequestBody'; import { ResponseModel } from './Response'; -interface XPayloadSample { +export interface XPayloadSample { lang: 'payload'; label: string; requestBodyContent: MediaContentModel;