forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
177 lines (158 loc) · 4.72 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'
import groupBy from 'lodash.groupby'
import {
categories,
findCategory,
services,
getDefinitionsForCategory,
} from '../lib/service-definitions'
import ServiceDefinitionSetHelper from '../lib/service-definitions/service-definition-set-helper'
import { baseUrl } from '../constants'
import Meta from './meta'
import Header from './header'
import SuggestionAndSearch from './suggestion-and-search'
import DonateBox from './donate'
import MarkupModal from './markup-modal'
import Usage from './usage'
import Footer from './footer'
import {
CategoryHeading,
CategoryHeadings,
CategoryNav,
} from './category-headings'
import BadgeExamples from './badge-examples'
import { BaseFont, GlobalStyle } from './common'
const AppContainer = styled(BaseFont)`
text-align: center;
`
export default class Main extends React.Component {
constructor(props) {
super(props)
this.state = {
isSearchInProgress: false,
isQueryTooShort: false,
searchResults: undefined,
selectedExample: undefined,
}
this.searchTimeout = 0
this.handleExampleSelected = this.handleExampleSelected.bind(this)
this.dismissMarkupModal = this.dismissMarkupModal.bind(this)
this.searchQueryChanged = this.searchQueryChanged.bind(this)
}
static propTypes = {
// `pageContext` is the `context` passed to `createPage()` in
// `gatsby-node.js`. In the case of the index page, `pageContext` is empty.
pageContext: {
category: PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
}),
}.isRequired,
}
performSearch(query) {
const isQueryTooShort = query.length === 1
let searchResults
if (query.length >= 2) {
const flat = ServiceDefinitionSetHelper.create(services)
.notDeprecated()
.search(query)
.toArray()
searchResults = groupBy(flat, 'category')
}
this.setState({
isSearchInProgress: false,
isQueryTooShort,
searchResults,
})
}
searchQueryChanged(query) {
/*
Add a small delay before showing search results
so that we wait until the user has stopped typing
before we start loading stuff.
This
a) reduces the amount of badges we will load and
b) stops the page from 'flashing' as the user types, like this:
https://user-images.githubusercontent.com/7288322/42600206-9b278470-85b5-11e8-9f63-eb4a0c31cb4a.gif
*/
this.setState({ isSearchInProgress: true })
window.clearTimeout(this.searchTimeout)
this.searchTimeout = window.setTimeout(() => this.performSearch(query), 500)
}
handleExampleSelected(example) {
this.setState({ selectedExample: example })
}
dismissMarkupModal() {
this.setState({ selectedExample: undefined })
}
renderCategory(category, definitions) {
const { id } = category
return (
<div key={id}>
<CategoryHeading category={category} />
<BadgeExamples
baseUrl={baseUrl}
definitions={definitions}
onClick={this.handleExampleSelected}
/>
</div>
)
}
renderMain() {
const {
pageContext: { category },
} = this.props
const { isSearchInProgress, isQueryTooShort, searchResults } = this.state
if (isSearchInProgress) {
return <div>searching...</div>
} else if (isQueryTooShort) {
return <div>Search term must have 2 or more characters</div>
} else if (searchResults) {
return Object.entries(searchResults).map(([categoryId, definitions]) =>
this.renderCategory(findCategory(categoryId), definitions)
)
} else if (category) {
const definitions = ServiceDefinitionSetHelper.create(
getDefinitionsForCategory(category.id)
)
.notDeprecated()
.toArray()
return (
<div>
<CategoryNav categories={categories} />
{this.renderCategory(category, definitions)}
</div>
)
} else {
return <CategoryHeadings categories={categories} />
}
}
render() {
const { selectedExample } = this.state
return (
<AppContainer id="app">
<GlobalStyle />
<Meta />
<Header />
<MarkupModal
baseUrl={baseUrl}
example={selectedExample}
onRequestClose={this.dismissMarkupModal}
/>
<section>
<SuggestionAndSearch
baseUrl={baseUrl}
onBadgeClick={this.handleExampleSelected}
queryChanged={this.searchQueryChanged}
/>
<DonateBox />
</section>
{this.renderMain()}
<Usage baseUrl={baseUrl} />
<Footer baseUrl={baseUrl} />
</AppContainer>
)
}
}