|
|
|
|
|
import React from 'react'; |
|
|
import compose from 'recompose/compose'; |
|
|
import { withRouter, type Location, type History } from 'react-router'; |
|
|
import querystring from 'querystring'; |
|
|
import { connect } from 'react-redux'; |
|
|
import type { Dispatch } from 'redux'; |
|
|
import { addToastWithTimeout } from 'src/actions/toasts'; |
|
|
|
|
|
type Props = { |
|
|
location: Location, |
|
|
history: History, |
|
|
dispatch: Dispatch<Object>, |
|
|
}; |
|
|
|
|
|
class QueryParamToastDispatcher extends React.Component<Props> { |
|
|
getParams = (props: Props) => { |
|
|
return querystring.parse(props.location.search.replace('?', '')); |
|
|
}; |
|
|
|
|
|
componentDidMount() { |
|
|
const params = this.filterToastParams(this.getParams(this.props)); |
|
|
if (this.hasValidToastParams(params)) { |
|
|
this.props.dispatch( |
|
|
addToastWithTimeout(params.toastType, params.toastMessage) |
|
|
); |
|
|
return this.cleanLocation(); |
|
|
} |
|
|
} |
|
|
|
|
|
componentDidUpdate(prevProps: Props) { |
|
|
const currProps = this.props; |
|
|
const prevParams = this.filterToastParams(this.getParams(prevProps)); |
|
|
const currParams = this.filterToastParams(this.getParams(currProps)); |
|
|
|
|
|
const currValid = this.hasValidToastParams(currParams); |
|
|
if (!currValid) return; |
|
|
|
|
|
if (prevParams.toastMessage !== currParams.toastMessage) { |
|
|
currProps.dispatch( |
|
|
addToastWithTimeout(currParams.toastType, currParams.toastMessage) |
|
|
); |
|
|
return this.cleanLocation(); |
|
|
} |
|
|
} |
|
|
|
|
|
hasValidToastParams = (params: Object) => { |
|
|
const validToastTypes = ['success', 'error', 'neutral']; |
|
|
return ( |
|
|
params.toastType && |
|
|
validToastTypes.indexOf(params.toastType) >= 0 && |
|
|
params.toastMessage |
|
|
); |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
filterToastParams = (params: Object) => ({ |
|
|
toastMessage: params['toastMessage'], |
|
|
toastType: params['toastType'], |
|
|
}); |
|
|
|
|
|
cleanLocation = () => { |
|
|
const params = this.getParams(this.props); |
|
|
const clean = {}; |
|
|
Object.keys(params).map(key => { |
|
|
if (!key || key === 'toastMessage' || key === 'toastType') return null; |
|
|
clean[key] = params[key]; |
|
|
return null; |
|
|
}); |
|
|
const cleanParams = querystring.stringify(clean); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return this.props.history.push({ search: decodeURIComponent(cleanParams) }); |
|
|
}; |
|
|
|
|
|
render() { |
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
export default compose( |
|
|
withRouter, |
|
|
connect() |
|
|
)(QueryParamToastDispatcher); |
|
|
|