Как сделать невидимый обратный звонок по запросу MercadoLivre? - PullRequest
0 голосов
/ 26 июня 2019

I m use the mercadolivre s API и требуется запрос, который возвращает обратный вызов с токеном, но ... Мне нужно, чтобы этот процесс был невидимым и возвращал только токен.

Я вызываю функцию смой бэкэнд и обратный вызов:

import React, { Component } from 'react';
import {getToken} from '../../../auth';
import axios from 'axios';
import Swal from 'sweetalert2';

class CallBack extends Component {

  constructor(props) {
    super(props);
    const token = window.location.href.split('?')[1].split('=')[1].split('&')[0];
    this.state = {
      token: token,
      message: '',
      status: '',
      executed: false,
      doIt: 0
    };
  }
  render(){
    const token = this.state.token;

    return (
        !this.state.executed ? (

            axios.post(process.env.REACT_APP_API_URL + `/accounts/from-mercado-livre`,
                {"code": token,},
                {headers: {"Authorization": 'Bearer ' + getToken()}},
            ).then(res => {

              this.setState(
                  {
                    executed: true,
                    doIt: 2
                  }
              )

              if (res.data.status === 'success') {
                Swal.fire({
                  html: '<p>' + res.data.message + '</p>', type: 'success', showConfirmButton: true,
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              } else {
                Swal.fire({
                  html: '<p>' + res.data.message + '</p>', type: 'error', showConfirmButton: true,
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              }
            }).catch(error => {
              console.log('rejected');
              console.log(error.response);


              if (error.response !== undefined) {
                Swal.fire({
                  html: '<p>' + error.response.data.message + '</p>',
                  type: 'error',
                  showConfirmButton: false,
                  showCancelButton: true,
                  cancelButtonText: 'Fechar',
                  onClose: () => {
                    this.props.history.push('/listacontas');
                    window.location.reload();
                  }
                });
              } else {
                this.props.history.push('/listacontas');
                window.location.reload();
              }
            })

        ) : (
            console.log('None')
        )
    ) 
  }
}

export default CallBack;

Я отправляю токен пользователя для моего бэкенда и его возвращаемый токен с информацией для обратного вызова.

Я надеюсь сделать весь процесс невидимым только для пользователяпоказывает сообщение об успехе.

...