2019-07-09 09:53:50 +02:00
|
|
|
import React, { memo, useCallback, useEffect, useRef, useState } from 'react';
|
2019-07-08 17:01:12 +02:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { bindActionCreators, compose } from 'redux';
|
2019-07-09 09:53:50 +02:00
|
|
|
import { capitalize, get, isEmpty, sortBy } from 'lodash';
|
|
|
|
import { FormattedMessage } from 'react-intl';
|
2019-07-08 17:01:12 +02:00
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
import {
|
|
|
|
ButtonDropdown,
|
|
|
|
DropdownToggle,
|
|
|
|
DropdownMenu,
|
|
|
|
DropdownItem,
|
|
|
|
} from 'reactstrap';
|
2019-07-08 20:27:38 +02:00
|
|
|
import { PluginHeader, getQueryParameters } from 'strapi-helper-plugin';
|
2019-07-08 17:01:12 +02:00
|
|
|
|
|
|
|
import pluginId from '../../pluginId';
|
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
import FilterLogo from '../../assets/images/icon_filter.png';
|
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
import Container from '../../components/Container';
|
2019-07-09 16:56:40 +02:00
|
|
|
import CustomTable from '../../components/CustomTable';
|
2019-07-09 12:31:18 +02:00
|
|
|
import FilterPicker from '../../components/FilterPicker';
|
2019-07-09 09:53:50 +02:00
|
|
|
import InputCheckbox from '../../components/InputCheckbox';
|
2019-07-08 20:27:38 +02:00
|
|
|
import Search from '../../components/Search';
|
2019-07-08 17:01:12 +02:00
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
import { onChangeListLabels, resetListLabels } from '../Main/actions';
|
|
|
|
|
|
|
|
import { AddFilterCta, DropDownWrapper, Img, Wrapper } from './components';
|
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
import { getData, resetProps } from './actions';
|
2019-07-08 17:01:12 +02:00
|
|
|
import reducer from './reducer';
|
|
|
|
import saga from './saga';
|
|
|
|
import makeSelectListView from './selectors';
|
2019-07-09 16:56:40 +02:00
|
|
|
const generateSearchFilters = search => {
|
|
|
|
return search
|
|
|
|
.split('&')
|
|
|
|
.filter(
|
|
|
|
x =>
|
|
|
|
!x.includes('_limit') &&
|
|
|
|
!x.includes('_page') &&
|
|
|
|
!x.includes('_sort') &&
|
|
|
|
!x.includes('source') &&
|
|
|
|
!x.includes('_q=')
|
|
|
|
)
|
|
|
|
.reduce((acc, current) => {
|
|
|
|
const [name, value] = current.split('=');
|
|
|
|
acc[name] = value;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
2019-07-08 17:01:12 +02:00
|
|
|
|
|
|
|
function ListView({
|
2019-07-08 20:27:38 +02:00
|
|
|
count,
|
2019-07-09 16:56:40 +02:00
|
|
|
data,
|
2019-07-08 20:27:38 +02:00
|
|
|
emitEvent,
|
|
|
|
location: { search },
|
|
|
|
getData,
|
2019-07-08 17:01:12 +02:00
|
|
|
layouts,
|
2019-07-08 20:27:38 +02:00
|
|
|
isLoading,
|
|
|
|
history: { push },
|
2019-07-08 17:01:12 +02:00
|
|
|
match: {
|
|
|
|
params: { slug },
|
|
|
|
},
|
2019-07-09 09:53:50 +02:00
|
|
|
onChangeListLabels,
|
|
|
|
resetListLabels,
|
2019-07-08 20:27:38 +02:00
|
|
|
resetProps,
|
2019-07-08 17:01:12 +02:00
|
|
|
}) {
|
|
|
|
strapi.useInjectReducer({ key: 'listView', reducer, pluginId });
|
|
|
|
strapi.useInjectSaga({ key: 'listView', saga, pluginId });
|
2019-07-09 16:56:40 +02:00
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
const [isLabelPickerOpen, setLabelPickerState] = useState(false);
|
2019-07-09 12:31:18 +02:00
|
|
|
const [isFilterPickerOpen, setFilterPickerState] = useState(false);
|
2019-07-09 09:53:50 +02:00
|
|
|
const getLayoutSettingRef = useRef();
|
|
|
|
getLayoutSettingRef.current = settingName =>
|
|
|
|
get(layouts, [slug, 'settings', settingName], '');
|
2019-07-08 20:27:38 +02:00
|
|
|
|
|
|
|
const generateSearchParams = useCallback(
|
|
|
|
(updatedParams = {}) => {
|
|
|
|
return {
|
|
|
|
_limit:
|
2019-07-09 09:53:50 +02:00
|
|
|
getQueryParameters(search, '_limit') ||
|
|
|
|
getLayoutSettingRef.current('pageSize'),
|
2019-07-08 20:27:38 +02:00
|
|
|
_page: getQueryParameters(search, '_page') || 1,
|
|
|
|
_q: getQueryParameters(search, '_q') || '',
|
|
|
|
_sort:
|
|
|
|
getQueryParameters(search, '_sort') ||
|
2019-07-09 09:53:50 +02:00
|
|
|
`${getLayoutSettingRef.current(
|
|
|
|
'defaultSortBy'
|
|
|
|
)}:${getLayoutSettingRef.current('defaultSortOrder')}`,
|
2019-07-08 20:27:38 +02:00
|
|
|
source: getQueryParameters(search, 'source'),
|
2019-07-09 16:56:40 +02:00
|
|
|
...generateSearchFilters(search),
|
2019-07-08 20:27:38 +02:00
|
|
|
...updatedParams,
|
|
|
|
};
|
|
|
|
},
|
2019-07-09 09:53:50 +02:00
|
|
|
[getLayoutSettingRef, search]
|
2019-07-08 20:27:38 +02:00
|
|
|
);
|
2019-07-08 17:01:12 +02:00
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
const toggleLabelPickerState = () =>
|
|
|
|
setLabelPickerState(prevState => !prevState);
|
2019-07-09 12:31:18 +02:00
|
|
|
const toggleFilterPickerState = () =>
|
|
|
|
setFilterPickerState(prevState => !prevState);
|
2019-07-09 09:53:50 +02:00
|
|
|
|
2019-07-08 17:01:12 +02:00
|
|
|
useEffect(() => {
|
2019-07-08 20:27:38 +02:00
|
|
|
getData(slug, generateSearchParams());
|
2019-07-08 17:01:12 +02:00
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
return () => {
|
|
|
|
resetProps();
|
|
|
|
};
|
|
|
|
/* eslint-disable-next-line react-hooks/exhaustive-deps */
|
|
|
|
}, [slug]);
|
|
|
|
|
|
|
|
const pluginHeaderActions = [
|
|
|
|
{
|
|
|
|
id: 'addEntry',
|
|
|
|
label: 'content-manager.containers.List.addAnEntry',
|
|
|
|
labelValues: {
|
|
|
|
entity: capitalize(slug) || 'Content Manager',
|
|
|
|
},
|
|
|
|
kind: 'primaryAddShape',
|
|
|
|
onClick: () => {
|
|
|
|
emitEvent('willCreateEntry');
|
|
|
|
},
|
|
|
|
},
|
|
|
|
];
|
2019-07-09 09:53:50 +02:00
|
|
|
const getAllLabels = () => {
|
|
|
|
const metadatas = get(layouts, [slug, 'metadata'], {});
|
|
|
|
|
|
|
|
return sortBy(
|
|
|
|
Object.keys(metadatas)
|
|
|
|
.filter(key => !isEmpty(get(layouts, [slug, 'metadata', key, 'list'])))
|
|
|
|
.map(label => ({
|
|
|
|
name: label,
|
|
|
|
value: get(layouts, [slug, 'layouts', 'list'], []).includes(label),
|
|
|
|
})),
|
|
|
|
['label', 'name']
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-07-08 20:27:38 +02:00
|
|
|
const handleChangeParams = ({ target: { name, value } }) => {
|
|
|
|
const updatedSearch = generateSearchParams({ [name]: value });
|
|
|
|
const newSearch = Object.keys(updatedSearch)
|
|
|
|
.map(key => `${key}=${updatedSearch[key]}`)
|
|
|
|
.join('&');
|
|
|
|
|
|
|
|
push({ search: newSearch });
|
|
|
|
resetProps();
|
|
|
|
getData(slug, updatedSearch);
|
|
|
|
};
|
2019-07-09 09:53:50 +02:00
|
|
|
const handleChangeListLabels = ({ name, value }) => {
|
2019-07-09 16:56:40 +02:00
|
|
|
const currentSort = generateSearchParams()._sort;
|
|
|
|
const displayedLabels = getTableHeaders();
|
|
|
|
|
|
|
|
if (value && displayedLabels.length === 1) {
|
|
|
|
strapi.notification.error(
|
|
|
|
'content-manager.notification.error.displayedFields'
|
|
|
|
);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSort.split(':')[0] === name && value) {
|
|
|
|
const firstSortableElement = get(
|
|
|
|
displayedLabels.filter(h => h.name !== name && h.sortable === true),
|
|
|
|
['0', 'name'],
|
|
|
|
'id'
|
|
|
|
);
|
|
|
|
|
|
|
|
emitEvent('didChangeDisplayedFields');
|
|
|
|
handleChangeParams({
|
|
|
|
target: { name: '_sort', value: `${firstSortableElement}:ASC` },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-09 09:53:50 +02:00
|
|
|
onChangeListLabels({
|
|
|
|
target: { name: `${slug}.${name}`, value: !value },
|
|
|
|
});
|
|
|
|
};
|
2019-07-09 12:31:18 +02:00
|
|
|
const filterPickerActions = [
|
|
|
|
{
|
|
|
|
label: `${pluginId}.components.FiltersPickWrapper.PluginHeader.actions.clearAll`,
|
|
|
|
kind: 'secondary',
|
|
|
|
onClick: () => {
|
|
|
|
toggleFilterPickerState();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
label: `${pluginId}.components.FiltersPickWrapper.PluginHeader.actions.apply`,
|
|
|
|
kind: 'primary',
|
|
|
|
type: 'submit',
|
|
|
|
},
|
|
|
|
];
|
2019-07-08 17:01:12 +02:00
|
|
|
|
2019-07-09 16:56:40 +02:00
|
|
|
const handleSubmit = () => {
|
|
|
|
emitEvent('didFilterEntries');
|
|
|
|
toggleFilterPickerState();
|
|
|
|
};
|
|
|
|
const getTableHeaders = useCallback(() => {
|
|
|
|
const metadatas = get(layouts, [slug, 'metadata'], {});
|
|
|
|
|
|
|
|
return get(layouts, [slug, 'layouts', 'list'], []).map(label => {
|
|
|
|
const infos = get(metadatas, [label, 'list'], {});
|
|
|
|
|
|
|
|
return { ...infos, name: label };
|
|
|
|
});
|
|
|
|
}, [layouts, slug]);
|
|
|
|
|
2019-07-08 17:01:12 +02:00
|
|
|
return (
|
2019-07-08 20:27:38 +02:00
|
|
|
<>
|
2019-07-09 12:31:18 +02:00
|
|
|
<FilterPicker
|
|
|
|
actions={filterPickerActions}
|
|
|
|
isOpen={isFilterPickerOpen}
|
|
|
|
name={slug}
|
2019-07-09 16:56:40 +02:00
|
|
|
onSubmit={handleSubmit}
|
2019-07-09 12:31:18 +02:00
|
|
|
/>
|
2019-07-09 16:56:40 +02:00
|
|
|
<Container className="container-fluid">
|
2019-07-09 12:31:18 +02:00
|
|
|
{!isFilterPickerOpen && (
|
|
|
|
<PluginHeader
|
|
|
|
actions={pluginHeaderActions}
|
|
|
|
description={{
|
|
|
|
id:
|
|
|
|
count > 1
|
|
|
|
? `${pluginId}.containers.List.pluginHeaderDescription`
|
|
|
|
: `${pluginId}.containers.List.pluginHeaderDescription.singular`,
|
|
|
|
values: {
|
|
|
|
label: count,
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
title={{
|
|
|
|
id: slug || 'Content Manager',
|
|
|
|
}}
|
|
|
|
withDescriptionAnim={isLoading}
|
|
|
|
/>
|
|
|
|
)}
|
2019-07-09 09:53:50 +02:00
|
|
|
{getLayoutSettingRef.current('searchable') && (
|
2019-07-08 20:27:38 +02:00
|
|
|
<Search
|
|
|
|
changeParams={handleChangeParams}
|
|
|
|
initValue={getQueryParameters(search, '_q') || ''}
|
|
|
|
model={slug}
|
|
|
|
value={getQueryParameters(search, '_q') || ''}
|
|
|
|
/>
|
|
|
|
)}
|
2019-07-09 09:53:50 +02:00
|
|
|
<Wrapper>
|
|
|
|
<div className="row">
|
|
|
|
<div className="col-10">
|
2019-07-09 12:31:18 +02:00
|
|
|
<AddFilterCta type="button" onClick={toggleFilterPickerState}>
|
2019-07-09 09:53:50 +02:00
|
|
|
<Img src={FilterLogo} alt="filter_logo" />
|
|
|
|
<FormattedMessage
|
|
|
|
id={`${pluginId}.components.AddFilterCTA.add`}
|
|
|
|
/>
|
|
|
|
</AddFilterCta>
|
|
|
|
</div>
|
|
|
|
<div className="col-2">
|
|
|
|
<DropDownWrapper>
|
|
|
|
<ButtonDropdown
|
|
|
|
isOpen={isLabelPickerOpen}
|
|
|
|
toggle={toggleLabelPickerState}
|
|
|
|
direction="left"
|
|
|
|
>
|
|
|
|
<DropdownToggle />
|
|
|
|
<DropdownMenu>
|
|
|
|
<FormattedMessage id="content-manager.containers.ListPage.displayedFields">
|
|
|
|
{msg => (
|
|
|
|
<DropdownItem
|
|
|
|
onClick={() => {
|
|
|
|
resetListLabels(slug);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<span>{msg}</span>
|
|
|
|
<FormattedMessage id="content-manager.containers.Edit.reset" />
|
|
|
|
</div>
|
|
|
|
</DropdownItem>
|
|
|
|
)}
|
|
|
|
</FormattedMessage>
|
|
|
|
{getAllLabels().map(label => {
|
|
|
|
//
|
|
|
|
return (
|
|
|
|
<DropdownItem
|
|
|
|
key={label.name}
|
|
|
|
toggle={false}
|
|
|
|
onClick={() => handleChangeListLabels(label)}
|
|
|
|
>
|
|
|
|
<div>
|
|
|
|
<InputCheckbox
|
|
|
|
onChange={() => handleChangeListLabels(label)}
|
|
|
|
name={label.name}
|
|
|
|
value={label.value}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</DropdownItem>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</DropdownMenu>
|
|
|
|
</ButtonDropdown>
|
|
|
|
</DropDownWrapper>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-07-09 16:56:40 +02:00
|
|
|
<div className="row" style={{ paddingTop: '36px' }}>
|
|
|
|
<div className="col-12">
|
|
|
|
<CustomTable
|
|
|
|
data={data}
|
|
|
|
defaultSortBy={getLayoutSettingRef.current('defaultSortBy')}
|
|
|
|
defaultSortOrder={getLayoutSettingRef.current(
|
|
|
|
'defaultSortOrder'
|
|
|
|
)}
|
|
|
|
headers={getTableHeaders()}
|
|
|
|
isBulkable={getLayoutSettingRef.current('bulkable')}
|
|
|
|
onChangeParams={handleChangeParams}
|
|
|
|
slug={slug}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-07-09 09:53:50 +02:00
|
|
|
</Wrapper>
|
2019-07-08 20:27:38 +02:00
|
|
|
</Container>
|
|
|
|
</>
|
2019-07-08 17:01:12 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
ListView.defaultProps = {
|
|
|
|
layouts: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
ListView.propTypes = {
|
2019-07-08 20:27:38 +02:00
|
|
|
count: PropTypes.number.isRequired,
|
2019-07-09 16:56:40 +02:00
|
|
|
data: PropTypes.array.isRequired,
|
2019-07-08 20:27:38 +02:00
|
|
|
emitEvent: PropTypes.func.isRequired,
|
2019-07-08 17:01:12 +02:00
|
|
|
layouts: PropTypes.object,
|
2019-07-08 20:27:38 +02:00
|
|
|
location: PropTypes.shape({
|
|
|
|
search: PropTypes.string.isRequired,
|
|
|
|
}),
|
|
|
|
getData: PropTypes.func.isRequired,
|
|
|
|
isLoading: PropTypes.bool.isRequired,
|
2019-07-09 09:53:50 +02:00
|
|
|
history: PropTypes.shape({
|
|
|
|
push: PropTypes.func.isRequired,
|
|
|
|
}),
|
2019-07-08 17:01:12 +02:00
|
|
|
match: PropTypes.shape({
|
|
|
|
params: PropTypes.shape({
|
|
|
|
slug: PropTypes.string.isRequired,
|
|
|
|
}),
|
|
|
|
}),
|
2019-07-09 09:53:50 +02:00
|
|
|
onChangeListLabels: PropTypes.func.isRequired,
|
|
|
|
resetListLabels: PropTypes.func.isRequired,
|
2019-07-08 20:27:38 +02:00
|
|
|
resetProps: PropTypes.func.isRequired,
|
2019-07-08 17:01:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = makeSelectListView();
|
|
|
|
|
|
|
|
export function mapDispatchToProps(dispatch) {
|
|
|
|
return bindActionCreators(
|
|
|
|
{
|
2019-07-08 20:27:38 +02:00
|
|
|
getData,
|
2019-07-09 09:53:50 +02:00
|
|
|
onChangeListLabels,
|
|
|
|
resetListLabels,
|
2019-07-08 20:27:38 +02:00
|
|
|
resetProps,
|
2019-07-08 17:01:12 +02:00
|
|
|
},
|
|
|
|
dispatch
|
|
|
|
);
|
|
|
|
}
|
|
|
|
const withConnect = connect(
|
|
|
|
mapStateToProps,
|
|
|
|
mapDispatchToProps
|
|
|
|
);
|
|
|
|
|
|
|
|
export default compose(
|
|
|
|
withConnect,
|
|
|
|
memo
|
|
|
|
)(ListView);
|