mirror of
https://github.com/kevin-DL/build-gmail-clone-with-vue-3.git
synced 2026-01-17 05:04:51 +00:00
Two refactors: one to make it reactive, the other to make it work
The one that made it work was storing `ids` outside of the useEmailSelection function. That makes it shared between the multiple uses of the useEmailSelection function.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
<template>
|
||||
<h1>VMail Inbox</h1>
|
||||
|
||||
Number selected: {{selectedEmailIds.size}}
|
||||
|
||||
<p>Number selected: {{emailSelection.ids.size}}</p>
|
||||
|
||||
<MailTable :emails="emails" />
|
||||
</template>
|
||||
@@ -15,9 +15,9 @@
|
||||
let response = await fetch('/api/emails');
|
||||
let {emails} = await response.json();
|
||||
|
||||
let {selectedEmailIds} = useEmailSelection();
|
||||
let {emailSelection} = useEmailSelection();
|
||||
|
||||
return {emails, selectedEmailIds}
|
||||
return {emails, emailSelection}
|
||||
},
|
||||
components: {
|
||||
MailTable
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
<template>
|
||||
<p>Number selected: {{emailSelection.ids.size}}</p>
|
||||
|
||||
<table>
|
||||
<tbody>
|
||||
<tr v-for="email in emails" :key="email.id" :class="[email.read ? 'read' : '']">
|
||||
<td>
|
||||
<input type="checkbox"
|
||||
:checked="selectedEmailIds.has(email.id)"
|
||||
@click="toggleEmailSelection(email.id)" />
|
||||
:checked="emailSelection.ids.has(email.id)"
|
||||
@click="emailSelection.toggle(email.id)" />
|
||||
</td>
|
||||
<td>{{email.from}}</td>
|
||||
<td>
|
||||
@@ -23,8 +25,8 @@
|
||||
|
||||
export default {
|
||||
setup(){
|
||||
let {selectedEmailIds, toggleEmailSelection} = useEmailSelection();
|
||||
return {format, selectedEmailIds, toggleEmailSelection}
|
||||
let {emailSelection} = useEmailSelection();
|
||||
return {format, emailSelection,}
|
||||
},
|
||||
props: {
|
||||
emails: {
|
||||
|
||||
@@ -1,19 +1,21 @@
|
||||
import { ref } from 'vue';
|
||||
import { ref, reactive } from 'vue';
|
||||
|
||||
let ids = new Set(['1', '5']);
|
||||
export const useEmailSelection = function(){
|
||||
let selectedEmailIds = ref(new Set(['1', '5']))
|
||||
|
||||
let toggleEmailSelection = (id) => {
|
||||
if(selectedEmailIds.value.has(id)) {
|
||||
selectedEmailIds.value.delete(id)
|
||||
} else {
|
||||
selectedEmailIds.value.add(id);
|
||||
let emailSelection = reactive({
|
||||
ids: ids,
|
||||
toggle: function(id) {
|
||||
if(this.ids.has(id)) {
|
||||
this.ids.delete(id)
|
||||
} else {
|
||||
this.ids.add(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return {
|
||||
selectedEmailIds,
|
||||
toggleEmailSelection
|
||||
emailSelection,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user