add a composition api excersise

This commit is contained in:
Andreas Zweili 2021-09-04 15:18:48 +02:00
parent 57a4f57109
commit 8407158887
19 changed files with 464 additions and 1 deletions

View File

@ -0,0 +1,3 @@
> 1%
last 2 versions
not dead

View File

@ -0,0 +1,17 @@
module.exports = {
root: true,
env: {
node: true
},
'extends': [
'plugin:vue/vue3-essential',
'eslint:recommended'
],
parserOptions: {
parser: 'babel-eslint'
},
rules: {
'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off'
}
}

View File

@ -0,0 +1,22 @@
.DS_Store
node_modules
/dist
# local env files
.env.local
.env.*.local
# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@ -0,0 +1,3 @@
{
"singleQuote": true
}

View File

@ -0,0 +1,5 @@
module.exports = {
presets: [
'@vue/cli-plugin-babel/preset'
]
}

View File

@ -0,0 +1,23 @@
{
"name": "vue-first-app",
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
"dependencies": {
"core-js": "^3.6.5",
"vue": "^3.0.0"
},
"devDependencies": {
"@vue/cli-plugin-babel": "~4.5.0",
"@vue/cli-plugin-eslint": "~4.5.0",
"@vue/cli-service": "~4.5.0",
"@vue/compiler-sfc": "^3.0.0-0",
"babel-eslint": "^10.1.0",
"eslint": "^6.7.2",
"eslint-plugin-vue": "^7.0.0-0"
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link rel="icon" href="<%= BASE_URL %>favicon.ico">
<title><%= htmlWebpackPlugin.options.title %></title>
</head>
<body>
<noscript>
<strong>We're sorry but <%= htmlWebpackPlugin.options.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>

View File

@ -0,0 +1,67 @@
<template>
<main>
<user-list :users="activeUsers" @list-projects="selectUser"></user-list>
<projects-list :user="selectedUser"></projects-list>
</main>
</template>
<script>
import USER_DATA from './dummy-data.js';
import UserList from './components/users/UserList.vue';
import ProjectsList from './components/projects/ProjectsList.vue';
export default {
components: {
UserList,
ProjectsList,
},
data() {
return {
selectedUser: null,
activeUsers: USER_DATA,
};
},
methods: {
selectUser(uid) {
this.selectedUser = this.activeUsers.find((usr) => usr.id === uid);
},
},
};
</script>
<style>
* {
box-sizing: border-box;
}
html {
font-family: sans-serif;
}
body {
margin: 0;
}
main {
display: flex;
justify-content: space-around;
}
button {
font: inherit;
border: 1px solid #00006b;
background-color: transparent;
color: #00006b;
padding: 0.5rem 1.5rem;
cursor: pointer;
margin: 0.5rem 0.5rem 0.5rem 0;
}
button:hover,
button:active {
background-color: #efefff;
}
button.selected {
background-color: #00006b;
color: white;
}
</style>

View File

@ -0,0 +1,14 @@
<template>
<section>
<slot></slot>
</section>
</template>
<style scoped>
section {
margin: 2rem;
border: 1px solid #797979;
padding: 1rem;
flex: 1;
}
</style>

View File

@ -0,0 +1,33 @@
<template>
<form>
<input type="search" @input="search" :value="searchTerm" placeholder="Filter items" />
</form>
</template>
<script>
export default {
props: ['searchTerm'],
emits: ['search'],
methods: {
search(event) {
this.$emit('search', event.target.value);
},
},
};
</script>
<style scoped>
input {
font: inherit;
width: 100%;
display: block;
padding: 0.15rem;
border: 1px solid #ccc;
}
input:focus {
outline: none;
border-color: #00006b;
background-color: #eeeeff;
}
</style>

View File

@ -0,0 +1,24 @@
<template>
<li>
<h3>{{ title }}</h3>
</li>
</template>
<script>
export default {
props: ['title'],
};
</script>
<style scoped>
li {
background-color: #00006b;
color: white;
padding: 0.5rem;
margin: 1rem 0;
}
li h3 {
margin: 0;
}
</style>

View File

@ -0,0 +1,68 @@
<template>
<base-container v-if="user">
<h2>{{ user.fullName }}: Projects</h2>
<base-search v-if="hasProjects" @search="updateSearch" :search-term="enteredSearchTerm"></base-search>
<ul v-if="hasProjects">
<project-item v-for="prj in availableProjects" :key="prj.id" :title="prj.title"></project-item>
</ul>
<h3 v-else>No projects found.</h3>
</base-container>
<base-container v-else>
<h3>No user selected.</h3>
</base-container>
</template>
<script>
import ProjectItem from './ProjectItem.vue';
export default {
components: {
ProjectItem,
},
props: ['user'],
data() {
return {
enteredSearchTerm: '',
activeSearchTerm: '',
};
},
computed: {
hasProjects() {
return this.user.projects && this.availableProjects.length > 0;
},
availableProjects() {
if (this.activeSearchTerm) {
return this.user.projects.filter((prj) =>
prj.title.includes(this.activeSearchTerm)
);
}
return this.user.projects;
},
},
methods: {
updateSearch(val) {
this.enteredSearchTerm = val;
},
},
watch: {
enteredSearchTerm(val) {
setTimeout(() => {
if (val === this.enteredSearchTerm) {
this.activeSearchTerm = val;
}
}, 300);
},
user() {
this.enteredSearchTerm = '';
},
},
};
</script>
<style scoped>
ul {
list-style: none;
margin: 0;
padding: 0;
}
</style>

View File

@ -0,0 +1,31 @@
<template>
<li>
<h3>{{ userName }}</h3>
<button @click="viewProjects">View Projects</button>
</li>
</template>
<script>
export default {
props: ['id', 'userName'],
emits: ['list-projects'],
methods: {
viewProjects() {
this.$emit('list-projects', this.id);
},
},
};
</script>
<style scoped>
li {
margin: 0.5rem 0;
padding: 1rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.26);
border: 1px solid #ccc;
}
li h3 {
margin: 0;
}
</style>

View File

@ -0,0 +1,91 @@
<template>
<base-container>
<h2>Active Users</h2>
<base-search @search="updateSearch" :search-term="enteredSearchTerm"></base-search>
<div>
<button @click="sort('asc')" :class="{selected: sorting === 'asc'}">Sort Ascending</button>
<button @click="sort('desc')" :class="{selected: sorting === 'desc'}">Sort Descending</button>
</div>
<ul>
<user-item
v-for="user in displayedUsers"
:key="user.id"
:user-name="user.fullName"
:id="user.id"
@list-projects="$emit('list-projects', $event)"
></user-item>
</ul>
</base-container>
</template>
<script>
import UserItem from './UserItem.vue';
export default {
components: {
UserItem,
},
props: ['users'],
data() {
return {
enteredSearchTerm: '',
activeSearchTerm: '',
sorting: null,
};
},
computed: {
availableUsers() {
let users = [];
if (this.activeSearchTerm) {
users = this.users.filter((usr) =>
usr.fullName.includes(this.activeSearchTerm)
);
} else if (this.users) {
users = this.users;
}
return users;
},
displayedUsers() {
if (!this.sorting) {
return this.availableUsers;
}
return this.availableUsers.slice().sort((u1, u2) => {
if (this.sorting === 'asc' && u1.fullName > u2.fullName) {
return 1;
} else if (this.sorting === 'asc') {
return -1;
} else if (this.sorting === 'desc' && u1.fullName > u2.fullName) {
return -1;
} else {
return 1;
}
});
},
},
methods: {
updateSearch(val) {
this.enteredSearchTerm = val;
},
sort(mode) {
this.sorting = mode;
},
},
watch: {
enteredSearchTerm(val) {
setTimeout(() => {
if (val === this.enteredSearchTerm) {
this.activeSearchTerm = val;
}
}, 300);
}
},
};
</script>
<style scoped>
ul {
list-style: none;
margin: 0;
padding: 0;
}
</style>

View File

@ -0,0 +1,33 @@
export default [
{
id: 'max',
fullName: 'Maximilian Schwarzmüller',
projects: [
{ id: 'max_p1', title: 'Record the Vue course' },
{ id: 'max_p2', title: 'Create more courses' },
{ id: 'max_p3', title: 'Keep content updated' }
]
},
{
id: 'manu',
fullName: 'Manuel Lorenz',
projects: [
{ id: 'manu_p1', title: 'Create more courses' },
{ id: 'manu_p2', title: 'Dive into data science topics' },
{ id: 'manu_p3', title: 'Various things' }
]
},
{
id: 'julie',
fullName: 'Julie Jones',
projects: [
{ id: 'julie_p1', title: 'Create agenda for next months' },
{ id: 'julie_p2', title: 'Explore new topics' }
]
},
{
id: 'michael',
fullName: 'Michael Miller',
projects: []
}
];

View File

@ -0,0 +1,12 @@
import { createApp } from 'vue';
import BaseSearch from './components/UI/BaseSearch.vue';
import BaseContainer from './components/UI/BaseContainer.vue';
import App from './App.vue';
const app = createApp(App);
app.component('base-search', BaseSearch);
app.component('base-container', BaseContainer);
app.mount('#app');

View File

@ -8,4 +8,4 @@ services:
ports:
- 8080:8080
volumes:
- ./2021-09-04_composition-assignment-2-problem/:/app
- ./2021-09-04_composition-13-demo-starting-project/:/app