Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active November 10, 2022 13:02
Show Gist options
  • Save JeffreyWay/6ae646ce3bb5a8d02e7b34d2a66db589 to your computer and use it in GitHub Desktop.
Save JeffreyWay/6ae646ce3bb5a8d02e7b34d2a66db589 to your computer and use it in GitHub Desktop.
Learn Vue 3: Step by Step, Episode 10 - It's All So Easy - https://laracasts.com/series/learn-vue-3-step-by-step/episodes/10
import Assignment from "./Assignment.js";
export default {
components: { Assignment },
template: `
<section v-show="assignments.length">
<h2 class="font-bold mb-2">
{{ title }}
<span>({{ assignments.length }})</span>
</h2>
<div class="flex gap-2">
<button
@click="currentTag = tag"
v-for="tag in tags"
class="border rounded px-1 py-px text-xs"
:class="{
'border-blue-500 text-blue-500': tag === currentTag
}"
>{{ tag }}</button>
</div>
<ul class="border border-gray-600 divide-y divide-gray-600 mt-6">
<assignment
v-for="assignment in filteredAssignments"
:key="assignment.id"
:assignment="assignment"
></assignment>
</ul>
</section>
`,
props: {
assignments: Array,
title: String
},
data() {
return {
currentTag: 'all'
};
},
computed: {
filteredAssignments() {
if (this.currentTag === 'all') {
return this.assignments;
}
return this.assignments.filter(a => a.tag === this.currentTag);
},
tags() {
return ['all', ...new Set(this.assignments.map(a => a.tag))];
}
}
}
import AssignmentList from "./AssignmentList.js";
import AssignmentCreate from "./AssignmentCreate.js";
export default {
components: { AssignmentList, AssignmentCreate },
template: `
<section class="space-y-6">
<assignment-list :assignments="filters.inProgress" title="In Progress"></assignment-list>
<assignment-list :assignments="filters.completed" title="Completed"></assignment-list>
<assignment-create @add="add"></assignment-create>
</section>
`,
data() {
return {
assignments: [
{ name: 'Finish project', complete: false, id: 1, tag: 'math' },
{ name: 'Read Chapter 4', complete: false, id: 2, tag: 'science' },
{ name: 'Turn in Homework', complete: false, id: 3, tag: 'math' },
],
}
},
computed: {
filters() {
return {
inProgress: this.assignments.filter(assignment => ! assignment.complete),
completed: this.assignments.filter(assignment => assignment.complete)
};
}
},
methods: {
add(name) {
this.assignments.push({
name: name,
completed: false,
id: this.assignments.length + 1
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment