Skip to content

Instantly share code, notes, and snippets.

@JeffreyWay
Last active June 25, 2023 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JeffreyWay/1b86c5e7c71c5aca7c3e211e44497b63 to your computer and use it in GitHub Desktop.
Save JeffreyWay/1b86c5e7c71c5aca7c3e211e44497b63 to your computer and use it in GitHub Desktop.
Learn Vue 3: Step By Step, Episode 14, More Flexible Components With Slots and Flags - https://laracasts.com/series/learn-vue-3-step-by-step/episodes/14
import Assignment from "./Assignment.js";
import AssignmentTags from "./AssignmentTags.js";
export default {
components: { Assignment, AssignmentTags },
template: `
<section v-show="assignments.length" class="w-60">
<div class="flex justify-between items-start">
<h2 class="font-bold mb-2">
{{ title }}
<span>({{ assignments.length }})</span>
</h2>
<button v-show="canToggle" @click="$emit('toggle')">&times;</button>
</div>
<assignment-tags
v-model:currentTag="currentTag"
:initial-tags="assignments.map(a => a.tag)"
></assignment-tags>
<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>
<slot></slot>
</section>
`,
props: {
assignments: Array,
title: String,
canToggle: { type: Boolean, default: false }
},
data() {
return {
currentTag: 'all',
};
},
computed: {
filteredAssignments() {
if (this.currentTag === 'all') {
return this.assignments;
}
return this.assignments.filter(a => a.tag === this.currentTag);
}
}
}
import AssignmentList from "./AssignmentList.js";
import AssignmentCreate from "./AssignmentCreate.js";
export default {
components: { AssignmentList, AssignmentCreate },
template: `
<section class="flex gap-8">
<assignment-list :assignments="filters.inProgress" title="In Progress">
<assignment-create @add="add"></assignment-create>
</assignment-list>
<div v-show="showCompleted">
<assignment-list
:assignments="filters.completed"
title="Completed"
can-toggle
@toggle="showCompleted = !showCompleted"
></assignment-list>
</div>
</section>
`,
data() {
return {
assignments: [],
showCompleted: true
}
},
computed: {
filters() {
return {
inProgress: this.assignments.filter(assignment => ! assignment.complete),
completed: this.assignments.filter(assignment => assignment.complete)
};
}
},
created() {
fetch('http://localhost:3001/assignments')
.then(response => response.json())
.then(assignments => {
this.assignments = assignments;
});
},
methods: {
add(name) {
this.assignments.push({
name: name,
completed: false,
id: this.assignments.length + 1
});
}
}
}
{
"scripts": {
"start": "npx serve & npx json-server db.json -p 3001"
},
"devDependencies": {
"json-server": "^0.17.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment