import {
NOTIFICATIONS,
awardsCollectionId,
goalsCollectionId,
userWeighInsCollectionId,
userWeighInHistoryCollectionId,
} from '../../data/collectionIds';
import { getAccount } from './accountActions';
import { getJourneyIdSelector } from '../../selectors/userProfileSelectors';
import { getProfile } from './profileActions';
import { collectionClearAll } from '../entities/collectionActions';
import { userIdSelector } from '../../selectors/userAccountSelectors';
import { apiPost } from './apiActions/apiRequest';
import { GATEWAY_COMMUNITY_AUTH } from '../../data/Injectables';
export const RESTART_JOURNEY = 'journeyActions/RESTART_JOURNEY';
export const restartJourney = values => (dispatch, getState) =>
dispatch(apiPost(RESTART_JOURNEY, GATEWAY_COMMUNITY_AUTH, '/journeys', values)).then(result => {
if (result.data.id) {
const userId = userIdSelector(getState());
[
awardsCollectionId,
goalsCollectionId,
userWeighInsCollectionId,
userWeighInHistoryCollectionId,
].map(func => dispatch(collectionClearAll(func({ userId }))));
dispatch(collectionClearAll(NOTIFICATIONS));
dispatch(getProfile(true));
dispatch(getAccount(true));
}
});
/**
* We want to catch only validation errors as this takes the user down a
* seperate modal flow. All other errors we want to handle as normal -
* block the form and display an error message on screen
*/
export const setJourneyConfirmed = values => (dispatch, getState) => {
const journeyId = getJourneyIdSelector(getState());
return dispatch(
apiPost(
SET_FIRST_WEIGH_IN_CONFIRMED,
GATEWAY_COMMUNITY_AUTH,
`/journeys/${journeyId}/start-details`,
values,
),
)
.catch(e => (e?.response?.parsed?.error.code !== 'validation-error' ? new Error(e) : null))
.then(() => dispatch(getProfile(true)));
};
export const SET_FIRST_WEIGH_IN_CONFIRMED = 'journeyActions/SET_JOURNEY_CONFIRMED';
export const START_GROUP_JOURNEY = 'journeyActions/START_GROUP_JOURNEY';
export const startGroupJourney = values => (dispatch, getState) =>
dispatch(
apiPost(START_GROUP_JOURNEY, GATEWAY_COMMUNITY_AUTH, '/journeys/group-start', values),
).then(result => {
if (result.data.id) {
const userId = userIdSelector(getState());
[
awardsCollectionId,
goalsCollectionId,
userWeighInsCollectionId,
userWeighInHistoryCollectionId,
].map(func => dispatch(collectionClearAll(func({ userId }))));
dispatch(collectionClearAll(NOTIFICATIONS));
dispatch(getProfile(true));
dispatch(getAccount(true));
}
});