All files / components/Admin/AdminCustomers/CustomersTable CustomerTable.vue

100% Statements 6/6
100% Branches 2/2
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                                                                                                                                                                1x   1x 1x   1x   2x                                                                                                                                                                                                                 1x        
<template>
  <div class="d-flex flex-column w-100 h-100">
    <div class="d-flex justify-content-between">
      <h6 class="w-100 text-left p-3 m-0">Customers</h6>
      <button
        v-on:click.prevent="$emit('setIsTable')"
        class="btn btn-success text-nowrap my-auto mr-2"
      >
        Add customer
      </button>
    </div>
    <vue-good-table
      mode="remote"
      :columns="columns"
      :rows="customers"
      :select-options="{
        enabled: true,
        selectOnCheckboxOnly: true,
      }"
      :search-options="{ enabled: true }"
      :sort-options="{ enabled: true }"
      :pagination-options="{
        enabled: true,
        position: 'top',
        dropdownAllowAll: false,
        infoFn: (params) =>
          `Showing ${params.firstRecordOnPage} to ${params.lastRecordOnPage} of page ${params.currentPage}`,
      }"
      :totalRows="total"
      @on-page-change="onPageChange"
      @on-per-page-change="onPerPageChange"
      @on-cell-click="onCellClick"
      @on-sort-change="onSortChange"
      @on-selected-rows-change="selectionChanged"
      @on-search="onSearch"
      @on-select-all="onSelectAll"
      styleClass="vgt-table"
      compactMode
    >
      <template slot="table-row" slot-scope="props">
        <div v-if="props.column.field == 'username'">
          <p class="text-ellipsis m-0">{{ getCustomerName(props.row) }}</p>
        </div>
        <div
          v-else-if="props.column.field == 'blocked'"
          class="d-flex align-items-center"
        >
          <p class="m-0">{{ props.row.blocked ? "true" : "false" }}</p>
        </div>
        <div
          v-else-if="props.column.field == 'orders_count'"
          class="d-flex align-items-center"
        >
          <p class="m-0">{{ props.row.orders.length || 0 }} orders</p>
        </div>
        <div
          v-else-if="props.column.field == 'total_price'"
          class="d-flex align-items-center"
        >
          <p class="m-0">$ {{ props.row.total_price | formatNumber }}</p>
        </div>
        <div v-else class="d-flex align-items-center">
          <p class="text-ellipsis m-0">
            {{ props.formattedRow[props.column.field] }}
          </p>
        </div>
      </template>
      <div slot="selected-row-actions">
        <button class="btn btn-warning" v-on:click="blockItems(true)">
          Block
        </button>
        <button class="btn btn-success" v-on:click="blockItems(false)">
          Unblock
        </button>
        <button class="btn btn-danger" v-on:click="deleteItems">Delete</button>
      </div>
    </vue-good-table>
    <ConfirmModal :id="'confirm-delete-users'" v-on:confirm="confirm" />
  </div>
</template>
 
<script>
import { mapGetters, mapMutations, mapActions } from "vuex";
import { prevCurrNextItems } from "~/helpers";
 
import ConfirmModal from "~/components/Admin/common/ConfirmModal.vue";
 
import "~/utils/filters";
 
export default {
  name: "CustomerTable",
  components: { ConfirmModal },
  data: () => ({
    selectedRows: [],
    timer: null,
    columns: [
      {
        label: "Customer",
        field: "username",
      },
      {
        label: "Blocked",
        field: "blocked",
      },
      {
        label: "Email",
        field: "email",
      },
      {
        label: "Location",
        field: "state",
      },
      {
        label: "Orders",
        field: "orders_count",
      },
      {
        label: "Spent",
        field: "total_price",
      },
    ],
  }),
  computed: {
    ...mapGetters({
      customers: "admin_customers/customers",
      params: "admin_customers/params",
      total: "admin_customers/total",
    }),
  },
  methods: {
    prevCurrNextItems,
    ...mapActions({
      deleteCustomers: "admin_customers/deleteCustomers",
      blockCustomers: "admin_customers/blockCustomers",
      getCustomers: "admin_customers/getCustomers",
    }),
    ...mapMutations({
      setSelectedCustomers: "admin_customers/setSelectedCustomers",
      setParams: "admin_customers/setParams",
    }),
    onPageChange(params) {
      this.setParams({ ...this.params, page: params.currentPage });
      this.getCustomers();
    },
    onPerPageChange(params) {
      this.setParams({ ...this.params, currentPerPage: params.currentPerPage });
      this.getCustomers();
    },
    onCellClick: function (params) {
      const result = this.prevCurrNextItems(params.row, this.customers);
      this.setSelectedCustomers(result);
    },
    selectionChanged(params) {
      this.selectedRows = params.selectedRows;
    },
    onSelectAll(params) {
      this.selectedRows = params.selectedRows;
    },
    onSearch(params) {
      clearTimeout(this.timer);
      this.timer = null;
 
      this.setParams({ ...this.params, search: params.searchTerm });
 
      this.timer = setTimeout(() => {
        this.getCustomers();
      }, 1000);
    },
    onSortChange(params) {
      this.setParams({ ...this.params, sort: params[0] });
      this.getCustomers();
    },
    deleteItems() {
      this.$root.$emit("bv::show::modal", "confirm-delete-users");
    },
    confirm: async function () {
      const ids = this.selectedRows.map((item) => item.id);
      await this.deleteCustomers(ids);
    },
    async blockItems(blocked) {
      const ids = this.selectedRows.map((item) => item.id);
      await this.blockCustomers({ ids, blocked });
    },
    getCustomerName: function (customer) {
      const { first_name, last_name, username } = customer;
 
      if (first_name) {
        return `${first_name} ${last_name}`;
      }
      return username;
    },
  },
};
</script>
 
<style scoped>
</style>