All files / components/Admin/common/Customer CustomerMain.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 198 199 200 201 202 203 204 205 206                                                                                                                                                                              1x   1x 1x   1x 3x                                                                                                                                         1x                                                                                      
<template>
  <div class="row w-100 h-100 justify-content-center m-0">
    <div class="d-flex flex-column col-10">
      <div class="d-flex align-items-start mt-3">
        <button v-on:click="back" class="button">
          <BIconArrowLeft />
        </button>
        <div v-if="customer" class="w-100 px-3">
          <div class="d-flex flex-column">
            <h6 class="m-0 px-2 font-weight-bold">{{ getCustomerName() }}</h6>
            <span>{{ customer.state }} {{ customerDuration() }}</span>
          </div>
        </div>
      </div>
      <div
        v-if="!customer"
        class="h-100 d-flex justify-content-center align-items-center"
      >
        <p>User wasn't find</p>
      </div>
      <div v-else class="row mb-3">
        <div class="col-9">
          <div class="block w-100 d-flex justify-content-between mb-3 p-3">
            <div>
              <h6 class="text-center font-weight-bold">
                $ {{ ordersSpent | formatNumber }}
              </h6>
              <p>Amount spent</p>
            </div>
            <div>
              <h6 class="text-center font-weight-bold">
                {{ customerOrders.length }}
              </h6>
              <p>Orders</p>
            </div>
            <div>
              <h6 class="text-center font-weight-bold">
                $ {{ averageValue() | formatNumber }}
              </h6>
              <p>Average order value</p>
            </div>
          </div>
          <div
            class="block w-100 d-flex flex-column p-3"
            v-if="!customerOrders.length"
          >
            <p class="text-center">Customer hasn`t any orders</p>
            <button class="btn btn-success" v-on:click="$emit('openAddOrder')">
              Add order
            </button>
          </div>
          <div class="block w-100" v-else>
            <div v-if="viewAll === false" class="p-3">
              <h6 class="w-100">Last order place</h6>
              <CustomerOrder :order="customerOrders[0]" />
            </div>
            <div v-else class="p-3">
              <h6 class="w-100">All orders</h6>
              <CustomerOrder
                v-for="customerOrder in customerOrders"
                :order="customerOrder"
                :key="customerOrder.id"
              />
            </div>
            <div
              class="bottom d-flex justify-content-end align-items-center p-3"
            >
              <a
                href="#"
                v-on:click.prevent="viewAll = !viewAll"
                class="mr-2"
                >{{ viewAll ? "Hide all orders" : "View all orders" }}</a
              >
              <button
                class="btn btn-success"
                v-on:click="$emit('openAddOrder')"
              >
                Add order
              </button>
            </div>
          </div>
        </div>
        <CustomerRightSide class="col-3" :customer="customer" />
      </div>
    </div>
  </div>
</template>
 
<script>
import { prevCurrNextItems } from "~/helpers";
import "~/utils/filters";
 
import CustomerOrder from "./CustomerOrder.vue";
import CustomerRightSide from "./CustomerRightSide.vue";
 
export default {
  name: "CustomerMain",
  components: {
    CustomerOrder,
    CustomerRightSide,
  },
  props: {
    customer: Object,
    back: Function,
  },
  data: () => ({
    viewAll: false,
    customerOrders: [],
    ordersSpent: 0,
  }),
  methods: {
    prevCurrNextItems,
    averageValue: function () {
      const { total_price, orders_count } = this.customerOrders;
      return total_price / orders_count;
    },
    customerDuration: function () {
      const { created_at } = this.customer;
      const regDate = new Date(created_at);
 
      const intervals = [
        { label: "year", seconds: 31536000 },
        { label: "month", seconds: 2592000 },
        { label: "day", seconds: 86400 },
        { label: "hour", seconds: 3600 },
        { label: "minute", seconds: 60 },
        { label: "second", seconds: 1 },
      ];
 
      const seconds = Math.floor((Date.now() - regDate.getTime()) / 1000);
      const interval = intervals.find((i) => i.seconds < seconds);
      const count = Math.floor(seconds / interval.seconds);
      return `${count} ${interval.label}${count !== 1 ? "s" : ""} ago`;
    },
    getCustomerName: function () {
      const { first_name, last_name, username } = this.customer;
 
      if (first_name && last_name) {
        return `${first_name} ${last_name}`;
      }
      if (username) {
        return username;
      }
      return "undefined";
    },
  },
  async mounted() {
    if (this.customer) {
      const { orders } = this.customer;
 
      if (orders && orders.length) {
        this.customerOrders = orders.sort(
          (a, b) => new Date(b.created_at) - new Date(a.created_at)
        );
 
        this.ordersSpent = this.customerOrders.reduce(
          (acc, order) => (acc += order.total),
          0
        );
      }
    }
  },
};
</script>
 
<style scoped>
.button {
  border-radius: 5px;
  background: none;
}
 
.image-wrap:hover > .image-buttons {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
}
 
.icon-circle-wrap {
  border: 4px solid #ade9d0;
  border-radius: 50%;
}
 
.block-main {
  border-bottom: 1px solid black;
}
 
.wrap-img > span {
  top: -1rem;
  right: -0.2rem;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background-color: #e4e5e7;
}
 
.wrap-img > img {
  border: 2px solid #f3f4f5;
  border-radius: 5px;
}
 
.bottom {
  border-top: 1px solid #000;
}
</style>