generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ae6d4b
commit 94ce82d
Showing
6 changed files
with
207 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import React from 'react'; | ||
|
||
import { useAtom } from 'jotai'; | ||
import { SearchDriver } from '@elastic/search-ui'; | ||
|
||
import useWhyDidYouUpdate from '@eeacms/search/lib/hocs/useWhyDidYouUpdate'; | ||
import { | ||
getDefaultFilters, | ||
rebind, | ||
applyConfigurationSchema, | ||
} from '@eeacms/search/lib/utils'; | ||
import { | ||
onResultClick, | ||
onAutocompleteResultClick, | ||
} from '@eeacms/search/lib/request'; | ||
|
||
import { resetFilters, resetSearch } from './request'; | ||
import useFacetsWithAllOptions from './useFacetsWithAllOptions'; | ||
import { loadingFamily } from './state'; | ||
|
||
function useSearchApp({ | ||
appName, | ||
registry, | ||
paramOnSearch, | ||
paramOnAutocomplete, | ||
}) { | ||
const appConfig = React.useMemo( | ||
() => ({ | ||
...applyConfigurationSchema(rebind(registry.searchui[appName])), | ||
appName, | ||
}), | ||
[appName, registry], | ||
); | ||
|
||
const loadingAtom = loadingFamily(appName); | ||
const [, setIsLoading] = useAtom(loadingAtom); | ||
|
||
const onSearch = React.useCallback( | ||
async (state) => { | ||
setIsLoading(true); | ||
console.log('searching', state); | ||
const res = await paramOnSearch(appConfig)(state); | ||
console.log('search done', res); | ||
setIsLoading(false); | ||
return res; | ||
}, | ||
[appConfig, paramOnSearch, setIsLoading], | ||
); | ||
|
||
const onAutocomplete = React.useMemo(() => paramOnAutocomplete(appConfig), [ | ||
appConfig, | ||
paramOnAutocomplete, | ||
]); | ||
|
||
const locationSearchTerm = React.useMemo( | ||
() => | ||
typeof window !== 'undefined' | ||
? new URLSearchParams(window.location.search).get('q') | ||
: null, | ||
[], | ||
); | ||
|
||
const elasticConfig = React.useMemo( | ||
() => ({ | ||
...appConfig, | ||
onResultClick, | ||
onAutocompleteResultClick, | ||
onAutocomplete, | ||
onSearch, | ||
initialState: { | ||
resultsPerPage: appConfig.resultsPerPage || 20, | ||
...(locationSearchTerm | ||
? { filters: getDefaultFilters(appConfig) } | ||
: {}), | ||
}, | ||
// debug: true, | ||
}), | ||
[appConfig, onAutocomplete, onSearch, locationSearchTerm], | ||
); | ||
|
||
const { facetOptions } = React.useState(useFacetsWithAllOptions(appConfig)); | ||
|
||
const [driverInstance] = React.useState( | ||
__CLIENT__ ? new SearchDriver(elasticConfig) : null, | ||
); | ||
|
||
const mapContextToProps = React.useCallback( | ||
(params) => { | ||
const driver = driverInstance; | ||
const searchContext = { | ||
...params, | ||
driver, | ||
facetOptions, | ||
}; | ||
searchContext.resetFilters = resetFilters.bind({ | ||
appConfig, | ||
searchContext, | ||
}); | ||
searchContext.resetSearch = resetSearch.bind({ | ||
appConfig, | ||
searchContext, | ||
driver, | ||
}); | ||
return searchContext; | ||
}, | ||
[appConfig, driverInstance, facetOptions], | ||
); | ||
|
||
useWhyDidYouUpdate('BasicSearchApp', { | ||
appName, | ||
registry, | ||
appConfig, | ||
driverInstance, | ||
facetOptions, | ||
onAutocomplete, | ||
onSearch, | ||
locationSearchTerm, | ||
}); | ||
|
||
return { | ||
facetOptions, | ||
mapContextToProps, | ||
appConfig, | ||
driverInstance, | ||
elasticConfig, | ||
}; | ||
} | ||
|
||
export default useSearchApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import React from 'react'; | ||
|
||
export default function useWhyDidYouUpdate(name, props) { | ||
// Get a mutable ref object where we can store props ... | ||
// ... for comparison next time this hook runs. | ||
const previousProps = React.useRef(); | ||
React.useEffect(() => { | ||
if (previousProps.current) { | ||
// Get all keys from previous and current props | ||
const allKeys = Object.keys({ ...previousProps.current, ...props }); | ||
// Use this object to keep track of changed props | ||
const changesObj = {}; | ||
// Iterate through keys | ||
allKeys.forEach((key) => { | ||
// If previous is different from current | ||
if (previousProps.current[key] !== props[key]) { | ||
// Add to changesObj | ||
changesObj[key] = { | ||
from: previousProps.current[key], | ||
to: props[key], | ||
}; | ||
} | ||
}); | ||
// If changesObj not empty then output to console | ||
if (Object.keys(changesObj).length) { | ||
console.log('[why-did-you-update]', name, changesObj); | ||
} | ||
} | ||
// Finally update previousProps with current props for next hook call | ||
previousProps.current = props; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.