-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update server url based on variable selection
- Loading branch information
1 parent
d056253
commit be799b1
Showing
10 changed files
with
172 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { OpenAPIClientContext } from './types'; | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
import { useApiClientModal } from '@scalar/api-client-react'; | ||
|
||
export function ServerURLForm(props: { | ||
children: React.ReactNode; | ||
context: OpenAPIClientContext; | ||
server: OpenAPIV3.ServerObject; | ||
}) { | ||
const { children, context, server } = props; | ||
const router = useRouter(); | ||
const client = useApiClientModal(); | ||
const [isPending, startTransition] = React.useTransition(); | ||
|
||
function updateServerUrl(formData: FormData) { | ||
startTransition(() => { | ||
if (!server.variables) { | ||
return; | ||
} | ||
let params = new URLSearchParams(`block=${context.blockKey}`); | ||
const variableKeys = Object.keys(server.variables); | ||
for (const pair of formData.entries()) { | ||
if (variableKeys.includes(pair[0]) && !isNaN(Number(pair[1]))) { | ||
params.set(pair[0], `${pair[1]}`); | ||
} | ||
} | ||
router.push(`?${params}`, { scroll: false }); | ||
}) | ||
} | ||
|
||
return ( | ||
<form action={updateServerUrl} className="contents"> | ||
<fieldset disabled={isPending} className="contents"> | ||
<input type="hidden" name="block" value={context.blockKey} /> | ||
<span>{children}</span> | ||
</fieldset> | ||
</form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,63 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import classNames from 'classnames'; | ||
import { OpenAPIV3 } from 'openapi-types'; | ||
import { OpenAPIClientContext } from './types'; | ||
|
||
/** | ||
* Interactive component to show the value of a server variable and let the user change it. | ||
*/ | ||
export function OpenAPIServerURLVariable(props: { | ||
name: string; | ||
variable: OpenAPIV3.ServerVariableObject; | ||
enumIndex?: number; | ||
}) { | ||
const { variable } = props; | ||
const { enumIndex, name, variable } = props; | ||
|
||
if (variable.enum && variable.enum.length > 0) { | ||
return (<select | ||
className={classNames( | ||
'openapi-section-select', | ||
'openapi-select', | ||
)} | ||
value={variable.default} | ||
> | ||
{ | ||
variable.enum?.map((value: string) => { | ||
return ( | ||
<option key={value} value={value}> | ||
{value} | ||
</option> | ||
); | ||
}) ?? null} | ||
</select>); | ||
|
||
return ( | ||
<EnumSelect | ||
name={name} | ||
variable={variable} | ||
value={ | ||
!isNaN(Number(enumIndex)) | ||
? enumIndex | ||
: variable.enum.findIndex((v) => v === variable.default) | ||
} | ||
/> | ||
); | ||
} | ||
|
||
return <span className={classNames('openapi-url-var')}>{variable.default}</span>; | ||
} | ||
|
||
/** | ||
* Render a select if there is an enum for a Server URL variable | ||
*/ | ||
function EnumSelect(props: { | ||
value?: number; | ||
name: string; | ||
variable: OpenAPIV3.ServerVariableObject; | ||
}) { | ||
const { value, name, variable } = props; | ||
return ( | ||
<select | ||
name={name} | ||
onChange={(e) => { | ||
e.preventDefault(); | ||
e.currentTarget.form?.requestSubmit(); | ||
}} | ||
className={classNames('openapi-select')} | ||
value={value} | ||
> | ||
{variable.enum?.map((value: string, index: number) => { | ||
return ( | ||
<option key={value} value={index}> | ||
{value} | ||
</option> | ||
); | ||
}) ?? null} | ||
</select> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters