This repository has been archived on 2021-09-06. You can view files and clone it, but cannot push or open issues or pull requests.
vuejs_course/2021-09-04_composition-13-d.../src/components/projects/ProjectsList.vue

82 lines
1.7 KiB
Vue
Raw Normal View History

2021-09-04 15:18:48 +02:00
<template>
2021-09-04 16:01:46 +02:00
<base-container v-if="userSearch">
2021-09-04 15:18:48 +02:00
<h2>{{ user.fullName }}: Projects</h2>
2021-09-04 16:01:46 +02:00
<base-search
v-if="hasProjects"
@search="updateSearch"
:search-term="enteredSearchTerm"
></base-search>
2021-09-04 15:18:48 +02:00
<ul v-if="hasProjects">
2021-09-04 16:01:46 +02:00
<project-item
v-for="prj in availableProjects"
:key="prj.id"
:title="prj.title"
></project-item>
2021-09-04 15:18:48 +02:00
</ul>
<h3 v-else>No projects found.</h3>
</base-container>
<base-container v-else>
<h3>No user selected.</h3>
</base-container>
</template>
<script>
2021-09-04 16:01:46 +02:00
import { ref, computed, watch } from 'vue';
2021-09-04 15:18:48 +02:00
import ProjectItem from './ProjectItem.vue';
export default {
components: {
2021-09-04 16:01:46 +02:00
ProjectItem
2021-09-04 15:18:48 +02:00
},
props: ['user'],
2021-09-04 16:01:46 +02:00
setup(props) {
const activeSearchTerm = ref('');
const enteredSearchTerm = ref('');
const availableProjects = computed(function() {
if (activeSearchTerm.value) {
return props.user.projects.filter(prj =>
prj.title.includes(activeSearchTerm.value)
2021-09-04 15:18:48 +02:00
);
}
2021-09-04 16:01:46 +02:00
return props.user.projects;
});
const hasProjects = computed(function() {
return props.user.projects && availableProjects.value.length > 0;
});
watch(enteredSearchTerm, function(newValue) {
2021-09-04 15:18:48 +02:00
setTimeout(() => {
if (newValue === enteredSearchTerm.value) {
activeSearchTerm.value = newValue;
2021-09-04 15:18:48 +02:00
}
}, 300);
2021-09-04 16:01:46 +02:00
});
watch(props.user, function() {
enteredSearchTerm.value = '';
});
2021-09-04 16:01:46 +02:00
function updateSearch(val) {
enteredSearchTerm.value = val;
}
return {
enteredSearchTerm,
activeSearchTerm,
hasProjects,
availableProjects,
updateSearch
2021-09-04 16:01:46 +02:00
};
}
2021-09-04 15:18:48 +02:00
};
</script>
<style scoped>
ul {
list-style: none;
margin: 0;
padding: 0;
}
2021-09-04 16:01:46 +02:00
</style>