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 StringArrayEditor = require('../stringArrayEditor/stringArrayEditor.jsx'); const request = require('../../utils/request-middleware.js'); const ArchivePage = createClass({ displayName : 'ArchivePage', getDefaultProps : function () { return {}; }, getInitialState : function () { return { //request title : this.props.query.title || '', //tags : {}, legacy : true, v3 : true, pageSize : 10, page : 1, //response brewCollection : null, totalPages : null, totalBrews : null, searching : false, error : null, }; }, componentDidMount : function() { }, handleChange(inputName, e) { this.setState({ [inputName]: e.target.value }); }, updateStateWithBrews : function (brews, page, totalPages, totalBrews) { this.setState({ brewCollection : brews || null, page : page || 1, totalPages : totalPages || 1, totalBrews : totalBrews, searching : false }); }, loadPage : async function(page) { this.updateUrl(); try { this.setState({ searching: true, error: null }); const title = encodeURIComponent(this.state.title); const size = parseInt(this.state.pageSize); const {legacy, v3} = this.state; await request.get(`/api/archive?title=${title}&page=${page}&size=${size}&v3=${v3}&legacy=${legacy}`) .then((response)=>{ if(response.ok) { this.updateStateWithBrews(response.body.brews, page, response.body.totalPages, response.body.totalBrews); } }); } catch (error) { console.log(`LoadPage error: ${error}`); this.setState({ error: `${error}` }); this.updateStateWithBrews([], 1, 1); } }, updateUrl : function() { const url = new URL(window.location.href); const urlParams = new URLSearchParams(url.search); urlParams.set('title', this.state.title); urlParams.set('page', this.state.page); urlParams.set('pageSize', this.state.pageSize); urlParams.set('v3', this.state.v3); urlParams.set('legacy', this.state.legacy); url.search = urlParams.toString(); // Convert URLSearchParams to string window.history.replaceState(null, null, url); }, renderPaginationControls() { const { page, totalPages,} = this.state; const pages = new Array(totalPages).fill().map((_, index) => (
  • this.loadPage(index+1)}>{index + 1}
  • )); if(totalPages === null) {return;} return (
    {page > 1 && ( )}
      {pages}
    {page < totalPages && ( )}
    ); }, renderFoundBrews() { const { title, brewCollection, page, totalPages, error } = this.state; if(title === '' && error === null) {return (

    Whenever you want, just start typing...

    );} if(error === 'Error: Service Unavailable') { 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 || error === 'Error: Not found') { return (

    We haven't found brews meeting your request.

    ); } return (
    Brews found: {this.state.totalBrews} {brewCollection.map((brew, index)=>( ))} {this.renderPaginationControls()}
    ); }, renderForm : function () { return (

    Brew Lookup

    {/* In the future, we should be able to filter the results by adding tags. <this.handleChange('tags', e)}/> check metadataEditor.jsx L65 */}
    ); }, 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;