Skip to content

Commit

Permalink
Added update and delete tags functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MoustafaShaaban committed Apr 20, 2024
1 parent 0a1b74e commit 9c2eea9
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 1 deletion.
130 changes: 130 additions & 0 deletions src/components/TagDetail.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<template>
<q-page class="flex flex-center">
<q-card v-if="isEditing" flat bordered class="my-card">
<q-card-section class="row items-center q-pb-none q-mb-md" vertical>
<div class="text-h6">Edit: {{ tag.name }}</div>
</q-card-section>

<q-card-section>
<q-form @submit.prevent="handleSubmit">
<q-input filled v-model="tag.name" label="Tag Name" required lazy-rules
:rules="[val => val && val.length > 0 || 'Tag Name is required']" />


<q-page-sticky position="bottom-right" :offset="[18, 18]">
<q-btn type="submit" fab icon="done" color="primary">
</q-btn>
</q-page-sticky>
</q-form>
</q-card-section>

<q-page-sticky position="bottom-left" :offset="[18, 18]">
<q-btn @click="isEditing = false" fab icon="undo" color="negative">
</q-btn>
</q-page-sticky>
</q-card>


<q-card v-else class="my-card q-mt-md" flat bordered>
<q-card-section>
<div class="row items-center no-wrap">
<div class="col">
<div class="text-h6">{{ tag.name }}</div>
</div>
</div>
</q-card-section>


<q-card-actions>
<q-btn color="primary" type="button" size="sm" @click="isEditing = true">Edit</q-btn>
<q-btn color="negative" type="button" size="sm" @click="confirm">Delete</q-btn>
</q-card-actions>
</q-card>

<q-page-sticky position="bottom-left" :offset="[18, 18]">
<q-btn :to="{ 'name': 'tags' }" fab icon="undo" color="negative">
</q-btn>
</q-page-sticky>
</q-page>
</template>

<script setup>
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Dialog, date, Notify } from 'quasar';
import { useNotesStore } from '../stores/notes.js';
const notesStore = useNotesStore();
const route = useRoute();
const router = useRouter();
const isEditing = ref(false);
const tag = ref({
id: '',
name: ''
});
tag.value = notesStore.getTagById(route.params.id);
function handleSubmit() {
try {
notesStore.updateTag(route.params.id, tag.value);
isEditing = false;
Notify.create({
message: 'Tag Updated Successfully',
type: "positive",
actions: [
{ icon: 'close', color: 'white', round: true, }
]
})
} catch (error) {
Notify.create({
message: error.message,
type: "negative",
actions: [
{ icon: 'close', color: 'white', round: true, }
]
})
}
};
function confirm(id) {
Dialog.create({
dark: true,
title: 'Confirm',
color: 'primary',
message: 'Delete this tag?, Your Notes that has this tag will not be deleted',
cancel: true,
persistent: true
}).onOk(() => {
try {
notesStore.deleteTag(id)
router.push('/tags');
Notify.create({
message: 'Tag Deleted Successfully',
type: "positive",
actions: [
{ icon: 'close', color: 'white', round: true, }
]
})
} catch (error) {
Notify.create({
message: error.message,
type: "negative",
actions: [
{ icon: 'close', color: 'white', round: true, }
]
})
}
})
};
</script>


<style lang="sass" scoped>
.my-card
width: 100%
width: 350px
</style>
6 changes: 6 additions & 0 deletions src/components/TagList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ const notesStore = useNotesStore();
<q-card-section>
<div class="text-h6">{{ tag.name }}</div>
</q-card-section>

<q-separator />

<q-card-actions>
<q-btn color="info" flat :to="{ name: 'tag-detail', params: { id: tag.id } }">Details</q-btn>
</q-card-actions>
</q-card>
</div>
<div v-else>
Expand Down
6 changes: 6 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import NoteForm from "../components/NoteForm.vue";
import NoteDetail from "../components/NoteDetail.vue";
import TagForm from "../components/TagForm.vue";
import TagList from "../components/TagList.vue";
import TagDetail from "../components/TagDetail.vue";

const router = createRouter({
history: createWebHistory(import.meta.env.BASE_URL),
Expand Down Expand Up @@ -34,6 +35,11 @@ const router = createRouter({
name: 'add-tag',
component: TagForm,
},
{
path: '/tag-detail/:id',
name: 'tag-detail',
component: TagDetail,
},
{
path: "/about",
name: "about",
Expand Down
5 changes: 4 additions & 1 deletion src/stores/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ export const useNotesStore = defineStore('notes', {
getters: {
getNoteById: (state) => {
return (id) => state.notes.find((note) => note.id === id)
}
},
getTagById: (state) => {
return (id) => state.tags.find((tag) => tag.id === id)
},
},
actions: {
addNote(note) {
Expand Down

0 comments on commit 9c2eea9

Please sign in to comment.