/*eslint max-lines: ["warn", {"max": 400, "skipBlankLines": true, "skipComments": true}]*/
/*eslint max-params:["warn", { max: 10 }], */
import './vaultPage.less';
import React, { useState, useEffect, useRef } from 'react';
import Nav from '@navbar/nav.jsx';
import Navbar from '@navbar/navbar.jsx';
import RecentNavItems from '@navbar/recent.navitem.jsx';
const { both: RecentNavItem } = RecentNavItems;
import Account from '@navbar/account.navitem.jsx';
import NewBrew from '@navbar/newbrew.navitem.jsx';
import HelpNavItem from '@navbar/help.navitem.jsx';
import BrewItem from '../basePages/listPage/brewItem/brewItem.jsx';
import SplitPane from '@components/splitPane/splitPane.jsx';
import ErrorIndex from '../errorPage/errors/errorIndex.js';
import TagInput from '../../editor/tagInput/tagInput.jsx';
import request from '../../utils/request-middleware.js';
const DEFAULT_SORT_TYPE = 'alpha';
const DEFAULT_SORT_DIR = 'asc';
const DEFAULT_LAYOUT_MODE = 'grid';
const DEFAULT_THEME_MODE = 'light';
const USERPAGE_LAYOUT_MODE = 'HB_listPage_layout_mode';
const USERPAGE_THEME_MODE = 'HB_listPage_theme_mode';
const VaultPage = (props)=>{
const [pageState, setPageState] = useState(parseInt(props.query.page) || 1);
const [sortState, setSort] = useState(props.query.sort || 'title');
const [dirState, setdir] = useState(props.query.dir || 'asc');
//Response state
const [brewCollection, setBrewCollection] = useState(null);
const [totalBrews, setTotalBrews] = useState(null);
const [searching, setSearching] = useState(false);
const [error, setError] = useState(null);
const [layoutMode, setLayoutMode] = useState(DEFAULT_LAYOUT_MODE);
const [themeMode, setThemeMode] = useState(DEFAULT_THEME_MODE);
const titleRef = useRef(null);
const authorRef = useRef(null);
const countRef = useRef(null);
const v3Ref = useRef(null);
const legacyRef = useRef(null);
const submitButtonRef = useRef(null);
const parseTags = (value)=>value?.split(',').filter(Boolean) || [];
const [tagsListAnd, setTagListAnd] = useState(parseTags(props.query.tagsAnd));
const [tagsListOr, setTagListOr] = useState(parseTags(props.query.tagsOr));
const [tagsListNot, setTagListNot] = useState(parseTags(props.query.tagsNot));
useEffect(()=>{
disableSubmitIfFormInvalid();
const newLayoutMode = localStorage.getItem(USERPAGE_LAYOUT_MODE) || DEFAULT_LAYOUT_MODE;
const newThemeMode = localStorage.getItem(USERPAGE_THEME_MODE) || DEFAULT_THEME_MODE;
setLayoutMode(newLayoutMode);
setThemeMode(newThemeMode);
loadPage(pageState, true, props.query.sort, props.query.dir);
}, []);
useEffect(()=>{disableSubmitIfFormInvalid()},[tagsListAnd, tagsListOr, tagsListNot])
const updateStateWithBrews = (brews, page)=>{
setBrewCollection(brews || null);
setPageState(parseInt(page) || 1);
setSearching(false);
};
const buildQueryParams = (entries)=>{
const params = new URLSearchParams();
entries.forEach(([key, value])=>{
if(value === undefined || value === null || value === '') return;
if(Array.isArray(value) && value.length === 0) return;
params.set(key, Array.isArray(value) ? value.join(',') : value);
});
return params.toString();
};
const performSearch = async (formContents, page, sort, dir)=>{
const { title, author, count, v3, legacy, tagsAnd, tagsOr, tagsNot } = formContents;
const params = buildQueryParams([
['title', title],
['author', author],
['v3', v3],
['legacy', legacy],
['tagsAnd', tagsAnd],
['tagsOr', tagsOr],
['tagsNot', tagsNot],
['count', count],
['page', page],
['sort', sort],
['dir', dir]
]);
const url = new URL(window.location.href);
url.search = params;
window.history.replaceState(null, '', url.toString());
const response = await request.get(`/api/vault?${params}`)
.catch((error)=>{
console.error('error at loadPage: ', error.response?.body.message);
setError(error.response?.body);
updateStateWithBrews([], 1);
});
if(response?.ok) {
updateStateWithBrews(response.body.brews, page);
}
};
const loadTotal = async (formContents)=>{
const { title, author, v3, legacy, tagsAnd, tagsOr, tagsNot } = formContents;
setTotalBrews(null);
const params = buildQueryParams([
['title', title],
['author', author],
['v3', v3],
['legacy', legacy],
['tagsAnd', tagsAnd],
['tagsOr', tagsOr],
['tagsNot', tagsNot]
]);
const response = await request.get(`/api/vault/total?${params}`)
.catch((error)=>{
console.log('error at loadTotal: ');
console.error(error);
setError(error);
updateStateWithBrews([], 1);
});
if(response?.ok) {
setTotalBrews(response.body.totalBrews);
}
};
const loadPage = async (page, updateTotal, sort, dir)=>{
if(!validateForm()) return;
setSearching(true);
setError(null);
const title = titleRef.current.value || '';
const author = authorRef.current.value || '';
const count = countRef.current.value || 20;
const v3 = v3Ref.current.checked != false;
const legacy = legacyRef.current.checked != false;
const tagsAnd = tagsListAnd;
const tagsOr = tagsListOr;
const tagsNot = tagsListNot;
const formContents = {
title, author, count, v3, legacy, tagsAnd, tagsOr, tagsNot
};
const sortOption = sort || 'title';
const dirOption = dir || 'asc';
const pageProp = page || 1;
setSort(sortOption);
setdir(dirOption);
performSearch(formContents, pageProp, sortOption, dirOption);
if(updateTotal)
loadTotal(formContents);
};
const renderNavItems = ()=>(