From b2d8ef73126eff6c436464ba99a93043802323cf Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Wed, 11 May 2022 11:31:49 +0300 Subject: [PATCH] feat: add option for display request method in webhook --- src/components/Operation/Operation.tsx | 89 ++++++++++--------- src/components/SideMenu/MenuItem.tsx | 56 ++++++------ .../DiscriminatorDropdown.test.tsx.snap | 10 +++ src/services/RedocNormalizedOptions.ts | 3 + 4 files changed, 85 insertions(+), 73 deletions(-) diff --git a/src/components/Operation/Operation.tsx b/src/components/Operation/Operation.tsx index 8170351e..24d12f43 100644 --- a/src/components/Operation/Operation.tsx +++ b/src/components/Operation/Operation.tsx @@ -18,6 +18,7 @@ import { ResponsesList } from '../Responses/ResponsesList'; import { ResponseSamples } from '../ResponseSamples/ResponseSamples'; import { SecurityRequirements } from '../SecurityRequirement/SecurityRequirement'; import { SECTION_ATTR } from '../../services'; +import { titleize } from '../../utils/helpers'; const Description = styled.div` margin-bottom: ${({ theme }) => theme.spacing.unit * 6}px; @@ -27,48 +28,48 @@ export interface OperationProps { operation: OperationModel; } -@observer -export class Operation extends React.Component { - render() { - const { operation } = this.props; - - const { name: summary, description, deprecated, externalDocs, isWebhook } = operation; - const hasDescription = !!(description || externalDocs); - - return ( - - {options => ( - - -

- - {summary} {deprecated && Deprecated } - {isWebhook && Webhook } -

- {options.pathInMiddlePanel && !isWebhook && ( - +export const Operation = observer(({ operation }: OperationProps): JSX.Element => { + const { name: summary, description, deprecated, externalDocs, isWebhook, httpVerb } = operation; + const hasDescription = !!(description || externalDocs); + const { showWebhookVerb } = React.useContext(OptionsContext); + return ( + + {options => ( + + +

+ + {summary} {deprecated && Deprecated } + {isWebhook && ( + + {' '} + Webhook {showWebhookVerb && '| ' + titleize(httpVerb)} + )} - {hasDescription && ( - - {description !== undefined && } - {externalDocs && } - - )} - - - - - - - - {!options.pathInMiddlePanel && !isWebhook && } - - - - - - )} - - ); - } -} +

+ {options.pathInMiddlePanel && !isWebhook && ( + + )} + {hasDescription && ( + + {description !== undefined && } + {externalDocs && } + + )} + + + + + +
+ + {!options.pathInMiddlePanel && !isWebhook && } + + + + +
+ )} +
+ ); +}); diff --git a/src/components/SideMenu/MenuItem.tsx b/src/components/SideMenu/MenuItem.tsx index 138e88c9..5e79943b 100644 --- a/src/components/SideMenu/MenuItem.tsx +++ b/src/components/SideMenu/MenuItem.tsx @@ -8,11 +8,13 @@ import { MenuItems } from './MenuItems'; import { MenuItemLabel, MenuItemLi, MenuItemTitle, OperationBadge } from './styled.elements'; import { l } from '../../services/Labels'; import { scrollIntoViewIfNeeded } from '../../utils'; +import { OptionsContext } from '../OptionsProvider'; export interface MenuItemProps { item: IMenuItem; onActivate?: (item: IMenuItem) => void; withoutChildren?: boolean; + children?: React.ReactChild; } @observer @@ -70,37 +72,33 @@ export class MenuItem extends React.Component { export interface OperationMenuItemContentProps { item: OperationModel; + children?: React.ReactChild; } -@observer -export class OperationMenuItemContent extends React.Component { - ref = React.createRef(); +export const OperationMenuItemContent = observer((props: OperationMenuItemContentProps) => { + const { item } = props; + const ref = React.createRef(); + const { showWebhookVerb } = React.useContext(OptionsContext); - componentDidUpdate() { - if (this.props.item.active && this.ref.current) { - scrollIntoViewIfNeeded(this.ref.current); + React.useEffect(() => { + if (props.item.active && ref.current) { + scrollIntoViewIfNeeded(ref.current); } - } + }, [props.item.active, ref]); - render() { - const { item } = this.props; - return ( - - {item.isWebhook ? ( - {l('webhook')} - ) : ( - {shortenHTTPVerb(item.httpVerb)} - )} - - {item.sidebarLabel} - {this.props.children} - - - ); - } -} + return ( + + {item.isWebhook ? ( + + {showWebhookVerb ? item.httpVerb : l('webhook')} + + ) : ( + {shortenHTTPVerb(item.httpVerb)} + )} + + {item.sidebarLabel} + {props.children} + + + ); +}); diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index 4b0ada97..81245601 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -106,6 +106,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -360,6 +361,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -589,6 +591,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -885,6 +888,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -1139,6 +1143,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -1368,6 +1373,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -1620,6 +1626,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -1913,6 +1920,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -2167,6 +2175,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, @@ -2396,6 +2405,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView "scrollYOffset": [Function], "showExtensions": false, "showObjectSchemaExamples": false, + "showWebhookVerb": false, "sideNavStyle": "summary-only", "simpleOneOfTypeLabel": false, "sortEnumValuesAlphabetically": false, diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index 0628affd..92bec6b6 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -59,6 +59,7 @@ export interface RedocRawOptions { nonce?: string; hideFab?: boolean; minCharacterLengthToInitSearch?: number; + showWebhookVerb?: boolean; } export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean { @@ -257,6 +258,7 @@ export class RedocNormalizedOptions { generatedPayloadSamplesMaxDepth: number; hideFab: boolean; minCharacterLengthToInitSearch: number; + showWebhookVerb: boolean; nonce?: string; @@ -334,5 +336,6 @@ export class RedocNormalizedOptions { this.nonce = raw.nonce; this.hideFab = argValueToBoolean(raw.hideFab); this.minCharacterLengthToInitSearch = argValueToNumber(raw.minCharacterLengthToInitSearch) || 3; + this.showWebhookVerb = argValueToBoolean(raw.showWebhookVerb); } }