Files
shields/frontend/components/badge-examples.js
T
Paul MelnikowandGitHub 83ac6ff1b3 Enforce use of template literals (#2242)
This is consistent with what we're pretty much already doing, and saves us from making the request during code review.

These were all autofixed and most of them seem easier to read. Some in the legacy services should be rewritten in more legible forms during refactor (ie using intermediate variables, or using request’s qs option). There are some in helper functions and elsewhere that should get rewritten separately. I don't want to change them in this PR because the changes will get lost in this diff, though we could identify them here and fix them before or just after.
2018-11-02 17:11:44 -04:00

150 lines
3.4 KiB
JavaScript

import React from 'react'
import { Link } from 'react-router-dom'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import resolveBadgeUrl from '../lib/badge-url'
const Badge = ({
title,
exampleUrl,
previewUrl,
urlPattern,
documentation,
baseUrl,
longCache,
shouldDisplay = () => true,
onClick,
}) => {
const handleClick = onClick
? () =>
onClick({
title,
exampleUrl,
previewUrl,
urlPattern,
documentation,
})
: undefined
const previewImage = previewUrl ? (
<img
className={classNames('badge-img', { clickable: onClick })}
onClick={handleClick}
src={resolveBadgeUrl(previewUrl, baseUrl, { longCache })}
alt=""
/>
) : (
'\u00a0'
) // non-breaking space
const resolvedExampleUrl = resolveBadgeUrl(
urlPattern || previewUrl,
baseUrl,
{ longCache: false }
)
if (shouldDisplay()) {
return (
<tr>
<th
className={classNames({ clickable: onClick })}
onClick={handleClick}
>
{title}:
</th>
<td>{previewImage}</td>
<td>
<code
className={classNames({ clickable: onClick })}
onClick={handleClick}
>
{resolvedExampleUrl}
</code>
</td>
</tr>
)
}
return null
}
Badge.propTypes = {
title: PropTypes.string.isRequired,
exampleUrl: PropTypes.string,
previewUrl: PropTypes.string,
urlPattern: PropTypes.string,
documentation: PropTypes.string,
baseUrl: PropTypes.string,
longCache: PropTypes.bool.isRequired,
shouldDisplay: PropTypes.func,
onClick: PropTypes.func.isRequired,
}
const Category = ({ category, examples, baseUrl, longCache, onClick }) => {
if (examples.filter(example => example.shouldDisplay()).length === 0) {
return null
}
return (
<div>
<Link to={`/examples/${category.id}`}>
<h3 id={category.id}>{category.name}</h3>
</Link>
<table className="badge">
<tbody>
{examples.map(badgeData => (
<Badge
key={badgeData.key}
{...badgeData}
baseUrl={baseUrl}
longCache={longCache}
onClick={onClick}
/>
))}
</tbody>
</table>
</div>
)
}
Category.propTypes = {
category: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
}).isRequired,
examples: PropTypes.arrayOf(
PropTypes.shape({
title: PropTypes.string.isRequired,
exampleUrl: PropTypes.string,
previewUrl: PropTypes.string,
urlPattern: PropTypes.string,
documentation: PropTypes.string,
})
).isRequired,
baseUrl: PropTypes.string,
longCache: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}
const BadgeExamples = ({ categories, baseUrl, longCache, onClick }) => (
<div>
{categories.map((categoryData, i) => (
<Category
key={i}
{...categoryData}
baseUrl={baseUrl}
longCache={longCache}
onClick={onClick}
/>
))}
</div>
)
BadgeExamples.propTypes = {
categories: PropTypes.arrayOf(
PropTypes.shape({
category: Category.propTypes.category,
examples: Category.propTypes.examples,
})
),
baseUrl: PropTypes.string,
longCache: PropTypes.bool.isRequired,
onClick: PropTypes.func.isRequired,
}
export { Badge, BadgeExamples }