甜蜜警报2:如何根据response.status

时间:2019-04-15 13:45:19

标签: javascript sweetalert2

如果团队名称可用,我正在使用AJAX + Sweet Alert 2添加新团队,如果团队名称已在使用中,则返回错误。如果团队名称尚不存在,我可以使用过渡模式,但无法弄清楚如何将屏幕更改为“团队已创建”,并给它一个红色的X而不是绿色的勾号。

teams = {
  modalTeam: function(type) {
    if (type == 'create-team') {
      swal({
        title: 'Create a New Team',
        html: '<div class="form-group">' +
          '<input id="team_name" type="text" class="form-control" />' +
          '</div>',
        confirmButtonClass: 'btn btn-info',
        confirmButtonText: 'Create Team',
        buttonsStyling: false,
        backdrop: `
                    rgba(0,0,123,0.4)
                    url("/static/img/nyan-cat.gif")
                    center left
                    no-repeat
                  `,
        preConfirm: () => {
          var team_name = document.getElementById("team_name").value;
          return fetch(`/team/create/${team_name}`)
            .then(response => {
              if (response.status == 204) {
                // Here is where I want it to push to a different modal like the below swal but only error
                }

              return response.json()
            })
        },
      }).then(function(result) {
        swal({
          type: 'success',
          html: 'You created team: <strong>' + $('#team_name').val() + '</strong>',
          confirmButtonClass: 'btn btn-success',
          buttonsStyling: false
        })
      }).catch(swal.noop)
    }
}

0 个答案:

没有答案