0

The local storage should only hold state for the task where the completed button has been pressed. Currently when I refresh the page all the tasks are marked as completed.

<template>
<button type="button" v-bind:class="order_button_style" @click="on_order_button_click()">
  {{ buttonText }}
</button>
</div>
</template>

<script>
export default {

props: 'itemId', required: true,
data() {
    return {
        index: 'this.itemId',
        status: ''
    }
},
methods: {
on_order_button_click() {
  this.status = !this.status;
  localStorage.setItem('index', this.status);
}

},
mounted() {

this.status = localStorage.getItem('index') === "true";
},
computed: {
buttonText() {
  return this.status === true ? "Completed" : "Complete";
},
 order_button_style() {
  return this.status === true
    ? "btn btn-danger"
    : "btn btn-primary";
}

}

};
</script>
jsmith
  • 47
  • 2
  • 10

1 Answers1

1

In your calls to set localstorage, you are setting the item with a key of string 'index', not the index data property of your component. With what you have now, all components are reading from the same localstorage key.

For example, change:

mounted() {
    this.status = localStorage.getItem('index') === "true";
},

To:

mounted() {
    this.status = localStorage.getItem(this.index) === "true";
},

Also, I don't think the way you are setting a data property equal to a passed property works. I've never seen that before. I think you are just setting your index data property equal to the string literal 'this.itemId'.

I've rewritten your component trying to clean what errors I think there are. Let me know if this works.

<template>
    <button type="button" v-bind:class="order_button_style" @click="on_order_button_click()">
        {{ buttonText }}
    </button>
</template>

<script>
export default {
    props: {
        itemId: {
            type: String,
            required: true,
        }
    },
    data() {
        return {
            status: false,
        }
    },
    methods: {
        on_order_button_click() {
            this.status = !this.status;
            localStorage.setItem(this.itemId, this.status);
        }
    },
    mounted() {
        this.status = localStorage.getItem(this.itemId) === "true";
    },
    computed: {
        buttonText() {
            return this.status === true ? "Completed" : "Complete";
        },
        order_button_style() {
            return this.status === true ?
                "btn btn-danger" :
                "btn btn-primary";
        }

    }

};
</script>

Dylan Landry
  • 1,150
  • 11
  • 27
  • Thank you. You're right, I've removed the '' from setItem and getItem and also from index: index: this.itemId,. Now it looks like localStorage.setItem(this.index, this.status); } }, mounted() { this.status = localStorage.getItem(this.index) === "true"; – jsmith Jun 01 '20 at 20:25
  • This is still updating every task after page reload – jsmith Jun 01 '20 at 20:26
  • I just saw your update. Your code seems to work, I wonder what the difference was between what I tried in above comments – jsmith Jun 01 '20 at 20:32