Last active
May 2, 2018 15:31
-
-
Save ChadRehmKineticData/59e0818613409c9535cc54fed5128b75 to your computer and use it in GitHub Desktop.
Add lifecycle methods to CatalogSearchContianer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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