-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from oarepo/mirekys-search-app-init
feat(search): createSearchAppInit utility & dynamic result items
- Loading branch information
Showing
8 changed files
with
140 additions
and
7 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
35 changes: 35 additions & 0 deletions
35
oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/search/DynamicResultsListItem.jsx
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,35 @@ | ||
import React from "react"; | ||
import _get from "lodash/get"; | ||
import Overridable, { overrideStore } from "react-overridable"; | ||
import { AppContext } from "react-searchkit"; | ||
|
||
export const FallbackItemComponent = ({ result }) => ( | ||
<div> | ||
<h2>{result.id}</h2> | ||
</div> | ||
); | ||
|
||
export const DynamicResultsListItem = ({ | ||
result, | ||
selector = "$schema", | ||
FallbackComponent = FallbackItemComponent, | ||
}) => { | ||
const { buildUID } = React.useContext(AppContext); | ||
const selectorValue = _get(result, selector); | ||
|
||
if (!selectorValue) { | ||
console.warn("Result", result, `is missing value for '${selector}'.`); | ||
return <FallbackComponent result={result} />; | ||
} | ||
|
||
return ( | ||
<Overridable | ||
id={buildUID("ResultsList.item", selectorValue)} | ||
result={result} | ||
> | ||
<FallbackComponent result={result} /> | ||
</Overridable> | ||
); | ||
}; | ||
|
||
export default DynamicResultsListItem; |
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
82 changes: 82 additions & 0 deletions
82
oarepo_ui/theme/assets/semantic-ui/js/oarepo_ui/search/util.js
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,82 @@ | ||
import React from "react"; | ||
import { SearchApp } from "@js/invenio_search_ui/components"; | ||
import { loadComponents } from "@js/invenio_theme/templates"; | ||
import _camelCase from "lodash/camelCase"; | ||
import ReactDOM from "react-dom"; | ||
import { parametrize } from 'react-overridable' | ||
|
||
import { | ||
ActiveFiltersElement, | ||
BucketAggregationElement, | ||
BucketAggregationValuesElement, | ||
CountElement, | ||
EmptyResultsElement, | ||
ErrorElement, | ||
SearchAppFacets, | ||
SearchAppLayout, | ||
SearchAppResultOptions, | ||
SearchAppSearchbarContainer, | ||
SearchFiltersToggleElement, | ||
SearchAppSort, | ||
} from "@js/oarepo_ui/search"; | ||
|
||
|
||
|
||
export function createSearchAppInit ({ | ||
defaultComponentOverrides, | ||
autoInit = true, | ||
autoInitDataAttr = "invenio-search-config", | ||
multi = true, | ||
ContainerComponent = React.Fragment, | ||
}) { | ||
const initSearchApp = (rootElement) => { | ||
const { appId, ...config } = JSON.parse( | ||
rootElement.dataset[_camelCase(autoInitDataAttr)] | ||
); | ||
|
||
const componentPrefix = multi ? `${appId}.` : '' | ||
const SearchAppSearchbarContainerWithConfig = parametrize(SearchAppSearchbarContainer, { appName: appId }) | ||
const internalComponentDefaults = { | ||
[`${componentPrefix}ActiveFilters.element`]: ActiveFiltersElement, | ||
[`${componentPrefix}BucketAggregation.element`]: BucketAggregationElement, | ||
[`${componentPrefix}BucketAggregationValues.element`]: BucketAggregationValuesElement, | ||
[`${componentPrefix}Count.element`]: CountElement, | ||
[`${componentPrefix}EmptyResults.element`]: EmptyResultsElement, | ||
[`${componentPrefix}Error.element`]: ErrorElement, | ||
[`${componentPrefix}SearchApp.facets`]: SearchAppFacets, | ||
[`${componentPrefix}SearchApp.layout`]: SearchAppLayout, | ||
[`${componentPrefix}SearchApp.resultOptions`]: SearchAppResultOptions, | ||
[`${componentPrefix}SearchApp.searchbarContainer`]: SearchAppSearchbarContainerWithConfig, | ||
[`${componentPrefix}SearchFilters.Toggle.element`]: SearchFiltersToggleElement, | ||
[`${componentPrefix}SearchApp.sort`]: SearchAppSort, | ||
}; | ||
|
||
loadComponents(appId, { | ||
...internalComponentDefaults, | ||
...config.defaultComponents, | ||
...defaultComponentOverrides, | ||
}).then((res) => { | ||
ReactDOM.render( | ||
<ContainerComponent> | ||
<SearchApp | ||
config={config} | ||
// Use appName to namespace application components when overriding | ||
{...(multi && { appName: appId })} | ||
/> | ||
</ContainerComponent>, | ||
rootElement | ||
); | ||
}); | ||
}; | ||
|
||
if (autoInit) { | ||
const searchAppElements = document.querySelectorAll( | ||
`[data-${autoInitDataAttr}]` | ||
); | ||
for (const appRootElement of searchAppElements) { | ||
initSearchApp(appRootElement); | ||
} | ||
} else { | ||
return initSearchApp; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,8 +1,9 @@ | ||
{#def extra_context, ui_links #} | ||
{#def extra_context, ui_links, search_app_config #} | ||
{ | ||
"ui_links": {{ ui_links|tojson|safe }}, | ||
"permissions": {{ extra_context.permissions|tojson|safe }}, | ||
"dummy_filter": {{ 1|dummy|tojson|safe }} | ||
"dummy_filter": {{ 1|dummy|tojson|safe }}, | ||
"search_config": {{ search_app_config|tojson|safe }} | ||
} | ||
|
||
|
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,11 @@ | ||
import json | ||
|
||
|
||
def test_default_components( | ||
app, record_ui_resource, simple_record, client_with_credentials, fake_manifest | ||
): | ||
with client_with_credentials.get(f"/simple-model/") as c: | ||
txt = json.loads(c.text) | ||
search_config = txt["search_config"] | ||
|
||
assert search_config['defaultComponents'] == {} |