Skip to content

Commit

Permalink
refactor(oas31): change Contact component to be smart (#8478)
Browse files Browse the repository at this point in the history
Refs #8474
  • Loading branch information
char0n authored Mar 17, 2023
1 parent 91ba415 commit a3d3f26
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
43 changes: 43 additions & 0 deletions src/core/plugins/oas31/components/contact.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @prettier
*/
import React from "react"
import PropTypes from "prop-types"

import { sanitizeUrl } from "core/utils"

const Contact = ({ getComponent, specSelectors }) => {
const name = specSelectors.selectContactNameField()
const url = specSelectors.selectContactUrl()
const email = specSelectors.selectContactEmailField()

const Link = getComponent("Link")

return (
<div className="info__contact">
{url && (
<div>
<Link href={sanitizeUrl(url)} target="_blank">
{name} - Website
</Link>
</div>
)}
{email && (
<Link href={sanitizeUrl(`mailto:${email}`)}>
{url ? `Send email to ${name}` : `Contact ${name}`}
</Link>
)}
</div>
)
}

Contact.propTypes = {
getComponent: PropTypes.func.isRequired,
specSelectors: PropTypes.shape({
selectContactNameField: PropTypes.func.isRequired,
selectContactUrl: PropTypes.func.isRequired,
selectContactEmailField: PropTypes.func.isRequired,
}).isRequired,
}

export default Contact
19 changes: 17 additions & 2 deletions src/core/plugins/oas31/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@
*/
import Webhooks from "./components/webhooks"
import License from "./components/license"
import Contact from "./components/contact"
import Info from "./components/info"
import LicenseWrapper from "./wrap-components/license"
import ContactWrapper from "./wrap-components/contact"
import InfoWrapper from "./wrap-components/info"
import {
license,
contact,
webhooks,
selectLicenseNameField,
selectLicenseUrlField,
selectLicenseIdentifierField,
selectContactNameField,
selectContactEmailField,
selectContactUrlField,
makeSelectContactUrl,
makeIsOAS31,
makeSelectLicenseUrl,
} from "./spec-extensions/selectors"
Expand All @@ -27,18 +34,22 @@ const OAS31Plugin = () => {
const oas31Selectors = this.statePlugins.oas31.selectors
const specSelectors = this.statePlugins.spec.selectors

specSelectors.selectLicenseUrl = makeSelectLicenseUrl(system)
specSelectors.isOAS31 = makeIsOAS31(system)
specSelectors.selectLicenseUrl = makeSelectLicenseUrl(system)
specSelectors.selectContactUrl = makeSelectContactUrl(system)

oas31Selectors.selectLicenseUrl = makeOAS31SelectLicenseUrl(system)
},
components: {
Webhooks,
OAS31Info: Info,
OAS31License: License,
OAS31Contact: Contact,
},
wrapComponents: {
License: LicenseWrapper,
info: InfoWrapper,
License: LicenseWrapper,
Contact: ContactWrapper,
},
statePlugins: {
spec: {
Expand All @@ -47,6 +58,10 @@ const OAS31Plugin = () => {
selectLicenseNameField,
selectLicenseUrlField,
selectLicenseIdentifierField,
contact,
selectContactNameField,
selectContactEmailField,
selectContactUrlField,
webhooks,
},
wrapSelectors: {
Expand Down
34 changes: 32 additions & 2 deletions src/core/plugins/oas31/spec-extensions/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export const license = () => (system) => {
return system.specSelectors.info().get("license", map)
}

export const contact = () => (system) => {
return system.specSelectors.info().get("contact", map)
}

export const selectLicenseNameField = () => (system) => {
return system.specSelectors.license().get("name", "License")
}
Expand All @@ -28,15 +32,41 @@ export const selectLicenseUrlField = () => (system) => {
return system.specSelectors.license().get("url")
}

export const makeSelectLicenseUrl = (system) =>
createSelector(
() => system.specSelectors.url(),
() => system.oas3Selectors.selectedServer(),
() => system.specSelectors.selectLicenseUrlField(),
(specUrl, selectedServer, url) => {
if (url) {
return safeBuildUrl(url, specUrl, { selectedServer })
}

return undefined
}
)

export const selectLicenseIdentifierField = onlyOAS31(() => (system) => {
return system.specSelectors.license().get("identifier")
})

export const makeSelectLicenseUrl = (system) =>
export const selectContactNameField = () => (system) => {
return system.specSelectors.contact().get("name", "the developer")
}

export const selectContactEmailField = () => (system) => {
return system.specSelectors.contact().get("email")
}

export const selectContactUrlField = () => (system) => {
return system.specSelectors.contact().get("url")
}

export const makeSelectContactUrl = (system) =>
createSelector(
() => system.specSelectors.url(),
() => system.oas3Selectors.selectedServer(),
() => system.specSelectors.selectLicenseUrlField(),
() => system.specSelectors.selectContactUrlField(),
(specUrl, selectedServer, url) => {
if (url) {
return safeBuildUrl(url, specUrl, { selectedServer })
Expand Down
16 changes: 16 additions & 0 deletions src/core/plugins/oas31/wrap-components/contact.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @prettier
*/
import React from "react"

const ContactWrapper = (Original, system) => (props) => {
if (system.specSelectors.isOAS31()) {
const OAS31Contact = system.getComponent("OAS31Contact", true)

return <OAS31Contact />
}

return <Original {...props} />
}

export default ContactWrapper

0 comments on commit a3d3f26

Please sign in to comment.