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 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';
|
|
|
|
|
|
|
|
function ListView({
|
2019-07-08 20:27:38 +02:00
|
|
|
count,
|
|
|
|
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 09:53:50 +02:00
|
|
|
const [isLabelPickerOpen, setLabelPickerState] = useState(false);
|
|
|
|
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'),
|
|
|
|
...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-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 }) => {
|
|
|
|
emitEvent('didChangeDisplayedFields');
|
|
|
|
onChangeListLabels({
|
|
|
|
target: { name: `${slug}.${name}`, value: !value },
|
|
|
|
});
|
|
|
|
};
|
2019-07-08 17:01:12 +02:00
|
|
|
|
|
|
|
return (
|
2019-07-08 20:27:38 +02:00
|
|
|
<>
|
|
|
|
<Container>
|
|
|
|
<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">
|
|
|
|
<AddFilterCta type="button">
|
|
|
|
<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>
|
|
|
|
</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,
|
|
|
|
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);
|