Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add lifecycle methods to CatalogSearchContianer
import { connect } from 'react-redux';
import { CatalogSearchResults } from './CatalogSearchResults';
import { displayableFormPredicate } from '../../utils';
import { compose, lifecycle } from 'recompose';
import { CoreAPI } from 'react-kinetic-core';
import moment from 'moment';
const matches = (form, term) =>
form.name.toLowerCase().includes(term.toLowerCase()) ||
(form.description &&
form.description.toLowerCase().includes(term.toLowerCase()));
const mapStateToProps = (state, props) => {
const query = props.match.params.query || '';
return {
query,
username: state.kinops.profile.username,
forms: state.forms.data
.filter(displayableFormPredicate)
.filter(form => matches(form, query)),
};
};
const enhance = compose(
connect(mapStateToProps),
lifecycle({
componentWillMount() {
const { username, query, forms } = this.props;
CoreAPI.createSubmission({
kappSlug: 'admin',
formSlug: 'search-queries',
values: {
Status: 'active',
Query: query,
Requester: username,
Results: forms,
TimeStamp: moment().format(),
},
});
},
componentWillReceiveProps(nextProps) {
const { username, query, forms } = nextProps;
if (query !== this.props.query) {
CoreAPI.createSubmission({
kappSlug: 'admin',
formSlug: 'search-queries',
values: {
Status: 'active',
Query: query,
Requester: username,
Results: forms,
TimeStamp: moment().format(),
},
});
}
},
}),
);
export const CatalogSearchResultsContainer = enhance(CatalogSearchResults);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment