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

WIP: Feature/mapping weighting dashboard #2588

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
113 changes: 106 additions & 7 deletions assets/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -693,18 +693,101 @@ h2.ep-list-features {
* Weighting
*/

.weighting-settings .postbox {
.weighting-settings {
box-sizing: border-box;
max-width: 650px;

& * {
box-sizing: border-box;
& .postbox {

& .components-range-control__number {
display: none;
}

& .postbox__header {
align-items: center;
border-bottom: 1px solid #c3c4c7;
display: flex;
}

& .undo-changes {
background-color: transparent;
border: 0;
color: #1e8cbe;
cursor: pointer;
margin-left: auto;
padding: 0 10px;

&:hover {
text-decoration: underline;
}
}

& * {
box-sizing: border-box;
}
}

& .add-meta-key-wrap {
align-items: center;
display: flex;

& legend {
top: 0;
}
}

& .add-meta-key {
width: 300px;
}

& .submit__button {
align-items: center;
background: #fff;
bottom: 0;
display: flex;
padding: 10px;
position: sticky;

& .note {
margin-left: 10px;
}

& .reset-all-changes-button {
margin-left: auto;
}
}

}

.components-range-control__wrapper {
min-width: 100px;

& span:last-child {
background-color: #1e8cbe;
pointer-events: none;
}
}

div.components-base-control__field {
align-items: center;
display: flex;
height: 20px;
margin: 0;

& .components-base-control__label {
margin: 0;
}
}

.field-item .components-base-control__help {
margin-top: -10px;
padding: 0;
}

.weighting-settings .postbox h2.hndle {
color: #444;
cursor: inherit;
width: 110px;
}

.weighting-settings fieldset {
Expand Down Expand Up @@ -737,9 +820,26 @@ h2.ep-list-features {
background: #f9f9f9;
}

.weighting-settings .searchable {
.field-item {
align-items: center;
display: flex;
}

.remove-meta-item {
cursor: pointer;

& path {
fill: #d84440;
}
}

.field-item p {
display: inline-block;
width: 120px;
padding: 0 10px;

& a {
text-decoration: none;
}
}

.weighting-settings .weighting {
Expand All @@ -748,7 +848,6 @@ h2.ep-list-features {
}

.weighting-settings .weighting label {
margin-right: 10px;
min-width: 80px;
}

Expand All @@ -759,7 +858,7 @@ h2.ep-list-features {
height: 1em;
margin: 0;
vertical-align: middle;
width: 200px;
width: 120px;
}

.weighting-settings input[type="range"]:focus {
Expand Down
181 changes: 181 additions & 0 deletions assets/js/weighting/components/field-group.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/**
* Wordpress Dependecies.
*/
import { useState } from '@wordpress/element';
import AsyncSelect from 'react-select/async';
import { __ } from '@wordpress/i18n';
import PropTypes from 'prop-types';

/**
* Internal Dependencies.
*/
import FieldItem from './field-item';
import MetaItem from './meta-Item';
import { sortListByOrder } from '../utils';

const FieldGroup = ({
postType,
onChangeHandler,
getCurrentFormData,
formData,
setFormData,
undoHandler,
savedData,
}) => {
const [selectedValue, setSelectedValue] = useState(null);

// handle selection
const handleChange = (value) => {
let currentFormData = getCurrentFormData(postType.name);
if (currentFormData.metas.findIndex((metaItem) => metaItem.name === value.name) === -1) {
currentFormData = {
...currentFormData,
metas: [...currentFormData.metas, value],
};
}
const excludedFormData = formData.filter((item) => item.name !== postType.name);
const newFormData = [...excludedFormData, currentFormData];
setFormData(sortListByOrder(newFormData));
setSelectedValue(value);
};

// load options using API call
const loadOptions = (inputValue) => {
return fetch(
`https://jsonplaceholder.typicode.com/posts?search=${inputValue}$post_type=${postType.name}`,
)
.then((res) => res.json())
.then((result) => {
const newResult = [];
result.forEach((item) => {
newResult.push({
name: item.title,
searchable: false,
weight: 10,
});
});
return newResult;
});
};

const removeMetaItem = (metaName) => {
const newCurrentFormData = {
...getCurrentFormData(postType.name),
metas: getCurrentFormData(postType.name).metas.filter((item) => item.name !== metaName),
};

const excludedFormData = formData.filter((item) => item.name !== postType.name);
const newFormData = [...excludedFormData, newCurrentFormData];
setFormData(sortListByOrder(newFormData));
};

const currentOriginalData = savedData.find((item) => item.name === postType.name);

return (
<>
<div className="field-group">
<h3>{__('Attributes', 'elasticpress')}</h3>
<div className="fields">
{postType.attributes.map((attribute, index) => {
const fieldType = 'attributes';
const isAttributeChanged =
JSON.stringify(attribute) !==
JSON.stringify(currentOriginalData[fieldType][index]);
return (
<FieldItem
key={`${postType.name}-${attribute.name}`}
attribute={attribute}
fieldType={fieldType}
postType={postType}
onChangeHandler={onChangeHandler}
getCurrentFormData={getCurrentFormData}
currentIndex={index}
undoHandler={undoHandler}
isAttributeChanged={isAttributeChanged}
/>
);
})}
</div>
</div>
<div className="field-group">
<h3>{__('Taxonomies', 'elasticpress')}</h3>
<div className="fields">
{postType.taxonomies.map((taxonomy, index) => {
const fieldType = 'taxonomies';
const isAttributeChanged =
JSON.stringify(taxonomy) !==
JSON.stringify(currentOriginalData[fieldType][index]);

return (
<FieldItem
key={`${postType.name}-${taxonomy.name}`}
attribute={taxonomy}
fieldType={fieldType}
postType={postType}
onChangeHandler={onChangeHandler}
getCurrentFormData={getCurrentFormData}
currentIndex={index}
undoHandler={undoHandler}
isAttributeChanged={isAttributeChanged}
/>
);
})}
</div>
</div>
<div className="field-group">
<h3>{__('Meta to be indexed', 'elasticpress')}</h3>
<div className="fields">
{getCurrentFormData(postType.name) &&
getCurrentFormData(postType.name).metas &&
getCurrentFormData(postType.name).metas.map((meta, index) => {
const fieldType = 'metas';
const isAttributeChanged =
JSON.stringify(meta) !==
JSON.stringify(currentOriginalData[fieldType][index]);
return (
<MetaItem
key={`${postType.name}-${meta.name}`}
attribute={meta}
fieldType={fieldType}
postType={postType}
onChangeHandler={onChangeHandler}
getCurrentFormData={getCurrentFormData}
currentIndex={index}
removeMetaItem={removeMetaItem}
undoHandler={undoHandler}
isAttributeChanged={isAttributeChanged}
/>
);
})}
<fieldset className="add-meta-key-wrap">
<legend>{__('Add Meta Key:', 'elasticpress')} </legend>
<div className="add-meta-key">
<AsyncSelect
cacheOptions
defaultOptions
value={selectedValue}
getOptionLabel={(e) => e.name}
getOptionValue={(e) => e.name}
loadOptions={loadOptions}
// onInputChange={handleInputChange}
onChange={handleChange}
/>
</div>
</fieldset>
</div>
</div>
</>
);
};

FieldGroup.propTypes = {
postType: PropTypes.object.isRequired,
onChangeHandler: PropTypes.func.isRequired,
getCurrentFormData: PropTypes.func.isRequired,
formData: PropTypes.array.isRequired,
setFormData: PropTypes.func.isRequired,
undoHandler: PropTypes.func.isRequired,
savedData: PropTypes.array.isRequired,
};

export default FieldGroup;
Loading