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

72 lines
1.5 KiB
Vue
Raw Normal View History

2021-09-04 15:18:48 +02:00
<template>
2021-09-04 16:45:51 +02:00
<base-container v-if="user">
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
2021-09-06 21:37:16 +02:00
v-for="prj in availableItems"
2021-09-04 16:01:46 +02:00
: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-06 21:37:16 +02:00
import { computed, watch, toRefs } from 'vue';
2021-09-04 15:18:48 +02:00
import ProjectItem from './ProjectItem.vue';
2021-09-06 21:37:16 +02:00
import useSearch from '../../hooks/search';
2021-09-04 15:18:48 +02:00
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) {
2021-09-06 21:37:16 +02:00
//const propsWithRefs = toRefs(props);
//const user = propsWithRefs.user;
const { user } = toRefs(props);
2021-09-04 16:01:46 +02:00
2021-09-06 21:37:16 +02:00
const projects = computed(function() {
return user.value ? user.value.projects : [];
2021-09-04 16:01:46 +02:00
});
2021-09-06 21:37:16 +02:00
const { enteredSearchTerm, availableItems, updateSearch } = useSearch(
projects,
'title'
);
const hasProjects = computed(function() {
2021-09-06 21:37:16 +02:00
return props.user.projects && availableItems.value.length > 0;
});
2021-09-04 16:49:18 +02:00
watch(user, function() {
2021-09-06 21:52:25 +02:00
updateSearch('');
});
2021-09-04 16:01:46 +02:00
return {
enteredSearchTerm,
hasProjects,
2021-09-06 21:37:16 +02:00
availableItems,
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>