Skip to content
Snippets Groups Projects
RentHistoryItem.vue 3.9 KiB
Newer Older
Gilgard's avatar
Gilgard committed
<template>
  <!-- A card for a rent history containing the item's title,
       name of the person who rented it, dates/renting period, and a button
       to rate if not already rated it. -->
Gilgard's avatar
Gilgard committed
  <div
    class="bg-white shadow dark:bg-gray-800 select-none cursor-pointer hover:bg-gray-50"
  >
    <!-- Title -->
Gilgard's avatar
Gilgard committed
    <p class="font-bold mx-4" id="title">
      {{ historyItem.listing.title }}
    </p>

    <!-- Name of the user who rented -->
Gilgard's avatar
Gilgard committed
    <div class="flex flex-row items-center">
      <div class="flex flex-col px-4 flex-1">
Titus Netland's avatar
Titus Netland committed
        <router-link :to="{ path: '/profile/' + user.userId }">
Gilgard's avatar
Gilgard committed
          Leid til: {{ user.firstName }} {{ user.lastName }}
Gilgard's avatar
Gilgard committed
        </router-link>
Gilgard's avatar
Gilgard committed
      </div>

      <!-- Period it was rented for -->
Gilgard's avatar
Gilgard committed
      <div class="flex flex-col flex-1">
Gilgard's avatar
Gilgard committed
        <div>
          Fra:
          {{ this.getDateString(historyItem.fromTime, isMultipleDays) }}
        </div>
        <div>
          Til: {{ this.getDateString(historyItem.toTime, isMultipleDays) }}
        </div>
      </div>

      <!-- Button to rate -->
Gilgard's avatar
Gilgard committed
      <colored-button
Gilgard's avatar
Gilgard committed
        v-if="!isRated"
Gilgard's avatar
Gilgard committed
        :text="'Vurder'"
        class="px-4 flex-1"
        @click="
          $emit('rate', {
            show: true,
            name: user.firstName + ' ' + user.lastName,
            title: historyItem.listing.title,
            rentID: historyItem.rentId,
            renterIsReceiverOfRating: !currentUserIsRenter,
          })
        "
      />
Gilgard's avatar
Gilgard committed
    </div>
  </div>
</template>

<script>
Gilgard's avatar
Gilgard committed
import ColoredButton from "@/components/BaseComponents/ColoredButton.vue";
import { parseCurrentUser } from "@/utils/token-utils";
Gilgard's avatar
Gilgard committed
import userService from "@/services/user.service";
Gilgard's avatar
Gilgard committed

export default {
  name: "RentHistoryItem",
Gilgard's avatar
Gilgard committed
  components: {
    ColoredButton,
  },
  data() {
    return {
      user: {},
Titus Netland's avatar
Titus Netland committed
      isRated: true,
Gilgard's avatar
Gilgard committed
    };
  },
Gilgard's avatar
Gilgard committed
  props: {
    historyItem: {
      rentId: Number,
Gilgard's avatar
Gilgard committed
      fromTime: Number,
      toTime: Number,
      isAccepted: Boolean,
      listing: {
        listingID: Number,
        title: String,
        pricePerDay: Number,
        address: String,
        userID: Number,
      },
Gilgard's avatar
Gilgard committed
      renterId: Number,
    },
  },
  computed: {
    currentUser() {
      return parseCurrentUser();
    },
    //Checks if the rent period was multiple days or not
Gilgard's avatar
Gilgard committed
    isMultipleDays() {
Gilgard's avatar
Gilgard committed
      if (this.historyItem.toTime - this.historyItem.fromTime < 86400000) {
Gilgard's avatar
Gilgard committed
        return false;
      }
      return true;
    },
    /**
     * computed the total price based on how long the renting period was.
     */
Gilgard's avatar
Gilgard committed
    price() {
      if (this.isMultipleDays) {
        let numDays = Math.round(
Gilgard's avatar
Gilgard committed
          (this.historyItem.toTime - this.historyItem.fromTime) / 86400000
Gilgard's avatar
Gilgard committed
        );
Gilgard's avatar
Gilgard committed
        return this.historyItem.listing.pricePerDay * numDays;
Gilgard's avatar
Gilgard committed
      }
Gilgard's avatar
Gilgard committed
      return this.historyItem.listing.pricePerDay;
Gilgard's avatar
Gilgard committed
    },
    /**
     * Checks if the user rented its own item
     */
Gilgard's avatar
Gilgard committed
    currentUserIsRenter() {
      return this.isCurrentUser(this.historyItem.renterId);
    },
Gilgard's avatar
Gilgard committed
  },
  methods: {
    /**
     * returns a date as a string
     */
Gilgard's avatar
Gilgard committed
    getDateString(milliseconds) {
Gilgard's avatar
Gilgard committed
      let today = new Date();
Gilgard's avatar
Gilgard committed
      let date = new Date(milliseconds);
      const shortMonthOfRentHistoryItem = date.toLocaleString("default", {
        month: "short",
      });
      let dateString = date.getDate() + ". " + shortMonthOfRentHistoryItem;
Gilgard's avatar
Gilgard committed

      if (date.getFullYear() != today.getFullYear()) {
        dateString += "." + date.getFullYear();
      }
Gilgard's avatar
Gilgard committed
      return dateString;
    },
    isCurrentUser(id) {
      return id == this.currentUser.accountId;
    },
  },
  /**
   * Gets the user and checks if a rating is saved, before mounting the page.
   */
Gilgard's avatar
Gilgard committed
  async beforeCreate() {
    if (this.currentUserIsRenter) {
      this.user = await userService.getUserFromId(
        this.historyItem.listing.userID
      );
    } else {
      this.user = await userService.getUserFromId(this.historyItem.renterId);
    }
    this.isRated = await userService.isRated(this.historyItem.rentId);
Gilgard's avatar
Gilgard committed
  },
};
</script>