<commit_before>/*
 * Copyright 2017 Lime - HighTech Solutions s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

export function dispatchAction(dispatch, response) {
    if (response.data.next.length > 0) {
        if (response.data.result === "CONFIRMED") {
            const next = response.data.next;
            let authMethods = [];
            for (let key in next) {
                switch (next[key].authMethod) {
                    case "USER_ID_ASSIGN": {
                        // TODO - implement USER_ID_ASSIGN, for now it is ignored
                        break;
                    }
                    case "USERNAME_PASSWORD_AUTH": {
                        dispatch({
                            type: "SHOW_SCREEN_LOGIN",
                            payload: {
                                loading: false,
                                error: false,
                                message: "login.pleaseLogIn"
                            }
                        });
                        break;
                    }
                    case "POWERAUTH_TOKEN": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("POWERAUTH_TOKEN");
                        break;
                    }
                    case "SMS_KEY": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("SMS_KEY");
                        break;
                    }
                }
            }
            if (authMethods.length > 0) {
                dispatch({
                    type: "SHOW_SCREEN_OPERATION_REVIEW",
                    payload: {
                        authMethods: authMethods
                    }
                });
            }
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    } else {
        if (response.data.result === "CONFIRMED") {
            dispatch({
                type: "SHOW_SCREEN_SUCCESS",
                payload: {
                    message: response.data.message
                }
            })
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    }
}

export function dispatchError(dispatch, error) {
    let errorMessage;
    if (error.response) {
        errorMessage = error.response.data.message;
    } else if (error.request) {
        errorMessage = "message.invalidRequest"
    } else {
        errorMessage = error.message;
    }
    dispatch({
        type: "SHOW_SCREEN_ERROR",<commit_msg>Support for responseObject in error messages, comments for resolving error messages in the UI<commit_after>/*
 * Copyright 2017 Lime - HighTech Solutions s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

export function dispatchAction(dispatch, response) {
    if (response.data.next.length > 0) {
        if (response.data.result === "CONFIRMED") {
            const next = response.data.next;
            let authMethods = [];
            for (let key in next) {
                switch (next[key].authMethod) {
                    case "USER_ID_ASSIGN": {
                        // TODO - implement USER_ID_ASSIGN, for now it is ignored
                        break;
                    }
                    case "USERNAME_PASSWORD_AUTH": {
                        dispatch({
                            type: "SHOW_SCREEN_LOGIN",
                            payload: {
                                loading: false,
                                error: false,
                                message: "login.pleaseLogIn"
                            }
                        });
                        break;
                    }
                    case "POWERAUTH_TOKEN": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("POWERAUTH_TOKEN");
                        break;
                    }
                    case "SMS_KEY": {
                        // add powerauth token authentication method for operation review step
                        authMethods.push("SMS_KEY");
                        break;
                    }
                }
            }
            if (authMethods.length > 0) {
                dispatch({
                    type: "SHOW_SCREEN_OPERATION_REVIEW",
                    payload: {
                        authMethods: authMethods
                    }
                });
            }
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    } else {
        if (response.data.result === "CONFIRMED") {
            dispatch({
                type: "SHOW_SCREEN_SUCCESS",
                payload: {
                    message: response.data.message
                }
            })
        } else if (response.data.result === "AUTH_FAILED") {
            dispatch({
                type: "SHOW_SCREEN_ERROR",
                payload: {
                    message: response.data.message
                }
            })
        }
    }
}

export function dispatchError(dispatch, error) {
    let errorMessage;
    if (error.response) {
        if (error.response.data.responseObject.message) {
            // structured error responses which include a responseObject with a message
            errorMessage = error.response.data.responseObject.message;
        } else {
            // responses with message field in the data
            errorMessage = error.response.data.message;
        }
    } else if (error.request) {
        // the request was made but no response was received
        // see: https://www.npmjs.com/package/axios#handling-errors
        errorMessage = "message.invalidRequest"
    } else {
        // something happened in setting up the request that triggered an Error
        // see: https://www.npmjs.com/package/axios#handling-errors
        errorMessage = error.message;
    }
    dispatch({
        type: "SHOW_SCREEN_ERROR",
{'fraction_non_alphanumeric': 0.08213199312260283, 'fraction_numerical': 0.0026451527575717497, 'mean_word_length': 1.2432512607534856, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3397675', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1721, 'n_words': 492}