diff --git a/site/gatsby-site/gatsby-node.js b/site/gatsby-site/gatsby-node.js index a24c0455b7..cffa7e5dee 100644 --- a/site/gatsby-site/gatsby-node.js +++ b/site/gatsby-site/gatsby-node.js @@ -34,6 +34,8 @@ const createBlogPages = require('./page-creators/createBlogPages'); const createDocPages = require('./page-creators/createDocPages'); +const createDiscoverPage = require('./page-creators/createDiscoverPage'); + const algoliasearch = require('algoliasearch'); const Translator = require('./src/utils/Translator'); @@ -75,6 +77,7 @@ exports.createPages = async ({ actions, graphql, reporter }) => { }); for (const pageCreator of [ + createDiscoverPage, createBlogPages, createCitationPages, createWordCountsPages, diff --git a/site/gatsby-site/page-creators/createDiscoverPage.js b/site/gatsby-site/page-creators/createDiscoverPage.js new file mode 100644 index 0000000000..b7cdc49e55 --- /dev/null +++ b/site/gatsby-site/page-creators/createDiscoverPage.js @@ -0,0 +1,43 @@ +const path = require('path'); + +const createDiscoverPage = async (graphql, createPage) => { + const pageQuery = await graphql(` + query DiscoverPageQuery { + reports: allMongodbAiidprodReports { + nodes { + report_number + date_published + } + } + } + `); + + const reports = pageQuery.data.reports?.nodes; + + const numBins = 16; + + const histogramBins = new Array(numBins).fill().map(() => 0); + + const publishDates = reports.map((report) => new Date(report.date_published)); + + const latestPublishDate = Math.max(...publishDates); + + const earliestPublishDate = Math.min(...publishDates); + + for (const publishDate of publishDates) { + const position = + (publishDate - earliestPublishDate) / (latestPublishDate - earliestPublishDate); + + const index = Math.round(position * (histogramBins.length - 1)); + + histogramBins[index] += 1; + } + + createPage({ + path: '/apps/discover/', + component: path.resolve('./src/templates/discover.js'), + context: { histogramBins }, + }); +}; + +module.exports = createDiscoverPage; diff --git a/site/gatsby-site/playwright/e2e/discover.spec.ts b/site/gatsby-site/playwright/e2e/discover.spec.ts index 79ffb09705..c58ecd37d2 100644 --- a/site/gatsby-site/playwright/e2e/discover.spec.ts +++ b/site/gatsby-site/playwright/e2e/discover.spec.ts @@ -10,7 +10,7 @@ import config from '../../config'; test.describe('The Discover app', () => { const url = '/apps/discover'; - + test('Successfully loads', async ({ page }) => { await page.goto(url); }); @@ -503,4 +503,20 @@ test.describe('The Discover app', () => { await expect(count).toBeGreaterThanOrEqual(0); }).toPass(); }); + + test('Should select date range', async ({ page }) => { + await page.goto(url); + + await page.locator('[data-cy="epoch_date_published"]').first().click(); + + const rangeKnobSelector = '[data-cy="epoch_date_published"] [data-cy="range-knob"]'; + + await page.locator(rangeKnobSelector).first().focus(); + + await page.locator(rangeKnobSelector).first().press('ArrowRight'); + + await page.locator(rangeKnobSelector).first().press('ArrowRight'); + + await expect(page).toHaveURL(/.*epoch_date_published_min.*/g); + }); }); diff --git a/site/gatsby-site/src/components/discover/Controls.js b/site/gatsby-site/src/components/discover/Controls.js index fc2c2ecf0f..110c3c3d7d 100644 --- a/site/gatsby-site/src/components/discover/Controls.js +++ b/site/gatsby-site/src/components/discover/Controls.js @@ -12,7 +12,7 @@ import { Trans } from 'react-i18next'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faCaretDown, faCaretRight } from '@fortawesome/free-solid-svg-icons'; -const Controls = () => { +const Controls = ({ histogramBins }) => { const { indexUiState } = useInstantSearch(); const [expandFilters, setExpandFilters] = useState(false); @@ -75,7 +75,7 @@ const Controls = () => {
- +
); }; diff --git a/site/gatsby-site/src/components/discover/Discover.js b/site/gatsby-site/src/components/discover/Discover.js index 24a2cfc1f2..bf2c5c2713 100644 --- a/site/gatsby-site/src/components/discover/Discover.js +++ b/site/gatsby-site/src/components/discover/Discover.js @@ -30,7 +30,7 @@ function mapping() { }; } -export default function Discover() { +export default function Discover({ histogramBins }) { const { locale } = useLocalization(); const [indexName] = useState(`instant_search-${locale}-featured`); @@ -114,7 +114,7 @@ export default function Discover() { - {width > 767 ? : } + {width > 767 ? : } diff --git a/site/gatsby-site/src/components/discover/Filter.js b/site/gatsby-site/src/components/discover/Filter.js index 7a82cf0bec..250c208dbe 100644 --- a/site/gatsby-site/src/components/discover/Filter.js +++ b/site/gatsby-site/src/components/discover/Filter.js @@ -54,7 +54,7 @@ function ToggleContent({ label, touched, faIcon, toggled, accordion = false }) { ); } -function ButtonToggle({ label, faIcon, touched, type, filterProps }) { +function ButtonToggle({ label, faIcon, touched, type, filterProps, histogramBins }) { const [toggled, setToggled] = useState(true); const toggleDropdown = () => { @@ -105,20 +105,20 @@ function ButtonToggle({ label, faIcon, touched, type, filterProps }) { toggled ? 'hidden' : 'block ' } bg-white divide-y divide-gray-100 rounded-lg shadow dark:bg-gray-700`} > - + ); } -function FilterContent({ type, filterProps }) { +function FilterContent({ type, filterProps, histogramBins }) { const { default: Component } = componentsMap[type]; - return ; + return ; } const FilterOverlay = React.forwardRef(function Container( - { type, filterProps, ...overlayProps }, + { histogramBins, type, filterProps, ...overlayProps }, ref ) { return ( @@ -131,14 +131,14 @@ const FilterOverlay = React.forwardRef(function Container( >
- +
); }); -export default function Filter({ type, ...filterProps }) { +export default function Filter({ type, histogramBins, ...filterProps }) { const { label, faIcon, attribute } = filterProps; const { touchedCount } = componentsMap[type]; @@ -149,13 +149,7 @@ export default function Filter({ type, ...filterProps }) { return ( <> - + ); } diff --git a/site/gatsby-site/src/components/discover/Filters.js b/site/gatsby-site/src/components/discover/Filters.js index e8d3d81b1e..267ead7bbf 100644 --- a/site/gatsby-site/src/components/discover/Filters.js +++ b/site/gatsby-site/src/components/discover/Filters.js @@ -3,7 +3,7 @@ import REFINEMENT_LISTS from 'components/discover/REFINEMENT_LISTS'; import Filter from './Filter'; import { graphql, useStaticQuery } from 'gatsby'; -function Filters({ expandFilters }) { +function Filters({ expandFilters, histogramBins }) { const { taxa: { nodes: taxa }, } = useStaticQuery(graphql` @@ -44,7 +44,7 @@ function Filters({ expandFilters }) { } return (
- +
); })} diff --git a/site/gatsby-site/src/components/discover/filterTypes/RangeInput.js b/site/gatsby-site/src/components/discover/filterTypes/RangeInput.js index b183b392ef..5fc4b4c711 100644 --- a/site/gatsby-site/src/components/discover/filterTypes/RangeInput.js +++ b/site/gatsby-site/src/components/discover/filterTypes/RangeInput.js @@ -1,8 +1,8 @@ -import React from 'react'; +import React, { useState, useRef, useEffect } from 'react'; import { useRange } from 'react-instantsearch'; import { Trans } from 'react-i18next'; import { debounce } from 'debounce'; -import { Button } from 'flowbite-react'; +import { Button, Checkbox } from 'flowbite-react'; import { Form, Formik } from 'formik'; import Label from 'components/forms/Label'; import TextInputGroup from 'components/forms/TextInputGroup'; @@ -13,7 +13,7 @@ const formatDate = (epoch) => new Date(epoch * 1000).toISOString().substr(0, 10) const dateToEpoch = (date) => new Date(date).getTime() / 1000; -export default function RangeInput({ attribute }) { +export default function RangeInput({ attribute, histogramBins }) { const { range: { min, max }, start, @@ -53,6 +53,16 @@ export default function RangeInput({ attribute }) { {({ values, errors, touched, handleBlur }) => ( <>
+ { + onChange({ min, max }); + }, 1000)} + {...{ histogramBins, attribute }} + />