./app/service/axios_backend.js

import axios from 'axios';
import Config from '../config';

const getPromise = (urlObj) => {
    return new Promise((resolve, reject) => {
        urlObj = _.defaults(urlObj, { 
            baseURL: CONFIG.API.ENDPOINT,
            headers: {'X-Signature' : signature} })
        axios(urlObj)
        .then((response) => {
            if (response && response.data && response.data.success){
                resolve(response.data);
            } else {
                reject({
                    ...response.data
                    code: response.data.exception_code || response.status.code,
                    message: response.data.debug_message || response.status.message
                })
            }
        }).catch((error) => {
            reject({
                ...error
                code: 'GENERIC_CODE',
                message: error.message
            });
        });

    })
}

const addBookmarkTo = ({prog_id}) => getPromise({
    url: CONFIG.API.ME.BOOKMARK,
    data: { prog_id },
    method: 'post'});

./app/saga/OptimisiticExceptionSaga.js

import { showExceptionTips } from '../services';
import { startAccountLogin, restartApplication } from '../actions';



function* handleException(exception, actions){
    const userActionOnExceptionTips = yield call(showExceptionTips, exception);
    if (userActionOnExceptionTips == 'OK'){
        let follow_up_action = actions && actions[exception.code];
        if (!follow_up_action){
            // shared handling
            switch (exception.code){
                case 401:
                case 'UNAUTHORIZED':
                    follow_up_action = startAccountLogin(exception);
                    break;
                case 403:
                case 'FORBIDDEN':
                    follow_up_action = startAccountLogin();
                    break;
                case 'GENERIC_CODE':
                    follow_up_action = restartApplication();
                    break;
            }
            yield put(follow_up_action)
        }
    }
}

export default handleException;

./app/sagas/ProfileEdit.js

import { PROGRAMME_BOOKMARK } from '../constants';
import { get_current_programme_id } from '../selectors';
import { addBookmarkTo } from '../services';
import handleException from '../ExceptionSaga';
import { showProgramExpiry } from '../actions';

function* watchProgrammeBookmarkTrigger(){
    while (true){
        yield take(PROGRAMME_BOOKMARK)
        const prog_id = yield select(get_current_programme_id);
        try {
            const response = yield call(addBookmarkTo, {prog_id});
            //.....
        } catch (exception) {
            yield fork(handleException, exception, {
                'PROGRAMME_EXPIRY': showProgramExpiry
            })
        }
    }
}

function* rootSaga(){
    yield fork[
        watchProgrammeBookmarkTrigger
    ]
}

results matching ""

    No results matching ""