|
| 1 | +import React from "react"; |
| 2 | + |
| 3 | +import FormItem from "@theme/ApiDemoPanel/FormItem"; |
| 4 | +import FormSelect from "@theme/ApiDemoPanel/FormSelect"; |
| 5 | +import FormTextInput from "@theme/ApiDemoPanel/FormTextInput"; |
| 6 | +import { useTypedDispatch, useTypedSelector } from "@theme/ApiItem/hooks"; |
| 7 | + |
| 8 | +import sharedStyles from "../shared.module.css"; |
| 9 | +import { |
| 10 | + resolveAuthFieldMeta, |
| 11 | + resolveSchemeOptionLabel, |
| 12 | +} from "../utils"; |
| 13 | +import { |
| 14 | + setAuthData, |
| 15 | + setSelectedAuth, |
| 16 | +} from "@theme/ApiDemoPanel/Authorization/slice"; |
| 17 | + |
| 18 | +function Authorization() { |
| 19 | + const data = useTypedSelector((state: any) => state.auth.data); |
| 20 | + const options = useTypedSelector((state: any) => state.auth.options); |
| 21 | + const selected = useTypedSelector((state: any) => state.auth.selected); |
| 22 | + const dispatch = useTypedDispatch(); |
| 23 | + |
| 24 | + if (selected === undefined) { |
| 25 | + return null; |
| 26 | + } |
| 27 | + |
| 28 | + const selectedAuth = options[selected] || []; |
| 29 | + const optionEntries = Object.entries(options); |
| 30 | + const handleFieldChange = |
| 31 | + (scheme: string, key: string) => |
| 32 | + (event: React.ChangeEvent<HTMLInputElement>) => { |
| 33 | + const nextValue = event.target.value; |
| 34 | + dispatch( |
| 35 | + setAuthData({ |
| 36 | + scheme, |
| 37 | + key, |
| 38 | + value: nextValue ? nextValue : undefined, |
| 39 | + }) |
| 40 | + ); |
| 41 | + }; |
| 42 | + |
| 43 | + if (selectedAuth.length === 0) { |
| 44 | + return null; |
| 45 | + } |
| 46 | + |
| 47 | + return ( |
| 48 | + <div className={sharedStyles.section}> |
| 49 | + <h5 className={sharedStyles.sectionTitle}>Authorization</h5> |
| 50 | + <p className={sharedStyles.sectionDescription}> |
| 51 | + Provide any credentials required for the live request. Header names stay |
| 52 | + visible as helper text. |
| 53 | + </p> |
| 54 | + |
| 55 | + {optionEntries.length > 1 && ( |
| 56 | + <FormItem |
| 57 | + description="Choose which credential set should be attached to this request." |
| 58 | + label="Security Scheme" |
| 59 | + > |
| 60 | + <FormSelect |
| 61 | + onChange={(event: React.ChangeEvent<HTMLSelectElement>) => |
| 62 | + dispatch(setSelectedAuth(event.target.value)) |
| 63 | + } |
| 64 | + options={optionEntries.map(([key, schemes]) => ({ |
| 65 | + label: resolveSchemeOptionLabel(key, schemes as any[]), |
| 66 | + value: key, |
| 67 | + }))} |
| 68 | + value={selected} |
| 69 | + /> |
| 70 | + </FormItem> |
| 71 | + )} |
| 72 | + |
| 73 | + {selectedAuth.flatMap((scheme: any) => |
| 74 | + resolveAuthFieldMeta(scheme).map((field) => ( |
| 75 | + <FormItem |
| 76 | + description={field.description} |
| 77 | + helperText={field.helperText} |
| 78 | + key={`${scheme.key}-${field.dataKey}`} |
| 79 | + label={field.label} |
| 80 | + > |
| 81 | + <FormTextInput |
| 82 | + onChange={handleFieldChange(scheme.key, field.dataKey)} |
| 83 | + password={field.password} |
| 84 | + placeholder={field.placeholder} |
| 85 | + value={data[scheme.key]?.[field.dataKey] ?? ""} |
| 86 | + /> |
| 87 | + </FormItem> |
| 88 | + )) |
| 89 | + )} |
| 90 | + </div> |
| 91 | + ); |
| 92 | +} |
| 93 | + |
| 94 | +export default Authorization; |
0 commit comments