Skip to content

Commit

Permalink
feat: do not display push notifications while in barcode view.
Browse files Browse the repository at this point in the history
The notifications are not removed, they are stacked into a queue then displayed when the `state.app.shouldNotificationBeDisplayed` is set back to `true`
  • Loading branch information
r0xsh committed Dec 2, 2024
1 parent df749ac commit a421250
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/components/NotificationHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import { useDispatch, useSelector } from 'react-redux';

import {
clearNotifications,
shouldNotificationBeDisplayed,
startSound,
stopSound,
} from '../redux/App/actions';
import NotificationModal from './NotificationModal';
import {
selectNotificationsToDisplay,
selectNotificationsWithSound,
selectShouldNotificationBeDisplayed
} from '../redux/App/selectors';
import { AppState } from 'react-native';

Expand All @@ -25,6 +27,7 @@ const NOTIFICATION_DURATION_MS = 10000;
export default function NotificationHandler() {
const notificationsToDisplay = useSelector(selectNotificationsToDisplay);
const notificationsWithSound = useSelector(selectNotificationsWithSound);
const shouldNotificationBeDisplayed = useSelector(selectShouldNotificationBeDisplayed);

const dispatch = useDispatch();

Expand All @@ -44,14 +47,19 @@ export default function NotificationHandler() {
// but it's very limited, e.g. handlers set via setTimeout are not executed
// so we do not play sound in that case, because we will not be able to stop it
if (
shouldNotificationBeDisplayed &&
notificationsWithSound.length > 0 &&
AppState.currentState === 'active'
) {
dispatch(startSound());
} else {
dispatch(stopSound());
}
}, [notificationsWithSound, dispatch]);
}, [notificationsWithSound, shouldNotificationBeDisplayed, dispatch]);

if (!shouldNotificationBeDisplayed) {
return null;
}

return (
<NotificationModal
Expand Down
23 changes: 20 additions & 3 deletions src/navigation/courier/barcode/Barcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { phonecall } from 'react-native-communications';
import BottomModal from '../../../components/BottomModal';
import { navigateToTask } from '../../utils';
import { selectTasks } from '../../../redux/Courier';
import { shouldNotificationBeDisplayed } from '../../../redux/App/actions';

async function _fetchBarcode(httpClient, barcode) {
if (barcode) {
Expand Down Expand Up @@ -76,7 +77,13 @@ function TextSection({ title, value, variant = 'data' }) {
);
}

function BarcodePage({ t, httpClient, navigation, taskLists }) {
function BarcodePage({
t,
httpClient,
navigation,
taskLists,
shouldNotificationBeDisplayed,
}) {
const [barcode, setBarcode] = useState(null);
const [entity, setEntity] = useState(null);
const [clientActionsQueue, setClientActionsQueue] = useState([]);
Expand Down Expand Up @@ -186,6 +193,13 @@ function BarcodePage({ t, httpClient, navigation, taskLists }) {
}
}

useEffect(() => {
shouldNotificationBeDisplayed(false);
return () => {
shouldNotificationBeDisplayed(true);
};
}, []);

Check failure on line 201 in src/navigation/courier/barcode/Barcode.js

View workflow job for this annotation

GitHub Actions / Basic tests

React Hook useEffect has a missing dependency: 'shouldNotificationBeDisplayed'. Either include it or remove the dependency array. If 'shouldNotificationBeDisplayed' changes too often, find the parent component that defines it and wrap that definition in useCallback

useEffect(() => {
async function processActions() {
if (clientActionsQueue.length === 0) return;
Expand Down Expand Up @@ -341,8 +355,11 @@ function mapStateToProps(state) {
};
}

function mapDispatchToProps(_dispatch) {
return {};
function mapDispatchToProps(dispatch) {
return {
shouldNotificationBeDisplayed: should =>
dispatch(shouldNotificationBeDisplayed(should)),
};
}

export default connect(
Expand Down
4 changes: 4 additions & 0 deletions src/redux/App/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const SAVE_PUSH_NOTIFICATION_TOKEN_SUCCESS =
'@app/SAVE_PUSH_NOTIFICATION_TOKEN_SUCCESS';
export const DELETE_PUSH_NOTIFICATION_TOKEN_SUCCESS =
'@app/DELETE_PUSH_NOTIFICATION_TOKEN_SUCCESS';
export const SHOULD_NOTIFICATION_BE_DISPLAYED = '@app/SHOULD_NOTIFICATION_BE_DISPLAYED';

export const LOGIN = '@app/LOGIN';
export const SET_LOADING = '@app/SET_LOADING';
Expand Down Expand Up @@ -165,6 +166,9 @@ export const savePushNotificationTokenSuccess = createFsAction(
export const deletePushNotificationTokenSuccess = createFsAction(
DELETE_PUSH_NOTIFICATION_TOKEN_SUCCESS,
);
export const shouldNotificationBeDisplayed = createFsAction(
SHOULD_NOTIFICATION_BE_DISPLAYED,
)

const _loadMyStoresSuccess = createFsAction(LOAD_MY_STORES_SUCCESS);

Expand Down
9 changes: 9 additions & 0 deletions src/redux/App/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
RESET_PASSWORD_REQUEST_SUCCESS,
RESUME_CHECKOUT_AFTER_ACTIVATION,
SAVE_PUSH_NOTIFICATION_TOKEN_SUCCESS,
SHOULD_NOTIFICATION_BE_DISPLAYED,
SET_BACKGROUND_GEOLOCATION_ENABLED,
SET_BASE_URL,
SET_CURRENT_ROUTE,
Expand Down Expand Up @@ -70,6 +71,7 @@ const initialState = {
currentRoute: null,
pushNotificationToken: null,
pushNotificationTokenSaved: null,
shouldNotificationBeDisplayed: true,
loading: false,
notifications: [],
lastAuthenticationError: null,
Expand Down Expand Up @@ -194,6 +196,13 @@ export default (state = initialState, action = {}) => {
return updateNotifications(state, event, params);
}

case SHOULD_NOTIFICATION_BE_DISPLAYED: {
return {
...state,
shouldNotificationBeDisplayed: action.payload,
}
}

case CLEAR_NOTIFICATIONS:
return {
...state,
Expand Down
1 change: 1 addition & 0 deletions src/redux/App/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,4 @@ export const selectNotificationsToDisplay = createSelector(
export const selectSettingsLatLng = state => state.app.settings.latlng;
export const selectStripePublishableKey = state =>
state.app.settings.stripe_publishable_key;
export const selectShouldNotificationBeDisplayed = state => state.app.shouldNotificationBeDisplayed;

0 comments on commit a421250

Please sign in to comment.