{"size": 1894, "ext": "js", "max_stars_repo_path": "src/actions/authorization/logout.js", "max_stars_repo_name": "dmitriy-ryazantsev/webpack_hmr-babel-react-redux-karma-mocha-chai-sinon-enzyme", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/actions/authorization/logout.js", "max_issues_repo_name": "dmitriy-ryazantsev/webpack_hmr-babel-react-redux-karma-mocha-chai-sinon-enzyme", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/actions/authorization/logout.js", "max_forks_repo_name": "dmitriy-ryazantsev/webpack_hmr-babel-react-redux-karma-mocha-chai-sinon-enzyme", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.9746835443, "max_line_length": 117, "alphanum_fraction": 0.5237592397}
import fetch from 'isomorphic-fetch';
import ActionTypes from './../../constants/actionTypes';
import addLogEntry from './../log/addLogEntry';
import LogMessageType from './../../constants/logMessageType';
import Defaults from './../../constants/defaults';


export function requestLogout()
{
    const action =
    {
        type: ActionTypes.RequestLogout
    };

    return action;
}

export function receiveLogout()
{
    const action =
    {
        type: ActionTypes.ReceiveLogout
    };

    return action;
}

export function receiveLogoutError()
{
    const action =
    {
        type: ActionTypes.ReceiveLogoutError
    };

    return action;
}

export function logout()
{
    return function (dispatch)
    {
        dispatch(requestLogout());
        let url = Defaults.Url;
        url += '/api/useridentity/logout';
        let header = new Headers();
        let token = sessionStorage.getItem('tokenKey');
        if(token)
        {
            header.append('Authorization', 'Bearer ' + token);
        }
        let promise = fetch(url,
        {
            method: 'POST',
            headers: header
        })
        .then(response =>
        {
            if(response.ok)
            {
                sessionStorage.removeItem('tokenKey')
                sessionStorage.removeItem('loginName')
                dispatch(receiveLogout());
                dispatch(addLogEntry('Пользователь вышел из системы!', LogMessageType.Warning));
            }
            else
            {
                throw new Error('POST: ' + response.url + ' ' + response.status + ' (' + response.statusText + ')');
            }
        })
        .catch(exc =>
        {
            dispatch(receiveLogoutError());
            dispatch(addLogEntry(exc.message));
        });

        return promise;
    }
}
