Skip to content

Commit 4cb8521

Browse files
committed
feat(View): Implement post modal
1 parent 1b30a22 commit 4cb8521

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

src/composables/posts.composable.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ref } from "vue";
2+
import { useLoading } from "./loading.composable";
3+
import PostsServices from "@/services/posts.services";
4+
5+
export default function useFetchPost() {
6+
const { endLoading, isLoading, startLoading } = useLoading();
7+
8+
const post = ref(null);
9+
10+
function fetchPost(id) {
11+
startLoading()
12+
return PostsServices.getOneById(id).then((response) => {
13+
post.value = response.data
14+
return response
15+
}).finally(()=>{
16+
endLoading()
17+
});
18+
}
19+
20+
return {
21+
postIsLoading: isLoading,
22+
post,
23+
fetchPost
24+
};
25+
}

src/views/CommentsView.vue

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
</VColumn>
1515
<VColumn :header="$t('Email')" field="email" />
1616
<VColumn :header="$t('Post')" field="postId">
17-
<template #body>
18-
<button class="btn btn-sm btn-outline-info" title="View Related Post">
17+
<template #body="{ item }">
18+
<button class="btn btn-sm btn-outline-info" title="View Related Post"
19+
@click="handleFetchPost(item.postId)">
1920
<i class="bi-eye" />
2021
</button>
2122
</template>
@@ -52,23 +53,46 @@
5253
</template>
5354
</VColumn>
5455
</VTableServer>
56+
<VModal v-model="postModalIsOpen">
57+
<div v-if="postIsLoading">
58+
Loading...
59+
</div>
60+
<h1 v-if="!postIsLoading && post !== null">
61+
{{ post.title }}
62+
</h1>
63+
</VModal>
5564
</template>
5665

5766
<script>
5867
import CommentsFilterForm from '@/components/comments/CommentsFilterForm.vue';
5968
import VColumn from '@/components/data-table/VColumn.vue';
6069
import VTableServer from '@/components/data-table/VTableServer.vue';
70+
import VModal from '@/components/VModal.vue';
6171
import { useFetchComments } from '@/composables/comments.composable';
6272
import { useApplyFilters } from '@/composables/filter.composable';
63-
import { computed } from 'vue';
73+
import useFetchPost from '@/composables/posts.composable';
74+
import { computed, ref } from 'vue';
6475
6576
export default {
6677
name: 'CommentsView',
6778
setup() {
6879
const { comments, commentsIsLoading, fetchComments } = useFetchComments()
69-
7080
fetchComments({})
7181
82+
const { post, postIsLoading, fetchPost } = useFetchPost()
83+
const postModalIsOpen = ref(false)
84+
85+
const handleFetchPost = async (id) => {
86+
try {
87+
postModalIsOpen.value = true
88+
await fetchPost(id)
89+
} catch {
90+
postModalIsOpen.value = false
91+
}
92+
93+
94+
}
95+
7296
const filters = useApplyFilters({
7397
searchQuery: undefined,
7498
status: undefined,
@@ -91,24 +115,29 @@ export default {
91115
92116
const highlightText = (text) => {
93117
const query = filters?.searchQuery && isNaN(Number(filters.searchQuery)) ? filters.searchQuery.trim().toLowerCase() : undefined;
94-
118+
95119
if (!query) return text;
96120
const regex = new RegExp(`(${query})`, 'gi');
97121
return String(text).replace(regex, '<mark>$1</mark>');
98122
99123
}
100-
124+
101125
return {
102126
comments: filteredComments,
103127
commentsIsLoading,
104128
filters,
105-
highlightText
129+
highlightText,
130+
post,
131+
postIsLoading,
132+
handleFetchPost,
133+
postModalIsOpen
106134
}
107135
},
108136
components: {
109137
VTableServer,
110138
VColumn,
111-
CommentsFilterForm
139+
CommentsFilterForm,
140+
VModal
112141
}
113142
}
114143
</script>

0 commit comments

Comments
 (0)