Skip to content
This repository was archived by the owner on Feb 27, 2024. It is now read-only.

Commit 3d4355a

Browse files
author
Greg Rickaby
authored
Merge branch 'staging' into feature/add-jsdoc-linting
2 parents a329174 + dcc745b commit 3d4355a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1807
-39
lines changed

api/algolia/connector.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import algoliasearch from 'algoliasearch/lite'
2+
import getEnvVar from '@/functions/getEnvVar'
3+
4+
// Define env vars.
5+
export const algoliaIndexName = getEnvVar('ALGOLIA_INDEX_NAME', true)
6+
export const algoliaSearchKey = process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_ONLY_KEY
7+
export const algoliaAppId = process.env.NEXT_PUBLIC_ALGOLIA_APPLICATION_ID
8+
9+
const algoliaClient = algoliasearch(algoliaAppId, algoliaSearchKey)
10+
11+
export const searchClient = {
12+
search(requests) {
13+
if (requests.every(({params}) => !params.query)) {
14+
return Promise.resolve({
15+
results: requests.map(() => ({
16+
hits: [],
17+
nbHits: 0,
18+
nbPages: 0,
19+
page: 0,
20+
processingTimeMS: 0
21+
}))
22+
})
23+
}
24+
25+
return algoliaClient.search(requests)
26+
}
27+
}
28+
29+
export const searchResultsClient = {
30+
search(requests) {
31+
return algoliaClient.search(requests)
32+
}
33+
}

api/wordpress/_global/getPostTypeStaticProps.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {algoliaIndexName} from '@/api/algolia/connector'
12
import getPostTypeById from './getPostTypeById'
23
import getPostTypeArchive from './getPostTypeArchive'
34
import {addApolloState} from '@/api/apolloConfig'
@@ -50,6 +51,11 @@ export default async function getPostTypeStaticProps(
5051
props.error = false
5152
}
5253

54+
// Add Algolia env vars.
55+
props.algolia = {
56+
indexName: algoliaIndexName
57+
}
58+
5359
// Merge in query results as Apollo state.
5460
return addApolloState(apolloClient, {
5561
props,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import PropTypes from 'prop-types'
2+
import {Field} from 'formik'
3+
4+
export default function Checkbox({className, id, label, name}) {
5+
return (
6+
<div className={className}>
7+
{label && (
8+
<label htmlFor={id}>
9+
<Field name={name} type="checkbox" value={`value${id}`} />
10+
{label}
11+
</label>
12+
)}
13+
</div>
14+
)
15+
}
16+
17+
Checkbox.propTypes = {
18+
className: PropTypes.string,
19+
id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired,
20+
label: PropTypes.string.isRequired,
21+
name: PropTypes.string.isRequired
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './Checkbox'
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import PropTypes from 'prop-types'
2+
import {ErrorMessage} from 'formik'
3+
import Checkbox from '@/components/atoms/Inputs/Checkbox'
4+
5+
export default function CheckboxGroup({
6+
checkboxes,
7+
className,
8+
description,
9+
id: groupId,
10+
label
11+
}) {
12+
return (
13+
<div
14+
aria-labelledby={groupId}
15+
className={className}
16+
id={groupId}
17+
role="group"
18+
>
19+
{label && <label htmlFor={groupId}>{label}</label>}
20+
{description && <p>{description}</p>}
21+
{!!checkboxes.length > 0 &&
22+
checkboxes.map((checkbox) => {
23+
const {id, label} = checkbox
24+
25+
return (
26+
<Checkbox
27+
id={id}
28+
key={`${groupId}-${id}`}
29+
label={label}
30+
name={groupId}
31+
/>
32+
)
33+
})}
34+
<ErrorMessage name={groupId} />
35+
</div>
36+
)
37+
}
38+
39+
CheckboxGroup.propTypes = {
40+
checkboxes: PropTypes.arrayOf(PropTypes.object).isRequired,
41+
className: PropTypes.string,
42+
description: PropTypes.string,
43+
id: PropTypes.string.isRequired,
44+
isRequired: PropTypes.bool,
45+
label: PropTypes.string,
46+
validation: PropTypes.func
47+
}
48+
49+
CheckboxGroup.defaultProps = {
50+
isRequired: false
51+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {default} from './CheckboxGroup'

components/atoms/Inputs/Text/Text.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,33 @@ import {Field, ErrorMessage} from 'formik'
44
export default function Text({
55
className,
66
description,
7-
fieldId,
7+
id,
88
isRequired,
99
label,
1010
type,
1111
validation
1212
}) {
1313
return (
14-
<div className={className} key={fieldId}>
15-
{label && <label htmlFor={fieldId}>{label}</label>}
14+
<div className={className} key={id}>
15+
{label && <label htmlFor={id}>{label}</label>}
1616
<Field
1717
aria-required={isRequired}
18-
id={fieldId}
19-
name={fieldId}
18+
id={id}
19+
name={id}
2020
required={isRequired}
2121
type={type}
2222
validate={validation}
2323
/>
2424
{description && <p>{description}</p>}
25-
<ErrorMessage name={fieldId} />
25+
<ErrorMessage name={id} />
2626
</div>
2727
)
2828
}
2929

3030
Text.propTypes = {
3131
className: PropTypes.string,
3232
description: PropTypes.string,
33-
fieldId: PropTypes.string.isRequired,
33+
id: PropTypes.string.isRequired,
3434
isRequired: PropTypes.bool,
3535
label: PropTypes.string.isRequired,
3636
type: PropTypes.string.isRequired,

components/atoms/Inputs/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export {default as Checkbox} from './Checkbox'
2+
export {default as CheckboxGroup} from './CheckboxGroup'
13
export {default as Text} from './Text'

components/common/AlgoliaProvider.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import PropTypes from 'prop-types'
2+
import {createContext} from 'react'
3+
4+
// Initialize Algolia context object.
5+
export const AlgoliaContext = createContext({
6+
algolia: null
7+
})
8+
9+
/**
10+
* Provide indexName env var.
11+
*
12+
* @param {Object} props The component attributes as props.
13+
* @return {Element} The child elements wrapped in a context provider.
14+
*/
15+
export default function AlgoliaProvider(props) {
16+
const {value, children} = props
17+
18+
return (
19+
<AlgoliaContext.Provider value={value}>{children}</AlgoliaContext.Provider>
20+
)
21+
}
22+
23+
AlgoliaProvider.propTypes = {
24+
indexName: PropTypes.string,
25+
children: PropTypes.object,
26+
value: PropTypes.object
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {searchResultsClient} from '@/api/algolia/connector'
2+
import cn from 'classnames'
3+
import PropTypes from 'prop-types'
4+
import {AlgoliaContext} from '@/components/common/AlgoliaProvider'
5+
import React, {useContext} from 'react'
6+
import {Configure, InstantSearch} from 'react-instantsearch-dom'
7+
import styles from './AlgoliaResults.module.css'
8+
import NoResults from './templates/NoResults'
9+
import SearchResults from './templates/SearchResults'
10+
11+
// TODO: Create Storybook for this component.
12+
13+
export default function AlgoliaResults({config}) {
14+
const {indexName} = useContext(AlgoliaContext)
15+
return (
16+
<section className={cn('container', styles.algoliaResults)}>
17+
{config.query !== '' && (
18+
<InstantSearch
19+
searchClient={config.query !== '' ? searchResultsClient : ''}
20+
indexName={indexName}
21+
>
22+
<Configure {...config} />
23+
<SearchResults indexName={indexName} />
24+
</InstantSearch>
25+
)}
26+
{config.query === '' && <NoResults query={config.query} />}
27+
</section>
28+
)
29+
}
30+
31+
AlgoliaResults.propTypes = {
32+
config: PropTypes.shape({
33+
query: PropTypes.string,
34+
hitsPerPage: PropTypes.number.isRequired
35+
})
36+
}
37+
38+
AlgoliaResults.defaultProps = {
39+
config: {
40+
query: '',
41+
hitsPerPage: 15
42+
}
43+
}

0 commit comments

Comments
 (0)