require('./archivePage.less'); const React = require('react'); const createClass = require('create-react-class'); const _ = require('lodash'); const cx = require('classnames'); const Nav = require('naturalcrit/nav/nav.jsx'); const Navbar = require('../../navbar/navbar.jsx'); const RecentNavItem = require('../../navbar/recent.navitem.jsx').both; const Account = require('../../navbar/account.navitem.jsx'); const NewBrew = require('../../navbar/newbrew.navitem.jsx'); const HelpNavItem = require('../../navbar/help.navitem.jsx'); const BrewItem = require('../basePages/listPage/brewItem/brewItem.jsx'); const request = require('../../utils/request-middleware.js'); const ArchivePage = createClass({ displayName : 'ArchivePage', getDefaultProps : function () { return {}; }, getInitialState : function () { return { title : this.props.query.title || '', brewCollection : null, page : 1, totalPages : 1, searching : false, error : null, }; }, componentDidMount : function() { }, handleChange(e) { this.setState({ title: e.target.value }); }, updateStateWithBrews : function (brews, page, totalPages) { this.setState({ brewCollection : brews || null, page : page || 1, totalPages : totalPages || 1, searching : false }); }, loadPage : async function(page) { this.updateUrl(); try { //this.updateUrl(); this.setState({ searching: true, error: null }); const title = encodeURIComponent(this.state.title); await request.get(`/api/archive?title=${title}&page=${page}`) .then((response)=>{ if(response.ok) { this.updateStateWithBrews(response.body.brews, page, response.body.totalPages); } }); } catch (error) { console.log(`LoadPage error: ${error}`); this.updateStateWithBrews([], 1, 1); } }, updateUrl : function() { const url = new URL(window.location.href); const urlParams = new URLSearchParams(url.search); // Set the title and page parameters urlParams.set('title', this.state.title); urlParams.set('page', this.state.page); url.search = urlParams.toString(); // Convert URLSearchParams to string window.history.replaceState(null, null, url); }, renderPaginationControls() { const { title, brewCollection, page, totalPages, error } = this.state; const pages = new Array(totalPages).fill().map((_, index) => (
  • this.loadPage(index+1)}>{index + 1}
  • )); return (
    {page > 1 && ( )}
      {pages}
    {page < totalPages && ( )}
    ); }, renderFoundBrews() { const { title, brewCollection, page, totalPages, error } = this.state; if(title === '') {return (

    Whenever you want, just start typing...

    );} if(error !== null) { return (

    I'm sorry, your request didn't work


    Your search is not specific enough. Too many brews meet this criteria for us to display them.

    ); } if(!brewCollection || brewCollection.length === 0) { return (

    We haven't found brews meeting your request.

    ); } return (
    {brewCollection.map((brew, index)=>( ))} {this.renderPagination()}
    ); }, renderForm : function () { return (

    Brew Lookup

    { if(e.key === 'Enter') { this.handleChange(e); this.loadPage(1); } }} placeholder='v3 Reference Document' /> {/* In the future, we should be able to filter the results by adding tags. */}
    ); }, renderNavItems : function () { return ( Archive: Search for brews ); }, render : function () { return (
    {this.renderNavItems()}

    Welcome to the Archive

    {this.renderForm()}

    Your results, my lordship

    {this.renderFoundBrews()}
    ); }, }); module.exports = ArchivePage;