Skip to content

Commit

Permalink
Allow modification of schema
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Oct 4, 2021
1 parent c2c35a4 commit 3744f0d
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 35 deletions.
52 changes: 49 additions & 3 deletions src/SearchBlock/SearchBlockView.jsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,60 @@
import React from 'react';
import config from '@plone/volto/registry';
import { SearchApp } from '@eeacms/search';
import { SearchBlockSchema } from './schema';

import '@elastic/react-search-ui-views/lib/styles/styles.css';
import './styles.less';

export default function SearchBlockView({ data = {} }) {
/**
* Reuse the schema to allow pinpointing in the config, to allow adjusting the
* data based on the schema definition.
*
* There's two styles of pinpointing:
*
* - with `configPath` you can use a dotted path to the key name
* - with `modifyConfig` you can write a function that can change the settings
*
*/
const applyBlockSettings = (config, appName, data, schema) => {
// apply mutations inline to the config
const settings = config.searchui[appName];
Object.keys(data).forEach((fieldName) => {
const field = schema.properties[fieldName];
if (!field) return;
const { configPath, modifyConfig } = field;
if (modifyConfig) {
modifyConfig(settings, data[fieldName]);
} else if (!configPath) {
return config;
} else {
const [key, ...bits] = configPath.split('.').reverse();
bits.reverse();
let branch = settings;
bits.forEach((bit) => {
if (Object.keys(branch).indexOf(bit) === -1) {
branch[bit] = {};
}
branch = branch[bit];
});
console.log('set', { branch, key, field: data[fieldName], fieldName });
branch[key] = data[fieldName];
}
});
return config;
};

export default function SearchBlockView(props) {
const { data = {} } = props;
const schema = SearchBlockSchema(props);
const { appName = 'default' } = data;
const registry = config.settings.searchlib;
console.log(config.settings.searchlib);
const registry = applyBlockSettings(
config.settings.searchlib,
appName,
data,
schema,
);
console.log('registry', registry);
return (
<div className="searchlib-block">
<SearchApp registry={registry} appName={appName} />
Expand Down
86 changes: 54 additions & 32 deletions src/SearchBlock/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,69 @@ export const SearchBlockSchema = () => ({
{
id: 'default',
title: 'Default',
fields: [
'appName',
// 'es_index'
],
fields: ['appName'],
},
{
id: 'nlp',
title: 'NLP Settings',
fields: ['use_qa_dp', 'qa_queryTypes'],
},
// {
// id: 'tile',
// title: 'Tile rendering',
// fields: ['tile_title', 'tile_description', 'tile_url', 'tile_image'],
// },
],

properties: {
appName: {
title: 'Searchlib app',
choices: [],
},
// url: {
// title: 'ES URL',
// default: '',
// },
// es_index: {
// widget: 'elasticsearch_select_index',
// },
// tile_title: {
// title: 'Title field',
// widget: 'elasticsearch_select_field',
// },
// tile_description: {
// title: 'Description field',
// widget: 'elasticsearch_select_field',
// },
// tile_url: {
// title: 'Primary URL field',
// widget: 'elasticsearch_select_field',
// },
// tile_image: {
// title: 'Image field',
// widget: 'elasticsearch_select_field',
// },
use_qa_dp: {
title: 'Use DeepPassageRetrieval for QA?',
description:
'If enabled, it will use DPR for basic query retrieval instead of ES BM25',
type: 'boolean',
configPath: 'nlp.qa.use_dp',
},
qa_queryTypes: {
title: 'QA Query types',
description: 'The QA system will be used for these types of queries',
choices: [
['query:interrogative', 'Question'],
['query:declarative', 'Statement'],
['query:keyword', 'Keyword'],
],
configPath: 'nlp.qa.qa_queryTypes',
isMulti: true,
// default: ['query:interrogative']
},
},

required: ['appName'],
});

// {
// id: 'tile',
// title: 'Tile rendering',
// fields: ['tile_title', 'tile_description', 'tile_url', 'tile_image'],
// },
// url: {
// title: 'ES URL',
// default: '',
// },
// es_index: {
// widget: 'elasticsearch_select_index',
// },
// tile_title: {
// title: 'Title field',
// widget: 'elasticsearch_select_field',
// },
// tile_description: {
// title: 'Description field',
// widget: 'elasticsearch_select_field',
// },
// tile_url: {
// title: 'Primary URL field',
// widget: 'elasticsearch_select_field',
// },
// tile_image: {
// title: 'Image field',
// widget: 'elasticsearch_select_field',
// },
4 changes: 4 additions & 0 deletions src/SearchBlock/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,7 @@ button.download-btn {
}
}
}

.answers-list .search-result {
font-size: 80%;
}

0 comments on commit 3744f0d

Please sign in to comment.