165 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-07-08 20:27:38 +02:00
import React, { memo, useCallback, useEffect } 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-08 20:27:38 +02:00
import { capitalize, get } from 'lodash';
2019-07-08 17:01:12 +02:00
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-08 20:27:38 +02:00
import Container from '../../components/Container';
import Search from '../../components/Search';
2019-07-08 17:01:12 +02:00
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-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-08 20:27:38 +02:00
const getLayoutSetting = useCallback(
settingName => get(layouts, [slug, 'settings', settingName], ''),
[layouts, slug]
);
const generateSearchParams = useCallback(
(updatedParams = {}) => {
return {
_limit:
getQueryParameters(search, '_limit') || getLayoutSetting('pageSize'),
_page: getQueryParameters(search, '_page') || 1,
_q: getQueryParameters(search, '_q') || '',
_sort:
getQueryParameters(search, '_sort') ||
`${getLayoutSetting('defaultSortBy')}:${getLayoutSetting(
'defaultSortOrder'
)}`,
source: getQueryParameters(search, 'source'),
...updatedParams,
};
},
[getLayoutSetting, search]
);
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]);
console.log(getLayoutSetting('layouts.list'));
const pluginHeaderActions = [
{
id: 'addEntry',
label: 'content-manager.containers.List.addAnEntry',
labelValues: {
entity: capitalize(slug) || 'Content Manager',
},
kind: 'primaryAddShape',
onClick: () => {
emitEvent('willCreateEntry');
},
},
];
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-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}
/>
{getLayoutSetting('searchable') && (
<Search
changeParams={handleChangeParams}
initValue={getQueryParameters(search, '_q') || ''}
model={slug}
value={getQueryParameters(search, '_q') || ''}
/>
)}
</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-08 17:01:12 +02:00
match: PropTypes.shape({
params: PropTypes.shape({
slug: PropTypes.string.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,
resetProps,
2019-07-08 17:01:12 +02:00
},
dispatch
);
}
const withConnect = connect(
mapStateToProps,
mapDispatchToProps
);
export default compose(
withConnect,
memo
)(ListView);