-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notificcations): implement opt in or out of notifications
- ensure users can opt in or out of notifications - write tests for the functionality [Delivers #165305236]
- Loading branch information
1 parent
621e1cf
commit 6322379
Showing
12 changed files
with
535 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import axios from 'axios'; | ||
import { BASE_URL, OPTINOUT, headers } from '../constants'; | ||
import { successToast, errorToast } from '../../helpers'; | ||
|
||
const config = { | ||
headers: { Authorization: localStorage.getItem('token') }, | ||
}; | ||
|
||
export const OptInOutAction = data => async dispatch => { | ||
dispatch({ type: OPTINOUT }); | ||
return axios | ||
.put(`${BASE_URL}/notifications/subscriptions`, data, config) | ||
.then(response => { | ||
dispatch(optInOutSuccess(response.data)); | ||
}) | ||
.catch(error => { | ||
dispatch(optInOutFailure(error)); | ||
}); | ||
}; | ||
|
||
export const NotifyStatusAction = () => async dispatch => { | ||
dispatch({ type: OPTINOUT }); | ||
return axios | ||
.get(`${BASE_URL}/notifications/subscriptions`, config) | ||
.then(response => { | ||
dispatch(optInOutSuccess(response.data)); | ||
}) | ||
.catch(error => { | ||
dispatch(optInOutFailure(error)); | ||
}); | ||
}; | ||
|
||
export const optInOutSuccess = response => ({ | ||
type: `${OPTINOUT}_SUCCESS`, | ||
response, | ||
}); | ||
|
||
export const optInOutFailure = error => ({ | ||
type: `${OPTINOUT}_FAILURE`, | ||
error, | ||
}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { OPTINOUT } from '../constants'; | ||
|
||
const initialState = { | ||
isLoading: false, | ||
data: {}, | ||
error: {}, | ||
}; | ||
|
||
export const OptInOutReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case `${OPTINOUT}`: | ||
return { | ||
...state, | ||
isLoading: true, | ||
}; | ||
case `${OPTINOUT}_SUCCESS`: | ||
return { | ||
...state, | ||
isLoading: false, | ||
data: action.response, | ||
}; | ||
case `${OPTINOUT}_FAILURE`: | ||
return { | ||
...state, | ||
isLoading: false, | ||
error: action.error, | ||
}; | ||
|
||
default: | ||
return { ...state }; | ||
} | ||
}; | ||
|
||
export const NotifyStatusReducer = (state = initialState, action) => { | ||
switch (action.type) { | ||
case `${OPTINOUT}`: | ||
return { | ||
...state, | ||
isLoading: true, | ||
}; | ||
case `${OPTINOUT}_SUCCESS`: | ||
return { | ||
...state, | ||
isLoading: false, | ||
data: action.response, | ||
}; | ||
case `${OPTINOUT}_FAILURE`: | ||
return { | ||
...state, | ||
isLoading: false, | ||
error: action.error, | ||
}; | ||
|
||
default: | ||
return { ...state }; | ||
} | ||
}; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import React, { Component } from 'react'; | ||
import { connect } from 'react-redux'; | ||
import PropTypes from 'prop-types'; | ||
import { OptInOutAction, NotifyStatusAction } from '../redux/actions/NotificationsOptinAction'; | ||
|
||
class OptInOutView extends Component { | ||
|
||
componentDidMount(){ | ||
const { NotifyStatusAction } = this.props; | ||
NotifyStatusAction(); | ||
} | ||
|
||
onClick = (e, isSubscribed) => { | ||
const { OptInOutAction } = this.props; | ||
const data = { | ||
[e.target.name]: isSubscribed, | ||
} | ||
OptInOutAction(data) | ||
}; | ||
|
||
render() { | ||
const { data, status } = this.props; | ||
const { isLoading } = status | ||
|
||
return ( | ||
<> | ||
<div className="opt-choice"> | ||
<p>Email</p> | ||
{data.data.subscriptions && data.data.subscriptions.email === true ? ( | ||
<label className="switch"> | ||
<input type="checkbox" name="email" checked onClick={() => this.onClick(event, false)} /> | ||
<span className="slider round" /> | ||
</label> | ||
):( | ||
<label className="switch"> | ||
<input type="checkbox" id="email" name="email" onClick={() => this.onClick(event, true)} /> | ||
<span className="slider round" /> | ||
</label> | ||
)} | ||
|
||
</div> | ||
<div className="opt-choice"> | ||
<p>In App</p> | ||
{data.data.subscriptions && data.data.subscriptions.app === true ? ( | ||
<label className="switch"> | ||
<input type="checkbox" name="app" checked onClick={() => this.onClick(event, false)} /> | ||
<span className="slider round" /> | ||
</label> | ||
):( | ||
<label className="switch"> | ||
<input type="checkbox" name="app" onClick={() => this.onClick(event, true)} /> | ||
<span className="slider round" /> | ||
</label> | ||
)} | ||
|
||
</div> | ||
</> | ||
); | ||
} | ||
} | ||
|
||
export const mapStateToProps = state => ({ | ||
data: state.OptInOutReducer, | ||
status: state.NotifyStatusReducer, | ||
}); | ||
|
||
export const mapDispatchToProps = () => ({ | ||
OptInOutAction, | ||
NotifyStatusAction, | ||
}); | ||
|
||
export default connect( | ||
mapStateToProps, | ||
mapDispatchToProps(), | ||
)(OptInOutView); |
Oops, something went wrong.