[FRAMEWORK] Xây dựng page Account Phần 5 (ok)

Favorite Activity, Business

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\js\account-business.js

jQuery(document).ready(function($) {
  function account_activity_load(type) {
    $(".status").removeClass("d-none");
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      dataType: "text",
      data: {
        action: 'account_activity_load_list_' + type,
        token: $("#nonce_token").val()
      },
      success: function(output) {
        $(".tab-" + type + " .account-list-item").append(output);
        $(".status").addClass("d-none");
      }
    });
  }
  function account_get_tab_number(type = "activity") {
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      dataType: "text",
      data: {
        action: 'account_activity_get_tab_number',
        type: type,
        token: $("#nonce_token").val()
      },
      success: function(output) {
        if (output != "error") {
          const obj = JSON.parse(output, function(key, value) {
            $(".account-load-btn[data-id='" + key + "'] span").text(value);
          });
        }
      }
    });
  }
  function account_business_load(type) {
    $(".status").removeClass("d-none");
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      dataType: "text",
      data: {
        action: 'account_business_load_list_' + type,
        token: $("#nonce_token").val()
      },
      success: function(output) {
        //is_busy = false;
        $(".tab-" + type + " .account-list-item").append(output);
        $(".status").addClass("d-none");
      }
    });
  }
  // ==
  account_activity_load("deal");
  account_get_tab_number();
  // ==
  $('body').on('click', '#change-acc', function(e) {
    const $this = $(this);
    const btnScan = $('#scan-voucher');
    const type = $this.attr('data-type');
    const userId = $this.attr('data-id');
    const tab_active = $(".tab-active").data("tab-id");
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      dataType: "text",
      data: {
        action: 'change_business_activity',
        token: $('#nonce_token').val(),
        type: type,
        id: userId,
      },
      beforeSend: function() {
        $(".account-list-item:not('.news') .col-6").not(".col-addnew").remove();
        $this.find('svg').addClass('spin');
      },
      success: function(response) {
        $(".menu-tab-account .li").not(".active").find("a").addClass("load");
        $this.find('svg').removeClass('spin');
        btnScan.toggle();
        if (type == 'activity') {
          // xử lý data business
          account_get_tab_number("business");
          $this.attr('data-type', 'business').find('span').text(app.business);
          $(".account-list-item .col-addnew").removeClass("d-none");
          if (tab_active != "news") {
            account_business_load(tab_active);
          }
          $(".menu-tab-account ul li:last-child,.tab-news").addClass("d-none");
          $(".col-activity-event").remove();
          return;
        }
        $this.attr('data-type', 'activity').find('span').text(app.activity);
        $(".account-list-item .col-addnew").addClass("d-none");
        account_get_tab_number();
        if (tab_active != "news") {
          account_activity_load(tab_active);
        }
        $(".menu-tab-account ul li:last-child,.tab-news").removeClass("d-none");
      },
      error: function() {
        $this.find('svg').removeClass('spin');
        alert('Error');
      }
    })
    return false;
  });
  // ==
  var exclude_img = [];
  var input_btn = 0;
  var xp = -1;
  $(document).on("click", ".add-image-btn", function(e) {
    let select = $(this).closest("form");
    input_btn++;
    select.find(".list-input-file").append("<input type='file' name='upload_files[]' id='filenumber" + input_btn + "' class='img_file upload_files' accept='.gif,.jpg,.jpeg,.png,' multiple/>");
    $("#filenumber" + input_btn).click();
  });
  $(document).on("change", ".upload_files", function(e) {
    let select = $(this).closest("form")
    files = e.target.files;
    filesLength = files.length;
    for (var i = 0; i < filesLength; i++) {
      var f = files[i];
      var res_ext = files[i].name.split(".");
      var img_or_video = res_ext[res_ext.length - 1];
      var fileReader = new FileReader();
      fileReader.name = f.name;
      fileReader.onload = function(e) {
        xp++;
        var file = e.target;
        select.find(".images-box").append("<div class='box preview-image'><div class='images-preview' style='background-image: url(" + e.target.result + ")'><button type='button' data-id='" + xp + "' class='remove-img btn-close' title='Remove'></button></div></div>");
      };
      fileReader.readAsDataURL(f);
    }
  });
  $(document).on("click", ".images-box .remove-img", function() {
    $(this).closest(".box").remove();
    exclude_img.push($(this).attr("data-id"));
  });
  // ==
  var is_busy = false;
  var add_deal_form = "#add-deal-form ";
  $(add_deal_form).submit(function() {
    if (is_busy == true) return;
    is_busy = true;
    var formData = new FormData(document.getElementById("add-deal-form"));
    formData.append('action', 'add_deal');
    formData.append('exclude_img', exclude_img);
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      contentType: false,
      processData: false,
      data: formData,
      success: function(output) {
        is_busy = false;
        if (output == "success") {
          $("#alert-success").modal("show");
          $("#add_deal_form").remove();
        }
      }
    });
    return false;
  });
  $(document).on("click", ".deal.add-favorite", function() {
    if (is_busy == true) return;
    is_busy = true;
    let select = $(this);
    $.ajax({
      url: app.ajaxUrl,
      type: "post",
      dataType: "text",
      data: {
        action: 'deal_favorite_process',
        id: $(this).attr("data-id"),
        token: $(this).attr("data-token"),
      },
      success: function(output) {
        $(".status").addClass("d-none");
        is_busy = false;
        if (output == "success") {
          select.toggleClass("added");
        }
      }
    })
  });
});

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\functions.php

C:\xampp\htdocs\wordpress8\wp-content\themes\addframwork\framework\ajax\deal.php

Last updated

Was this helpful?