Skip to content

Commit

Permalink
WIP on splitting components
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Dec 19, 2022
1 parent 341bb5e commit a890e6c
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 19 deletions.
11 changes: 6 additions & 5 deletions searchlib/components/SearchApp/BasicSearchApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ function SearchWrappers(SearchViewComponent) {
...searchContext
} = props;

const [payload, update] = React.useState(appConfigContext);

return (
<AppConfigContext.Provider value={appConfigContext}>
<AppConfigContext.Provider value={{ payload, update }}>
<SearchContext.Provider value={searchContext}>
<SearchViewComponent
{...searchContext}
Expand Down Expand Up @@ -63,10 +65,9 @@ export default function BasicSearchApp(props) {
paramOnAutocomplete,
});

const appConfigContext = React.useMemo(() => ({ appConfig, registry }), [
appConfig,
registry,
]);
const appConfigContext = React.useMemo(() => {
return { appConfig, registry };
}, [appConfig, registry]);

const WrappedSearchView = React.useMemo(() => {
return withSearch(mapContextToProps)(SearchWrappers(searchViewComponent));
Expand Down
37 changes: 35 additions & 2 deletions searchlib/components/SearchApp/SearchInputApp.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import React from 'react';

import registry from '@eeacms/search/registry';
import { AppConfigContext } from '@eeacms/search/lib/hocs';

import BasicSearchApp from './BasicSearchApp';
import { SearchBox } from '@eeacms/search/components';

const SearchInputView = (props) => {
const { mode = 'view', appConfig, ...searchContext } = props;
const {
mode = 'view',
appConfigContext,
appConfig,
...searchContext
} = props;

const onSubmit = appConfig.url
? (searchTerm) => {
console.log('submit', searchTerm);
}
: undefined;

const onSelectAutocomplete = appConfig.url
? (selection, options) => {
console.log('onselect', selection, options, searchContext);
searchContext.setSearchTerm(selection.suggestion);
}
: undefined;

return (
<SearchBox
searchContext={searchContext}
Expand All @@ -26,10 +47,22 @@ const SearchInputView = (props) => {
: undefined
}
mode={mode}
onSubmit={onSubmit}
onSelectAutocomplete={onSelectAutocomplete}
/>
);
};

const SearchInputViewWrapper = (props) => {
return (
<AppConfigContext.Consumer>
{(context) => <SearchInputView appConfigContext={context} {...props} />}
</AppConfigContext.Consumer>
);
};

export default function SearchApp(props) {
return <BasicSearchApp {...props} searchViewComponent={SearchInputView} />;
return (
<BasicSearchApp {...props} searchViewComponent={SearchInputViewWrapper} />
);
}
4 changes: 3 additions & 1 deletion searchlib/components/SearchApp/SearchResultsApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,14 @@ function SearchResultsView(props) {
) : (
<NoResultsComponent {...props} />
)
) : (
) : results.length ? (
<ContentBodyView {...props}>
{results.map((result, i) => (
<Item key={`${i}-${result.id}`} result={result} {...itemViewProps} />
))}
</ContentBodyView>
) : (
<div className="noResults"></div>
);
}

Expand Down
7 changes: 5 additions & 2 deletions searchlib/lib/hocs/appConfig.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import React from 'react';

export const AppConfigContext = React.createContext(null);
export const AppConfigContext = React.createContext({
payload: {},
update: () => {},
});

export const useAppConfig = () => {
const context = React.useContext(AppConfigContext);
Expand All @@ -12,7 +15,7 @@ export const useAppConfig = () => {
);
}

return context;
return context.payload;
};

export const withAppConfig = (WrappedComponent) => {
Expand Down
2 changes: 0 additions & 2 deletions searchlib/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,4 @@ if (typeof window !== 'undefined') {
window.searchUiConfig = config;
}

console.log('whole config', config);

export default config;
11 changes: 7 additions & 4 deletions src/SearchBlock/SearchBlockView.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import config from '@plone/volto/registry';
import { SearchBlockSchema } from './schema';
import { withBlockExtensions } from '@plone/volto/helpers';
import { applySchemaEnhancer, withBlockExtensions } from '@plone/volto/helpers';
import { applyBlockSettings } from './utils';
import { isEqual } from 'lodash';

Expand All @@ -27,12 +27,15 @@ const useDebouncedStableData = (data, timeout = 100) => {

function SearchBlockView(props) {
const { data = {}, mode = 'view', variation } = props;
const schema = React.useMemo(() => SearchBlockSchema({ formData: data }), [
data,
]);
const { appName = 'default' } = data;
const stableData = useDebouncedStableData(data);

const schema = React.useMemo(() => {
let schema = SearchBlockSchema({ formData: stableData });
schema = applySchemaEnhancer({ schema, formData: stableData });
return schema;
}, [stableData]);

const registry = React.useMemo(() => {
const reg = applyBlockSettings(
config.settings.searchlib,
Expand Down
10 changes: 10 additions & 0 deletions src/SearchBlock/templates/LandingPageView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,14 @@ function LandingPageView(props) {
return <LandingPageApp {...props} />;
}

LandingPageView.schemaEnhancer = ({ schema }) => {
schema.fieldsets[0].fields.push('url');
schema.properties.url = {
title: 'Results page',
widget: 'url',
};

return schema;
};

export default LandingPageView;
11 changes: 11 additions & 0 deletions src/SearchBlock/templates/SearchInputView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,15 @@ function SearchInputView(props) {
return <SearchInputApp {...props} />;
}

SearchInputView.schemaEnhancer = ({ schema }) => {
schema.fieldsets[0].fields.push('url');
schema.properties.url = {
title: 'Results page',
widget: 'url',
configPath: 'url',
};

return schema;
};

export default SearchInputView;
8 changes: 5 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,19 @@ const applyConfig = (config) => {
},
{
id: 'searchInputOnly',
title: 'Only Search input',
title: 'Search input',
view: SearchInputView,
schemaEnhancer: SearchInputView.schemaEnhancer,
},
{
id: 'landingPageOnly',
title: 'Only statistics',
title: 'Statistics',
view: LandingPageView,
schemaEnhancer: LandingPageView.schemaEnhancer,
},
{
id: 'searchResultsOnly',
title: 'Only search results',
title: 'Search results',
view: SearchResultsView,
},
],
Expand Down

0 comments on commit a890e6c

Please sign in to comment.