What is the best approach in VueJS to implement a skeleton loader and an empty page condition for my orders page simultaneously?

I have implemented a skeleton loader to display while the data is loading. However, I want to also show an empty order page if there is no data or orders coming in. I am trying to figure out the conditions for both scenarios - displaying the loader and the empty basket page. As a newcomer to Vuetify, I am currently stuck on how to achieve this. Below is my code snippet:

<template>
  <v-container>
    <div class="text-h2" style="margin-bottom: 20px;">
      Order History
    </div>
    <div v-if="orders.length < 1">
        <v-skeleton-loader
            v-for="n in 5"
            :key="n"
            class="flex-row"
            elevation="2"
            max-width="100%"
            style="margin-bottom: 20px"
            transition="scale-transition"
            type="list-item-avatar-three-line"
        >
        </v-skeleton-loader>
    </div>
    <v-card
        v-else
        v-for="order in orders"
        :key="order.id"
        class="mx-auto google-font mb-6"
        max-width="auto">
        <v-list-item-content>
            <v-row>
                <v-col cols="10">
                    <v-list-item-title class="mb-2 ml-4" style="font-size: 100%; margin-right: 5px">OrderID: {{ order.id }}</v-list-item-title>
                    <v-list-item-subtitle style="color: grey; font-size: 100%;" class="ml-4">Items {{ order.ITEMS.length }}</v-list-item-subtitle>
                </v-col>
                <v-col cols="2">
                    <v-list-item-title class="float-right mb-2 mr-6" style="font-size: 100%;">₹{{ order.TOTAL_COST }}</v-list-item-title>
                    <v-list-item-subtitle style="color: grey; font-size: 85%;" class="mr-6 float-right">{{ parseDate(order.ORDER_DATE) }}</v-list-item-subtitle>
                </v-col>
            </v-row>
            <v-divider></v-divider>
        </v-list-item-content>
        <v-btn text small style="margin-bottom: 10px; margin-left: 5px;" v-on:click="switchExpand(order.id)" :color="getStoreColor">Details<v-icon>{{
          expandedItems.indexOf(order.id) > -1 ? "mdi-chevron-up" : "mdi-chevron-down"
        }}</v-icon></v-btn>
    </v-card>
    <v-btn outlined rounded :color="getStoreColor" @click="logout">Logout</v-btn>
  </v-container>
</template>

Answer №1

Alright, it seems like you're in need of two conditions for your skeleton loader implementation. Instead of using v-if="orders.length < 1", which is incorrect for a loading state, try creating a separate reactive property to track the loading state. Let's assume you have

data() {
   return {
      loading: true // data is loading initially
   }
}

When making your API call, handle the loading state like this:

methods: {
   async fetchData() {
      this.loading = true // show the skeleton loader
      const apiRes = await this.myAPICall()
      this.loading = false // hide the skeleton loader after loading finishes
   }
}

This way, regardless of the response containing orders or not, the loader will be hidden based on the loading property's state only.

Your current condition checking if the length is greater than 1 might not work as expected when the API returns empty orders (length equals zero). Moving on to the second and final condition, let's check if there are any orders available with the following code:

computed: {
   isEmptyState() {
      return this.orders.length < 1 && !this.loading
   }
}

The above code indicates that we display a placeholder only when there are no orders (this.orders.length < 1) and the loading process has finished (!this.loading). To implement this, use the following snippet:

<div v-if="isEmptyState">Add your desired placeholder content here</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When I try to run Parcel, my ReactJS website just won't deploy in a serverless environment

For a while now, I've been working on a website using serverless. Everything was going smoothly until this morning when I encountered an issue with pushing updates from my site to the serverless platform. When trying to push, I received the following ...

Increase the count if the item is already in the shopping cart - React

Is there a way to avoid adding the same product to the basket multiple times and instead just increment a counter? How can I effectively keep track of each item's count? this.state = { description: '', commaSalesPrice: '', ...

Nuxt is unable to modify the data variable

When working with Vue in Nuxt, I often encounter an issue where changes to a variable in the data section of my component do not reflect in the HTML. For example: export default { data () { return { openPopup: '1' } }, methods: ...

Styling Process Steps in CSS

Just starting out with CSS! I'm looking to replicate the Process Step design shown in the image. https://i.stack.imgur.com/Cq0jY.png Here's the code I've experimented with so far: .inline-div{ padding: 1rem; border: 0.04rem gray ...

Having trouble with your jQuery animation on a div?

Check out this jsFiddle example: http://jsfiddle.net/uM68j/ After clicking on one of the links in the demo, the bar is supposed to smoothly slide to the top. However, instead of animating, it immediately jumps to the top. How can I modify the code to ac ...

How can I transfer data to a different component in Angular 11 that is not directly related?

Within the home component, there is a line that reads ...<app-root [message]="hii"> which opens the app-root component. The app-root component has an @input and {{message}} in the HTML is functioning properly. However, instead of opening t ...

Extracting information from JSON using arrays

I'm facing a bit of a challenge with this one. I've been trying to use jQuery on my website to update an element. It works perfectly fine without using an array of data in JSON, but as soon as I introduce an array of data, it stops functioning. I ...

What could be causing this JavaScript if statement to malfunction?

I found myself with some free time and decided to create a basic API using JavaScript. What I thought would be simple turned into a frustrating mistake. Oddly enough, my if/else statement isn't working correctly - it only executes the code within the ...

Mastering callback functions within AngularJS animations

As I delve into AngularJS animations, I am currently working on a slide-show animation: app.animation('slide-show', function () { return { setup: function (element) { }, start: function (element, done) { e ...

Implementation of multiple angular guards causing a crash on the page

I have been attempting to implement separate guards for distinct pages. Essentially, I am checking a boolean parameter to determine if a user is logged in or not. Below are the two guard.ts codes that I am using: export class IsAuthenticatedGuard implemen ...

Handling Errors in Asynchronous Functions with JavaScriptLet's explore the best practices for

I am a beginner in javascript and recently delved into async/await. After going through various resources, I gained a basic understanding. However, while experimenting with some code examples, I encountered unexpected results which left me puzzled about wh ...

Wordpress causing Jquery to malfunction; PHP function not executing

Looking to incorporate a script into my WordPress function.php file. Here's what I have so far: <?php function add_google_jquery() { if ( !is_admin() ) { wp_deregister_script('jquery'); wp_register_script('jquery', ...

Dynamic form name validation in Angular is crucial for ensuring the accuracy and

When it comes to validating a form in Angular, I usually use the ng-submit directive like this: <form name="formName" ng-submit="formName.$valid && submitForm()"></form> This method works well for forms with predefined names that I se ...

Transferring information within React components

Looking for some assistance with the code below. I'm facing an issue where the data is not being submitted along with the form, even though the correct values are displayed in the form. I have included a user query to fetch data from another object an ...

What is causing my ajax request to malfunction?

I'm encountering an issue while trying to send a request using AJAX. The request is successful when I use the following webservice: https://jsonplaceholder.typicode.com/users However, when I attempt to make the same request with my own service, I fac ...

Having trouble with your custom AngularJS directive not functioning correctly?

I am facing an issue with my custom directive implementation. I have a custom directive that contains a table which references a controller. The ProjectController part works fine when it is not included in the code, but as soon as I put everything into the ...

Ancient queries carry the weight of time

Extremely ancient, very old, incredibly aged beyond belief ...

Exploring Material UI: Understanding the contrast in functionalities between incorporating the Icon component and the Material Icons node

I am looking to incorporate Material Icons into my application. I have come across two methods provided by Material UI for adding the same icon to my site: Using the <Icon /> component, which is part of the @material-ui/core package: <!-- Add t ...

Display HTML in JavaScript without altering the Document Object Model

Is it possible to style a custom HTML tag called "location" without directly modifying the DOM? For instance, having <location loc-id="14" address="blah" zipcode="14" /> Would it be feasible to render it like this: <div class="location"> ...

An easy guide to dynamically assigning a property using jQuery

I am currently utilizing the toastr plugin and I would like to dynamically set the options using a JSON object that is retrieved from an AJAX call. I am encountering some difficulties in setting the options property and value programmatically. Below is a s ...