Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple Instant Results from the Modal #3159

Merged
merged 59 commits into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
36143e7
Support multiple Facets
goaround Dec 15, 2020
61d3fb0
Update facets.js
goaround Jan 21, 2021
afe9eb1
Merge branch 'develop'
Jan 28, 2021
e706d9f
Merge branch 'develop'
Jan 28, 2021
d7acb69
Update facets.js
goaround Feb 1, 2021
c12f3d4
Add facetTerms type and description
goaround Feb 1, 2021
f3b5640
Include dist files
goaround Feb 1, 2021
86d56f9
Update dist
goaround Feb 1, 2021
a35e0dd
Merge pull request #2004 from goaround/patch-5
brandwaffle Feb 3, 2021
a8c59b5
Revert "Support multiple Facets"
jeffpaul Feb 3, 2021
d3c8db6
Merge pull request #2067 from 10up/revert-2004-patch-5
brandwaffle Feb 3, 2021
68b24a8
Merge branch 'develop'
Feb 12, 2021
c47fbaf
Merge pull request #2108 from 10up/develop
brandwaffle Feb 25, 2021
ad70299
Merge pull request #2141 from 10up/develop
brandwaffle Mar 18, 2021
c2819dc
Merge branch 'develop'
felipeelia Jul 7, 2021
c47cfcb
Merge pull request #2259 from 10up/develop
jeffpaul Jul 7, 2021
c362484
Merge pull request #2268 from 10up/develop
felipeelia Jul 15, 2021
84b6ebd
Merge pull request #2270 from 10up/master
felipeelia Jul 15, 2021
7c9b6d3
Merge branch 'develop'
felipeelia Aug 26, 2021
b64d092
Merge branch 'develop'
felipeelia Aug 26, 2021
62b33be
Merge pull request #2330 from 10up/develop
felipeelia Aug 26, 2021
0cdd808
Merge branch 'develop'
felipeelia Sep 29, 2021
003fc2f
Merge branch 'develop'
felipeelia Oct 26, 2021
5af0a95
Merge branch 'develop'
felipeelia Nov 30, 2021
9bf85bf
Merge branch 'master' into trunk
felipeelia Nov 30, 2021
3dc6e46
Merge pull request #2505 from 10up/chore/ep-4-beta-1-notice
felipeelia Dec 8, 2021
d5c6d19
Merge branch 'develop'
felipeelia Dec 20, 2021
a3dc797
Merge branch 'develop'
felipeelia Dec 20, 2021
946acda
Merge branch 'master' into trunk
felipeelia Dec 20, 2021
ce167e8
Merge branch 'develop' into trunk
felipeelia Mar 8, 2022
f6bfde7
Adjust command to match repo's slug
felipeelia Mar 8, 2022
d6b286e
Merge branch 'develop' into trunk
felipeelia Mar 8, 2022
1e6a8ee
revert back the plugin name
felipeelia Mar 8, 2022
736dd48
Merge branch 'develop' into trunk
felipeelia Mar 16, 2022
cb6e43f
Merge branch 'develop' into trunk
felipeelia Apr 5, 2022
ef0a12c
Merge branch 'develop' into trunk
felipeelia May 26, 2022
a01c9a3
Merge pull request #2804 from 10up/develop
felipeelia May 26, 2022
145f577
Merge branch 'develop' into trunk
felipeelia Jun 28, 2022
71d23cc
Merge branch 'develop' into trunk
felipeelia Jul 14, 2022
fd9afcc
Merge branch 'develop' into trunk
felipeelia Aug 31, 2022
9d4ee0c
Merge branch 'develop' into trunk
felipeelia Sep 27, 2022
95d1cc0
Merge branch 'feature/2527' into feature/2979
JakePT Oct 11, 2022
b4511c7
WIP decoupling Instant Results from the Modal.
JakePT Oct 12, 2022
de69ae2
WIP decoupling Instant Results from the Modal.
JakePT Oct 18, 2022
89b798a
Merge branch 'develop' into feature/2979
JakePT Nov 21, 2022
ea78aca
Merge branch 'develop' into feature/2979
JakePT Nov 25, 2022
bd751c7
Reimplement reducer.
JakePT Nov 25, 2022
f34cbe5
Remove console.
JakePT Nov 25, 2022
1bb4bb6
Merge branch 'develop' into trunk
felipeelia Nov 29, 2022
e6ecd8e
Merge branch 'develop' into feature/2979
JakePT Dec 2, 2022
a352391
Rename Instant Results provider to ApiSearchProvider.
JakePT Dec 8, 2022
e66dec4
Merge branch 'develop' into trunk
felipeelia Jan 10, 2023
5f378be
Merge branch 'develop' into feature/2979
JakePT Jan 12, 2023
e63c972
WIP: Fix history behaviour.
JakePT Jan 18, 2023
513be83
Merge branch 'trunk' into feature/2979
JakePT Jan 18, 2023
a105dc4
Consistent import ordering and doc spacing.
JakePT Jan 18, 2023
51d4ae6
Reorganise API search library.
JakePT Jan 18, 2023
21072bf
Refinements.
JakePT Jan 19, 2023
dee13ba
Fix popping state.
JakePT Jan 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refinements.
  • Loading branch information
JakePT committed Jan 19, 2023
commit 21072bff53a99867db6ef4161281502a93e15d64
185 changes: 85 additions & 100 deletions assets/js/api-search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,7 @@ const Context = createContext();
*/
export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children, paramPrefix }) => {
/**
* Get default args from the schema.
*/
const defaultArgsFromSchema = useMemo(() => {
return getDefaultArgsFromSchema(argsSchema);
}, [argsSchema]);

/**
* Get any default args from the UR:.
* Any default args from the URL.
*/
const defaultArgsFromUrl = useMemo(() => {
if (!paramPrefix) {
Expand All @@ -59,30 +52,49 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
return getArgsFromUrlParams(argsSchema, paramPrefix);
}, [argsSchema, paramPrefix]);

/**
* All default args including defaults from the schema.
*/
const defaultArgs = useMemo(() => {
const defaultArgsFromSchema = getDefaultArgsFromSchema(argsSchema);

return {
...defaultArgsFromSchema,
...defaultArgsFromUrl,
};
}, [argsSchema, defaultArgsFromUrl]);

/**
* Whether the provider is "on" by default.
*/
const defaultIsOn = useMemo(() => {
return Object.keys(defaultArgsFromUrl).length > 0;
}, [defaultArgsFromUrl]);

/**
* Set up fetch method.
*/
const fetchResults = useFetchResults(apiHost, apiEndpoint);

/**
* Set up the reducer.
*/
const [state, dispatch] = useReducer(reducer, {
aggregations: {},
args: {
...defaultArgsFromSchema,
...defaultArgsFromUrl,
},
args: defaultArgs,
argsSchema,
isLoading: false,
isOn: Object.keys(defaultArgsFromUrl).length > 0,
isOn: defaultIsOn,
isPoppingState: false,
searchResults: [],
searchedTerm: '',
totalResults: 0,
});

/**
* Set up fetch method.
*/
const fetchResults = useFetchResults(apiHost, apiEndpoint);

/**
* Create state ref.
*
* Helps to avoid dependency hell.
*/
const stateRef = useRef(state);

Expand All @@ -94,37 +106,28 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
* @returns {void}
*/
const clearConstraints = useCallback(() => {
dispatch({
type: 'CLEAR_CONSTRAINTS',
argsSchema,
});
}, [argsSchema]);
dispatch({ type: 'CLEAR_CONSTRAINTS' });
}, []);

/**
* Set search args.
* Update the search query args, triggering a search.
*
* @param {object} args Search args.
* @returns {void}
*/
const search = useCallback((args) => {
dispatch({
type: 'SEARCH',
args,
});
dispatch({ type: 'SEARCH', args });
}, []);

/**
* Reset search args with new search term.
* Update the search term, triggering a search and resetting facet
* constraints.
*
* @param {string} searchTerm Search term.
* @returns {void}
*/
const searchFor = (searchTerm) => {
dispatch({
type: 'SEARCH_FOR',
argsSchema,
searchTerm,
});
dispatch({ type: 'SEARCH_FOR', searchTerm });
};

/**
Expand All @@ -134,10 +137,7 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
* @returns {void}
*/
const setIsLoading = (isLoading) => {
dispatch({
type: 'SET_IS_LOADING',
isLoading,
});
dispatch({ type: 'SET_IS_LOADING', isLoading });
};

/**
Expand All @@ -147,32 +147,25 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
* @returns {void}
*/
const setResults = (response) => {
dispatch({
type: 'SET_RESULTS',
response,
});
dispatch({ type: 'SET_RESULTS', response });
};

/**
* Load the next page of search results.
* Go to the next page of search results.
*
* @returns {void}
*/
const nextPage = () => {
dispatch({
type: 'NEXT_PAGE',
});
dispatch({ type: 'NEXT_PAGE' });
};

/**
* Load the previous page of search results.
* Go to the previous page of search results.
*
* @returns {void}
*/
const previousPage = () => {
dispatch({
type: 'PREVIOUS_PAGE',
});
dispatch({ type: 'PREVIOUS_PAGE' });
};

/**
Expand All @@ -181,14 +174,20 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
* @param {object} args Search args.
*/
const popState = (args) => {
dispatch({
type: 'POP_STATE',
args,
});
dispatch({ type: 'POP_STATE', args });
};

/**
* Turn off the provider.
*
* @returns {void}
*/
const turnOff = () => {
dispatch({ type: 'TURN_OFF' });
};

/**
* Push args state to history.
* Push search args to browser history.
*
* @returns {void}
*/
Expand All @@ -198,29 +197,24 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
}

const { args, isOn } = stateRef.current;
const state = { ...args, isOn };
const state = { args, isOn };

if (window.history.state) {
const params = isOn ? getUrlParamsFromArgs(args, argsSchema, paramPrefix) : null;
const url = getUrlWithParams(paramPrefix, params);
if (isOn) {
const params = getUrlParamsFromArgs(args, argsSchema, paramPrefix);
const url = getUrlWithParams(paramPrefix, params);

window.history.pushState(state, document.title, url);
window.history.pushState(state, document.title, url);
} else {
const url = getUrlWithParams(paramPrefix);

window.history.pushState(state, document.title, url);
}
} else {
window.history.replaceState(state, document.title, window.location.href);
}
}, [argsSchema, paramPrefix]);

/**
* Close search.
*
* @returns {void}
*/
const turnOff = () => {
dispatch({
type: 'TURN_OFF',
});
};

/**
* Handle popstate event.
*
Expand All @@ -242,11 +236,24 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,
);

/**
* Handle search.
* Handle initialization.
*
* @returns {Function} A cleanup function.
*/
const handleInit = useCallback(() => {
window.addEventListener('popstate', onPopState);

return () => {
window.removeEventListener('popstate', onPopState);
};
}, [onPopState]);

/**
* Handle a change to search args.
*
* @returns {void}
*/
const onSearch = useCallback(async () => {
const handleSearch = useCallback(() => {
const { args, isOn, isPoppingState } = stateRef.current;

if (!isPoppingState) {
Expand All @@ -261,40 +268,18 @@ export const ApiSearchProvider = ({ apiEndpoint, apiHost, argsSchema, children,

setIsLoading(true);

const response = await fetchResults(urlParams);

setResults(response);
setIsLoading(false);
fetchResults(urlParams).then((response) => {
setResults(response);
setIsLoading(false);
});
}, [argsSchema, fetchResults, pushState]);

/**
* Bind events to outside elements.
*
* @returns {Function} A cleanup function that unbinds the events.
*/
const handleInit = () => {
window.addEventListener('popstate', onPopState);

return () => {
window.removeEventListener('popstate', onPopState);
};
};

/**
* Perform a search.
*
* @returns {void}
*/
const handleSearch = () => {
onSearch();
};

/**
* Effects.
*/
useEffect(handleInit, [argsSchema, onPopState, paramPrefix, search]);
useEffect(handleInit, [handleInit]);
useEffect(handleSearch, [
onSearch,
handleSearch,
state.args,
state.args.orderby,
state.args.order,
Expand Down
4 changes: 2 additions & 2 deletions assets/js/api-search/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default (state, action) => {

switch (action.type) {
case 'CLEAR_CONSTRAINTS': {
const clearedArgs = getArgsWithoutConstraints(newState.args, action.argsSchema);
const clearedArgs = getArgsWithoutConstraints(newState.args, newState.argsSchema);

newState.args = clearedArgs;
newState.args.offset = 0;
Expand All @@ -28,7 +28,7 @@ export default (state, action) => {
break;
}
case 'SEARCH_FOR': {
const clearedArgs = getArgsWithoutConstraints(newState.args, action.argsSchema);
const clearedArgs = getArgsWithoutConstraints(newState.args, newState.argsSchema);

newState.args = clearedArgs;
newState.args.search = action.searchTerm;
Expand Down