To prevent a new member from being added when the form is submitted without any data or with an email that already exists, you need to update the createMember action in the TeamStore.js file to return a boolean value indicating whether the member was successfully created or not. Then, in the form submission handler, you can check this value and show the appropriate flash message.
Here's how you can update the createMember action:
createMember(name, email) {
if (this.members.length === this.max) {
flashMessage('The team is full', 'error');
return false;
} else if (this.members.find((m) => m.email === email)) {
flashMessage('The email is already in use', 'error');
return false;
} else if (!name || !email) {
flashMessage('The name and email are required', 'error');
return false;
} else {
// work out what the next id is
let ids = this.members.map((m) => m.id);
let max = Math.max(...ids);
// create the member
let member = {};
member.id = max + 1;
member.name = name;
member.email = email;
member.status = 'pending';
member.avatar = `https://i.pravatar.cc/50?u=${member.email}`;
this.members.push(member);
// show a flash message
flashMessage('The team member is added', 'success');
return true;
}
},
And here's how you can update the form submission handler:
<form class="flex flex-col gap-y-4" @submit.prevent="handleSubmit">
<!-- form fields -->
<button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">Add Member</button>
</form>
methods: {
async handleSubmit() {
const created = this.team.createMember(this.name, this.email);
if (created) {
this.showModal = false;
flashMessage('The team member is added', 'success');
}
},
},
In the form submission handler, we call the createMember action and store the returned value in a variable called created. If created is true, we close the modal and show a success flash message. If created is false, we don't close the modal and show an error flash message based on the condition that was not met in the createMember action.