How to filter computed value with computed value - vuex

<template>
<div>
<q-card
v-for="country in filteredData"
:key="country"
>
<q-card-section>
<div>{{ country.Country }}</div>
</q-card-section>
</q-card>
<q-inner-loading
:showing="summaryDataFetchStatus"
/>
</div>
</template>
<script>
import { useStore } from "vuex";
import { defineComponent, computed, onMounted, watch } from "vue";
export default defineComponent({
name: "CovidSummaryDataComponent",
setup() {
const store = useStore();
onMounted(() => {
store.dispatch("CovidStore/updateCovidSummary");
});
const summaryData = computed(() => store.getters["CovidStore/summaries"]);
const headerInputValue = computed(() => store.getters["CountrySearchBarStore/getSearchBarValue"]);
const summaryDataFetchStatus = computed(() => store.getters["CovidStore/getSummaryDataFetchStatus"]);
const filteredData = computed(() => summaryData.value.filter(country => country.Country.includes(headerInputValue.value)));
return {
filteredData,
summaryData,
headerInputValue ,
summaryDataFetchStatus
};
},
});
</script>
Hi all. I am trying to filter summaryData by headerInputValue but filteredData doesn't return array. It throws TypeError: Cannot read properties of null (reading 'filter') error. If i use v-for="country in summaryData" instead of v-for="country in filteredData" it work properly so it means summaryData is array. Then why i can't use filter on summaryData ?

Related

Prop being passed but not updating the component

So I have a function called updateAll(data.items) which should take the array and randomly assign a new name, it appears the data.items is updating when I view in vue dev tools and is being passed to the component but not actually updating the browser
I've also added in my buildData.js this is being used for the same app but in react so was trying to keep it as general as I could
//HomeView.vue
<script setup>
import { reactive, watch } from "vue";
import Hero from '#/components/Hero.vue'
import FunctionButton from '#/components/FunctionButton.vue'
import ItemsContainer from '#/components/ItemContainer.vue';
import * as dataFunc from '#/data/buildData'
const data = reactive({
items: JSON.parse(localStorage.getItem('items')) || []
})
console.log(data.items)
watch(() => data.items,(newValue) => {
localStorage.setItem('items', JSON.stringify(newValue))
}, {deep: true});
</script>
<template>
<div class='w-2/3 mx-auto text-center'>
<Hero HeroText="The one stop shop to store recipes!" />
<main>
<h1 class="text-5xl font-extrabold dark:text-white">Recipe Manager</h1>
<div class='functions gap-4 grid grid-cols-2 md:grid-cols-3 mt-8 mx-auto w-fit'>
<FunctionButton name="Add new recipe" #click="() => data.items = [...data.items,...dataFunc.buildData(1)]"/>
<FunctionButton name="Add 100 recipes" #click="() => data.items = [...data.items,...dataFunc.buildData(100)]"/>
<FunctionButton name="Add 1000 recipes" #click="() => data.items = [...data.items,...dataFunc.buildData(1000)]"/>
<FunctionButton name="Delete all recipes" #click="() => data.items = dataFunc.deleteAll()"/>
<FunctionButton name="Edit all recipes" #click="() => data.items = dataFunc.updateAll(data.items)"/>
</div>
<ItemsContainer :items="data.items"/>
</main>
</div>
</template>
//ItemContainer.vue
<script setup>
import Item from '#/components/Item.vue';
const props = defineProps({
items: {type: Array, default: () => []},
})
</script>
<template>
<div
class='items-container w-full md:w-max bg-gray-800 dark:bg-gray-800 text-white dark:text-black mx-auto mt-12 p-4 space-y-4'>
<Item
v-for="item in items"
v-if="items.length > 0"
:key="item.id"
:meal="item"
/>
<p v-else class='text-white'>No items to display</p>
</div>
</template>
I tried to make this as generic as possible as trying to make a similar app on Vue and React so they could share this
import { v4 as uuidv4 } from 'uuid';
function create(){
const data = {
id: uuidv4(),
category: category[random(category.length)],
name: meals[random(meals.length)],
prepTime: randomTime(),
cookTime: randomTime(),
isFav: false
}
return data
}
function random(max){
return Math.round(Math.random() * 1000) % max;
}
const meals = [
"Roast Chicken", "Omelette", "Steak Pie", "Beef Stir Fry", "Fish And Chips", "Tomato And Prawn Risotto", "Pepperoni Pizza", "Cheesy Nachos", "Fajitas", "Baked Potato",
"Full English Breakfast", "Pancakes", "Chocolate Brownies", "Meatballs With Red Pepper Sauce", "Chicken Cesar Salad", "Beef Burger", "Chips","Macaroni Cheese", "Fillet Steak", "Chicken Wings",
"BBQ Ribs", "Tomato Soup", "Prawn Dim Sum", "Pork Gyozas", "Tomato Bruschetta", "Spring Rolls", "Beef Jerky", "Lasagne", "Spagetti Carbonara", "Salmon And Potatoes"
]
const category = [
"Breakfast", "Lunch", "Dinner"
]
function randomTime(){
return Math.round(Math.random() * 30)
}
function buildData(count){
const data = new Array(count);
for(let i = 0; i<count; i++){
data[i] = create();
}
return data;
}
function updateAll(currentArray){
return currentArray.map((item) => {
return{...item, name: meals[random(meals.length)], }
})
}
function updateOne(item){
return{...item, name: meals[random(meals.length)], }
}
function favouriteAll(currentArray = []){
return currentArray.map((item) => {
return {...item, isFav: true}
})
}
function deleteAll(){
const data = []
return data;
}
export {buildData, deleteAll, favouriteAll, updateAll, updateOne};
Check the console for errors.
If I take the functionality from HomeView.vue, your ItemContainer.vue and an Item.vue (which you didn't provided), then it works.
Check the working Vue SFC Playground
The code:
App.vue
<script setup>
import { ref, watch, reactive } from 'vue'
import ItemsContainer from './ItemContainer.vue';
const data = reactive({
items: []
})
console.log(data.items)
watch(() => data.items, (newValue) => {
alert(JSON.stringify(newValue))
}, {deep: true});
const addItem = () => {
data.items.push({ id: data.items.length, name: 'New Item'})
}
</script>
<template>
<ItemsContainer :items="data.items"/><br />
<button type="button" #click="addItem()">Add</button>
</template>
ItemContainer.vue is the same.
Item.vue
<script setup>
const props = defineProps({
meal: {
type: Object,
default: {undefined}
}
})
</script>
<template>
<div>
{{JSON.stringify(meal)}}<br />
</div>
</template>
UPDATE
I've updated the playground with your functions and it still does work well. The problem is somewhere else.

How do i get a Vue Compsable to only work with a single target item in a list

I have a composable that takes a function and calls the function upon the long-press of a component that uses it(the composable). however, if I use it in a list of components, it seems to call the function on all the components instead of only the component/item I long-pressed in the list. How do I make it so that the function is only called on the target component/item?
The composable(/composables/useLongPress)
import { ref, onMounted, onUnmounted } from 'vue';
const longPress = (binding) => {
const isHeld = ref(false);
const activeHoldTimer = ref(null);
const onHoldStart = () => {
isHeld.value = true;
activeHoldTimer.value = setTimeout(() => {
if (isHeld.value) {
binding();
}
}, 1000);
};
const onHoldEnd = () => {
isHeld.value = false;
if (activeHoldTimer.value) {
clearTimeout(activeHoldTimer.value);
}
};
onMounted(() => {
window.addEventListener('mousedown', onHoldStart);
window.addEventListener('touchstart', onHoldStart);
window.addEventListener('click', onHoldEnd);
window.addEventListener('mouseout', onHoldEnd);
window.addEventListener('touchend', onHoldEnd);
window.addEventListener('touchcancel', onHoldEnd);
});
onUnmounted(() => {
window.removeEventListener('mousedown', onHoldStart);
window.removeEventListener('touchstart', onHoldStart);
window.removeEventListener('click', onHoldEnd);
window.removeEventListener('mouseout', onHoldEnd);
window.removeEventListener('touchend', onHoldEnd);
window.removeEventListener('touchcancel', onHoldEnd);
});
};
export default longPress;
The child component/item (/InvoiceListItem.vue)
<template>
<div class="grid grid-cols-2 py-4">
<div>
<p class="text-lg font-medium">{{ invoice.client }}</p>
<p class="mt-2 text-xs font-light text-gray-500">Due {{ invoice.due }}</p>
</div>
<div class="flex flex-col items-end">
<p class="text-lg font-bold">${{ invoice.amount }}</p>
<base-pill
:variant="invoice.status"
:label="capitalize(invoice.status)"
class="mt-2"
/>
</div>
</div>
</template>
<script setup>
import BasePill from '../../BasePill.vue';
import { capitalize } from '../../../utils';
import useLongPress from '../../../composables/useLongPress';
const props = defineProps({
invoice: {
type: Object,
required: true,
},
});
const log = () => console.log(props.invoice);
useLongPress(log);
</script>
<style lang="postcss" scoped></style>
The parent/list of components (/InvoiceList.vue)
<template>
<div class="divide-y">
<div v-for="(invoice, index) in invoices" :key="index">
<invoice-list-item :invoice="invoice" />
</div>
</div>
</template>
<script setup>
import InvoiceListItem from './InvoiceListItem.vue';
defineProps({
invoices: {
type: Array,
default: () => [],
},
});
</script>
<style lang="scss" scoped></style>
when I long press on one invoice item I am supposed to log the value of the one item but instead, I get the value of all items in the list

How to pass vuelidate object with props for jest test?

I have several components inside each of them have fields that are validated with vuelidate. All field validation rules are described in the parent component wrapper in the child components, I pass the object :v="$v", everything works. But in the test, when mounting, I get an error, and as I understand it, because when mounting, I do not pass this object, tell me how to do it right?
parent wrapper
<template>
<VModal>
<template v-slot:content>
<div class="layer">
<institutes-info
:v="$v"
ref="info-component"
/>
</div>
</template>
</VModal>
</template>
export default {
name: "WrapModalInstitutes",
validations: {
si: {
name: {
required,
maxLength: maxLength(256),
},
},
},
}
child component
<template>
<form action="" id="form-info" #submit.prevent="uploadInfo($event, si.id)">
<p class="sub-text">{{ $t("vnz.info") }}</p>
<div class="institut-info">
<div class="input-group" :class="{ 'form-group--error': v.si.name.$error, 'form-group--success': !v.si.name.$invalid }">
<label for="name-institut">{{ $t("vnz.nameVNZ") }}</label>
<div class="input-container">
<input id="name-institut" name="title" type="text" :placeholder="$t('vnz.nameVNZ')" v-model.trim="name" #keydown="trimSpecialCharacters($event)" #input="trimSpecialCharacters($event, 'name')">
<div class="error" v-if="!v.si.name.required && v.si.name.$dirty">{{ $t("vnz.notEmpty") }}</div>
</div>
</div>
</div>
<button v-if="edit" type="submit" class="btn-primary edit" :disabled="submitStatus === 'PENDING'">{{ $t("vnz.save") }}</button>
</form>
</template>
export default {
name: "InstitutesInfoModal",
props: ['v'],
}
test file
import Vuex from "vuex"
import {mount, createLocalVue, config} from "#vue/test-utils"
import InstitutesInfo from '../../assets/src/components/InstitutesInfo'
import Vuelidate from 'vuelidate'
import VueTheMask from 'vue-the-mask'
import translations from "../../assets/src/common-resources/translations/translations"
import fetchMock from 'jest-fetch-mock'
const locale = "uk";
config.mocks["$t"] = msg => translations[locale][msg]
fetchMock.enableMocks()
const localVue = createLocalVue()
localVue.use(Vuex)
localVue.use(Vuelidate)
localVue.use(VueTheMask)
describe('testing institute-info component', () => {
let store;
let mutations;
let actions;
beforeEach(() => {
mutations = {
'institutes/EDIT_SI_OBJ': jest.fn(),
'institutes/SUBMIT_STATUS': jest.fn()
}
actions = {
'institutes/UPLOAD_INFO': jest.fn()
}
store = new Vuex.Store({
modules: {
institutes: {
state: {
si: {
name: null,
},
submitStatus: null
},
mutations,
actions
}
}
})
})
test('some test"',() => {
const wrapper = mount(InstitutesInfo, {
store, localVue
})
})
})
[Vue warn]: Error in render: "TypeError: Cannot read property 'si' of undefined"

Vuex store Getter loadings faster than store State

I have a nuxtJS vue2 components as follows:
<template>
<div class="product-fullpage">
<div class="product-card">
<img :src="productById(this.$route.params.id).imageURL">
<div class="product-headings">
<div class="product-info" v-animate-on-scroll>
<h1>{{ productById(this.$route.params.id).name }}</h1>
<h2>£{{ productById(this.$route.params.id).price }}</h2>
</div>
<div class="product-cart" v-animate-on-scroll>
<div class="quantity-info">
<label for="quantity">Quantity:</label>
<input v-model.number="productInfo.quantity" name="quantity" type="number" min="0" max="99"/>
</div>
<button #click="addToCart" :disabled="noQuantity">Add To Cart ></button>
</div>
</div>
</div>
</div>
</template>
I'm using a getter that does the following:
<script>
import { mapGetters } from 'vuex'
export default {
computed: {
...mapGetters(['productById'])
},
}
</script>
Here's the getter
export const getters = {
productById: (state) => (id) => {
return state.products.find(product => product.id === id)
},
}
my state is set to pull from firebase
export const actions = {
async setProducts({commit}) {
let colRef = collection(db, 'products')
const querySnapshot = await getDocs(colRef)
querySnapshot.forEach((doc) => {
const imageDownloadURL = getDownloadURL(ref(storage, `${doc.data().imageRef}`))
.then( url => {
// console.log(url)
let article = ({
id: doc.id,
name: doc.data().name,
price: doc.data().price,
description: doc.data().description,
imageURL: url
})
commit('setProducts', article)
})
})
},
}
the mutation to set the state:
export const mutations = {
setProducts(state, article) {
let matchProduct = state.products.find(product => product.id == article.id)
if(!matchProduct) {
state.products.push(article)
}
},
}
and this is my state:
export const state = () => ({
products: [],
})
i thought that if i load everything beforehand in default.vue under 'layouts' that i can then have the store.state.products set.
<template>
<div class="container">
<!-- <nuxt /> -->
<nuxt v-if="!loading"/>
<div class="overlay" v-else>
Loading...
</div>
</div>
</template>
<script>
export default {
created() {
this.loading = true
this.$store.dispatch('setCart')
this.$store.dispatch('setProducts')
.finally(() => (this.loading=false))
},
data() {
return {
loading: false,
}
},
}
</script>
sorry if this is turning out to be a long post - but basically on initial load, I get my imageURL, name and price. but then on reload it comes out empty. i believe the getter is occurring before the store is loaded. how do i set it so that i can state.products.find(product => product.id == article.id) for my getter after state is loaded?

Vue 3 Composition API...how to replace getElementById

I wrote the following in Vue 3 Composition API.
If you look on the "onMounted" I'm attaching an event listener to the window to keep the status box to the bottom of the screen. I'm using absolute position and bottom 0.
I'm wondering if I can make this more "Vue" by replacing the getElementById? I tried refs but it's not working.
Any suggestions or should I leave well enough alone?
Thanks
<template>
<div>
<transition name="toast">
<div id="slide" class="slide-modal" v-if="statuses.length">
<div class="clear-notifications" #click='clearAllNotifications()'>Clear all notifications</div>
<div v-for="status in statuses" :key="status.id" class="status-div">
<div>{{ status.text }}</div>
<div #click="closeStatus(status)"><span class="-x10-cross"></span></div>
</div>
</div>
</transition>
</div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, watch, onMounted } from "vue";
import { useStore } from "../model/DataStore";
export default defineComponent({
setup() {
const store = useStore();
const statuses = ref([]);
const statusMessage = computed(() => store.state.statusMessage);
function addStatus(newMessage) {
statuses.value.push({
id: statuses.value.length + 1,
text: newMessage
})
}
watch(statusMessage, (newValue: string, oldValue: string) => {
addStatus(statusMessage.value);
})
onMounted(() => {
window.addEventListener("scroll", function (e) {
let vertical_position = 0;
vertical_position = pageYOffset;
if(document.getElementById("slide")){
document.getElementById('slide').style.bottom = -(vertical_position) + 'px';
}
});
})
return {
store,
statuses,
addStatus
};
},
methods: {
clearAllNotifications() {
this.statuses = []
},
closeStatus(elm: any) {
const index = this.statuses.map((status) => status.id).indexOf(elm.id);
this.statuses.splice(index, 1);
}
}
})
</script>
and here's the slide modal style:
.slide-modal {
max-height: 200px;
width: 500px;
background-color: #f2f2f2;
color: #505050;
padding: 8px;
display: flex;
flex-direction: column;
gap: 8px;
overflow-x: hidden;
position: absolute;
bottom: 0;
}
The docs give a pretty simple example
<template>
<div ref="root">This is a root element</div>
</template>
<script>
import { ref, onMounted } from 'vue'
export default {
setup() {
const root = ref(null)
onMounted(() => {
// the DOM element will be assigned to the ref after initial render
console.log(root.value) // <div>This is a root element</div>
})
return {
root
}
}
}
</script>
use ref() when creating
pass to template in return function
use ref="..." in template
and in onMounted, access via ref.value
so for your code it would be...
<template>
<div>
<transition name="toast">
<div ref="slideRef" class="slide-modal" v-if="statuses.length">
<div class="clear-notifications" #click='clearAllNotifications()'>Clear all notifications</div>
<div v-for="status in statuses" :key="status.id" class="status-div">
<div>{{ status.text }}</div>
<div #click="closeStatus(status)"><span class="-x10-cross"></span></div>
</div>
</div>
</transition>
</div>
</template>
<script lang="ts">
import { defineComponent, ref, computed, watch, onMounted } from "vue";
import { useStore } from "../model/DataStore";
export default defineComponent({
setup() {
const store = useStore();
const statuses = ref([]);
const slideRef = ref();
const statusMessage = computed(() => store.state.statusMessage);
function addStatus(newMessage) {
statuses.value.push({
id: statuses.value.length + 1,
text: newMessage
})
}
watch(statusMessage, (newValue: string, oldValue: string) => {
addStatus(statusMessage.value);
})
onMounted(() => {
window.addEventListener("scroll", function (e) {
let vertical_position = 0;
vertical_position = pageYOffset;
if(slideRef.value){
slideRef.value.style.bottom = -(vertical_position) + 'px';
}
});
})
return {
store,
statuses,
addStatus,
slideRef
};
},
})
</script>