2019-07-31 13:11:00 +09:00
|
|
|
import React, { Suspense, useState } from 'react'
|
|
|
|
import styled from 'styled-components'
|
|
|
|
|
|
|
|
import { Fallback } from './Availability'
|
|
|
|
import { mobile } from '../util/css'
|
|
|
|
|
2019-07-31 18:41:44 +09:00
|
|
|
class ErrorBoundary extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = { hasError: false }
|
|
|
|
}
|
|
|
|
static getDerivedStateFromError(error) {
|
|
|
|
return { hasError: true, message: error.message }
|
|
|
|
}
|
|
|
|
componentDidCatch(error, info) {}
|
|
|
|
render() {
|
|
|
|
if (this.state.hasError) {
|
|
|
|
return <h4>{this.state.message}</h4>
|
|
|
|
}
|
|
|
|
return this.props.children
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const BoundedSuspense = ({ children }) => (
|
|
|
|
<ErrorBoundary>
|
|
|
|
<Suspense fallback={<Fallback />}>{children}</Suspense>
|
|
|
|
</ErrorBoundary>
|
|
|
|
)
|
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
export function Card({ title, nameList, alternativeList = [], children }) {
|
2019-07-31 18:41:44 +09:00
|
|
|
const [revealAlternatives, setRevealAlternatives] = useState(false)
|
2019-07-31 13:11:00 +09:00
|
|
|
|
|
|
|
function onClick() {
|
2019-07-31 18:41:44 +09:00
|
|
|
setRevealAlternatives(true)
|
2019-07-31 13:11:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<CardWrapper>
|
|
|
|
<CardTitle>{title}</CardTitle>
|
|
|
|
<>
|
|
|
|
{nameList.map((name) => (
|
2019-07-31 18:41:44 +09:00
|
|
|
<BoundedSuspense>{children(name)}</BoundedSuspense>
|
2019-07-31 13:11:00 +09:00
|
|
|
))}
|
2019-07-31 18:41:44 +09:00
|
|
|
{revealAlternatives &&
|
|
|
|
alternativeList.map((name) => (
|
|
|
|
<BoundedSuspense>{children(name)}</BoundedSuspense>
|
|
|
|
))}
|
2019-07-31 13:11:00 +09:00
|
|
|
</>
|
2019-07-31 18:41:44 +09:00
|
|
|
{alternativeList.length > 0 && !revealAlternatives ? (
|
2019-07-31 13:11:00 +09:00
|
|
|
<ShowAlternativesButton onClick={onClick}>
|
|
|
|
show alternatives
|
|
|
|
</ShowAlternativesButton>
|
|
|
|
) : null}
|
|
|
|
</CardWrapper>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const CardWrapper = styled.div`
|
|
|
|
margin-bottom: 20px;
|
|
|
|
padding: 40px;
|
|
|
|
border-radius: 2px;
|
|
|
|
|
|
|
|
${mobile} {
|
|
|
|
padding: 20px;
|
|
|
|
box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.1);
|
|
|
|
border-radius: 0;
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-07-31 18:41:44 +09:00
|
|
|
const CardTitle = styled.div`
|
|
|
|
font-size: 0.8rem;
|
|
|
|
font-weight: bold;
|
|
|
|
margin-bottom: 15px;
|
|
|
|
`
|
|
|
|
|
2019-07-31 13:11:00 +09:00
|
|
|
const ShowAlternativesButton = styled.div`
|
|
|
|
display: inline-block;
|
|
|
|
margin-top: 10px;
|
|
|
|
padding: 5px 0;
|
|
|
|
border: none;
|
|
|
|
border-bottom: 1px dashed black;
|
|
|
|
cursor: pointer;
|
|
|
|
font-family: monospace;
|
|
|
|
font-size: 1rem;
|
|
|
|
`
|