mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-21 15:36:18 -05:00
Merging useSearchMonths and useSearchDateRange into useSearchDate to avoid setting query twice when closing DateSelectorModal.
This commit is contained in:
parent
278606f031
commit
1795fae1b0
5 changed files with 71 additions and 82 deletions
|
|
@ -13,7 +13,7 @@ import {
|
|||
Statistic,
|
||||
StatisticGroup
|
||||
} from "./components/Theme";
|
||||
import {Link, useNavigate} from "react-router-dom";
|
||||
import {Link, useNavigate, useSearchParams} from "react-router-dom";
|
||||
import {BandwidthProgressCombined, CPUUsageProgress} from "./components/admin/Status";
|
||||
import {ProgressPlaceholder} from "./components/Placeholder";
|
||||
import {GridColumn, GridRow, Icon, Message} from "semantic-ui-react";
|
||||
|
|
@ -170,8 +170,7 @@ export function DashboardPage() {
|
|||
loading,
|
||||
setSearchStr: setSuggestionSearchStr,
|
||||
setSearchTags,
|
||||
months, setMonths,
|
||||
dateRange, setDateRange,
|
||||
months, dateRange, setDates, clearDate,
|
||||
} = useSearchSuggestions(searchStr, activeTags);
|
||||
const {status} = useContext(StatusContext);
|
||||
|
||||
|
|
@ -235,9 +234,9 @@ export function DashboardPage() {
|
|||
{getSearchResultsInput()}
|
||||
</Grid.Column>
|
||||
<Grid.Column width={1} textAlign='right' style={{padding: 0}}>
|
||||
<DateSelectorButton onMonthsChange={setMonths} defaultMonthsSelected={months}
|
||||
defaultDateRange={dateRange}
|
||||
onDateRangeChange={setDateRange}/>
|
||||
<DateSelectorButton defaultMonthsSelected={months} defaultDateRange={dateRange}
|
||||
onClear={clearDate} onDatesChange={setDates}
|
||||
/>
|
||||
</Grid.Column>
|
||||
<Grid.Column width={1} textAlign='right'>
|
||||
<FileSearchFilterButton/>
|
||||
|
|
@ -249,9 +248,8 @@ export function DashboardPage() {
|
|||
<Grid>
|
||||
<Grid.Row columns={2}>
|
||||
<Grid.Column textAlign='right' width={2}>
|
||||
<DateSelectorButton onMonthsChange={setMonths} defaultMonthsSelected={months}
|
||||
defaultDateRange={dateRange}
|
||||
onDateRangeChange={setDateRange}
|
||||
<DateSelectorButton defaultMonthsSelected={months} defaultDateRange={dateRange}
|
||||
onClear={clearDate} onDatesChange={setDates}
|
||||
buttonProps={{size: 'big'}}/>
|
||||
</Grid.Column>
|
||||
<Grid.Column textAlign='right' width={2}>
|
||||
|
|
@ -267,9 +265,8 @@ export function DashboardPage() {
|
|||
<Grid>
|
||||
<Grid.Row columns={2}>
|
||||
<Grid.Column textAlign='right' width={1}>
|
||||
<DateSelectorButton onMonthsChange={setMonths} defaultMonthsSelected={months}
|
||||
defaultDateRange={dateRange}
|
||||
onDateRangeChange={setDateRange}
|
||||
<DateSelectorButton defaultMonthsSelected={months} defaultDateRange={dateRange}
|
||||
onClear={clearDate} onDatesChange={setDates}
|
||||
buttonProps={{size: 'big'}}/>
|
||||
</Grid.Column>
|
||||
<Grid.Column textAlign='right' width={1}>
|
||||
|
|
|
|||
|
|
@ -149,10 +149,10 @@ function DateRangeForm({dateRange, setDateRange}) {
|
|||
}
|
||||
|
||||
export function DateSelectorButton({
|
||||
onMonthsChange,
|
||||
onDateRangeChange,
|
||||
onDatesChange,
|
||||
defaultMonthsSelected,
|
||||
defaultDateRange,
|
||||
onClear,
|
||||
buttonProps
|
||||
}) {
|
||||
const emptyDateRange = [null, null];
|
||||
|
|
@ -179,14 +179,18 @@ export function DateSelectorButton({
|
|||
if (e) e.preventDefault();
|
||||
setOpen(false);
|
||||
// Only submit selection when user has closed selector.
|
||||
if (onDateRangeChange && dateRange[0] <= dateRange[1]) {
|
||||
onDateRangeChange(dateRange);
|
||||
} else if (onDateRangeChange && dateRangeIsEmpty(dateRange)) {
|
||||
//Clear the date range.
|
||||
onDateRangeChange(dateRange);
|
||||
let newFromDate;
|
||||
let newToDate;
|
||||
let newMonths;
|
||||
if (dateRange && dateRange[0] < dateRange[1]) {
|
||||
newFromDate = dateRange[0];
|
||||
newToDate = dateRange[1];
|
||||
}
|
||||
if (onMonthsChange) {
|
||||
onMonthsChange(monthsSelected);
|
||||
if (monthsSelected) {
|
||||
newMonths = monthsSelected;
|
||||
}
|
||||
if (onDatesChange) {
|
||||
onDatesChange(newFromDate, newToDate, newMonths);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -198,6 +202,10 @@ export function DateSelectorButton({
|
|||
if (e) e.preventDefault();
|
||||
setDateRange(emptyDateRange);
|
||||
setMonthsSelected([]);
|
||||
if (onClear) {
|
||||
onClear();
|
||||
}
|
||||
setOpen(false);
|
||||
}
|
||||
|
||||
return <React.Fragment>
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import React, {useState} from "react";
|
||||
import {Route, Routes, useNavigate} from "react-router-dom";
|
||||
import {FilesSearchView} from "./Files";
|
||||
import {useLatestRequest, usePages, useSearchDateRange, useSearchFilter, useSearchMonths} from "../hooks/customHooks";
|
||||
import {useLatestRequest, usePages, useSearchDate, useSearchFilter} from "../hooks/customHooks";
|
||||
import {ZimSearchView} from "./Zim";
|
||||
import {searchEstimateZims, searchSuggestions} from "../api";
|
||||
import {filterToMimetypes, fuzzyMatch, normalizeEstimate, SearchResultsInput, TabLinks} from "./Common";
|
||||
|
|
@ -38,8 +38,7 @@ const SUGGESTED_APPS = [
|
|||
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
const {months, setMonths} = useSearchMonths();
|
||||
const {dateRange, setDateRange} = useSearchDateRange();
|
||||
const {dateRange, setDateRange, months, setDates} = useSearchDate();
|
||||
const {searchParams, updateQuery, getLocationStr} = React.useContext(QueryContext);
|
||||
// `searchStr` means actually fetch the files/zims.
|
||||
const searchStr = searchParams.get('q');
|
||||
|
|
@ -90,8 +89,7 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false
|
|||
setSearchStr,
|
||||
clearSearch,
|
||||
setTags,
|
||||
months, setMonths,
|
||||
dateRange, setDateRange,
|
||||
months, dateRange, setDateRange, setDates,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,8 +101,7 @@ export function useSuggestions(searchStr, tagNames, filter) {
|
|||
zimsEstimates: [],
|
||||
}
|
||||
const [suggestions, setSuggestions] = React.useState(defaultSuggestions);
|
||||
const {months} = useSearchMonths();
|
||||
const {dateRange} = useSearchDateRange();
|
||||
const {dateRange, months} = useSearchDate();
|
||||
// fileGroups/channels/domains.
|
||||
const {data, sendRequest, loading} = useLatestRequest(500);
|
||||
// Zims are slow, so they are separate.
|
||||
|
|
@ -152,8 +149,7 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames) {
|
|||
const [searchStr, setSearchStr] = React.useState(defaultSearchStr || '');
|
||||
const [searchTags, setSearchTags] = React.useState(defaultTagNames);
|
||||
const {SingleTag, fuzzyMatchTagsByName} = React.useContext(TagsContext);
|
||||
const {months, setMonths} = useSearchMonths();
|
||||
const {dateRange, setDateRange} = useSearchDateRange();
|
||||
const {dateRange, months, setDates, clearDate} = useSearchDate();
|
||||
const {suggestions, loading} = useSuggestions(searchStr, searchTags, filter);
|
||||
const {getLocationStr} = React.useContext(QueryContext);
|
||||
|
||||
|
|
@ -290,11 +286,10 @@ export function useSearchSuggestions(defaultSearchStr, defaultTagNames) {
|
|||
suggestionsSums,
|
||||
searchStr, setSearchStr,
|
||||
setSearchTags,
|
||||
months, setMonths,
|
||||
months, dateRange, setDates, clearDate,
|
||||
handleResultSelect,
|
||||
resultRenderer,
|
||||
loading,
|
||||
dateRange, setDateRange,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -329,6 +324,7 @@ export function SearchIconButton() {
|
|||
setSearchStr,
|
||||
} = useSearchSuggestions();
|
||||
const [open, setOpen] = React.useState(false);
|
||||
const prevOpen = React.useRef(open);
|
||||
|
||||
const localHandleResultSelect = (i) => {
|
||||
// Close modal when user selects a result.
|
||||
|
|
@ -336,6 +332,14 @@ export function SearchIconButton() {
|
|||
handleResultSelect(i);
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
if (prevOpen.current === true && !open) {
|
||||
// User has closed the modal.
|
||||
setSearchStr('');
|
||||
}
|
||||
prevOpen.current = open;
|
||||
}, [open]);
|
||||
|
||||
return <React.Fragment>
|
||||
<SButton icon='search' onClick={() => setOpen(!open)} color='blue'/>
|
||||
<Modal open={open} onClose={() => setOpen(false)} centered={false}>
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ export const QueryContext = React.createContext({
|
|||
setSearchParams: null,
|
||||
updateQuery: null,
|
||||
getLocationStr: null,
|
||||
})
|
||||
});
|
||||
|
||||
export const AppMedia = createMedia({
|
||||
breakpoints: {
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ import {
|
|||
searchDirectories,
|
||||
searchVideos,
|
||||
searchZim,
|
||||
searchZims,
|
||||
setHotspot,
|
||||
setThrottle,
|
||||
} from "../api";
|
||||
|
|
@ -125,39 +124,23 @@ const removeEmptyValues = (obj) => {
|
|||
}
|
||||
|
||||
|
||||
export const useQuery = (defaultParams) => {
|
||||
export const useQuery = () => {
|
||||
const location = useLocation();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [state, setState] = React.useState(defaultParams || getSearchParamCopy(searchParams));
|
||||
|
||||
React.useEffect(() => {
|
||||
// Copy the state into the URL.
|
||||
setSearchParams(state);
|
||||
if (!_.isEmpty(state)) {
|
||||
console.debug('Changing URL params', state);
|
||||
}
|
||||
}, [JSON.stringify(state)]);
|
||||
|
||||
React.useEffect(() => {
|
||||
// Keep URL and state in sync.
|
||||
setState(getSearchParamCopy(searchParams));
|
||||
}, [JSON.stringify(searchParams)]);
|
||||
|
||||
const updateQuery = (newParams, replace = false) => {
|
||||
// Update the old state with the new values.
|
||||
setState(oldState => {
|
||||
if (replace) {
|
||||
// Ignore oldState because all values are going to be replaced.
|
||||
return removeEmptyValues(newParams);
|
||||
} else {
|
||||
return removeEmptyValues({...oldState, ...newParams});
|
||||
}
|
||||
});
|
||||
let newSearchParams = replace ?
|
||||
newParams
|
||||
: {...getSearchParamCopy(searchParams), ...newParams};
|
||||
newSearchParams = removeEmptyValues(newSearchParams);
|
||||
console.debug('useQuery.updateQuery', newSearchParams);
|
||||
setSearchParams(newSearchParams);
|
||||
};
|
||||
|
||||
const getLocationStr = (newSearchParams, pathname) => {
|
||||
// Get the current location, but with new params appended.
|
||||
newSearchParams = {...state, ...newSearchParams};
|
||||
newSearchParams = removeEmptyValues({...getSearchParamCopy(searchParams), ...newSearchParams});
|
||||
const newQuery = createSearchParams(newSearchParams);
|
||||
return `${pathname || location.pathname}?${newQuery.toString()}`
|
||||
}
|
||||
|
|
@ -167,9 +150,7 @@ export const useQuery = (defaultParams) => {
|
|||
|
||||
|
||||
export const QueryProvider = (props) => {
|
||||
const value = useQuery();
|
||||
|
||||
return <QueryContext.Provider value={value}>
|
||||
return <QueryContext.Provider value={useQuery()}>
|
||||
{props.children}
|
||||
</QueryContext.Provider>
|
||||
}
|
||||
|
|
@ -519,16 +500,17 @@ export const useSearchFiles = (defaultLimit = 48, emptySearch = false, model) =>
|
|||
}
|
||||
const mimetypes = filterToMimetypes(filter);
|
||||
setSearchFiles(null);
|
||||
let fromYear;
|
||||
let toYear;
|
||||
let fromDate;
|
||||
let toDate;
|
||||
if (dateRange) {
|
||||
fromYear = dateRange[0];
|
||||
toYear = dateRange[1];
|
||||
fromDate = dateRange[0];
|
||||
toDate = dateRange[1];
|
||||
}
|
||||
console.log('localSearchFiles', fromDate, toDate, months);
|
||||
try {
|
||||
let [file_groups, total] = await filesSearch(
|
||||
pages.offset, pages.limit, searchStr, mimetypes, model || model_, activeTags, headline,
|
||||
months, fromYear, toYear);
|
||||
months, fromDate, toDate);
|
||||
setSearchFiles(file_groups);
|
||||
pages.setTotal(total);
|
||||
} catch (e) {
|
||||
|
|
@ -983,31 +965,29 @@ export const useSearchOrder = () => {
|
|||
return {sort, setSort}
|
||||
}
|
||||
|
||||
export const useSearchMonths = () => {
|
||||
export const useSearchDate = () => {
|
||||
// Filters files using
|
||||
// ?month=1&month=2&month=3
|
||||
// and
|
||||
// ?fromDate=2023&toDate=2024
|
||||
const {searchParams, updateQuery} = React.useContext(QueryContext);
|
||||
const months = searchParams.getAll('month');
|
||||
|
||||
const setMonths = (newMonths) => {
|
||||
// Set new months, go back to first page.
|
||||
updateQuery({'month': newMonths, 'o': 0});
|
||||
}
|
||||
|
||||
return {months, setMonths}
|
||||
}
|
||||
|
||||
export const useSearchDateRange = () => {
|
||||
const {searchParams, updateQuery} = React.useContext(QueryContext);
|
||||
let fromDate = searchParams.get('fromDate');
|
||||
let toDate = searchParams.get('toDate');
|
||||
fromDate = fromDate ? parseInt(fromDate) : null;
|
||||
toDate = toDate ? parseInt(toDate) : null;
|
||||
|
||||
const setDateRange = ([newFromDate, newToDate]) => {
|
||||
// Set new date range, go back to first page.
|
||||
updateQuery({fromDate: newFromDate, toDate: newToDate, 'o': 0});
|
||||
const clearDate = () => {
|
||||
console.log('clearDate');
|
||||
updateQuery({fromDate: null, toDate: null, month: null, 'o': 0});
|
||||
}
|
||||
|
||||
return {dateRange: [fromDate, toDate], setDateRange}
|
||||
const setDates = (newFromDate, newToDate, newMonths) => {
|
||||
console.log('setDates', newFromDate, newToDate, newMonths);
|
||||
updateQuery({fromDate: newFromDate, toDate: newToDate, month: newMonths, 'o': 0});
|
||||
}
|
||||
|
||||
return {dateRange: [fromDate, toDate], setDates, months, clearDate}
|
||||
}
|
||||
|
||||
export const useUploadFile = () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue