Skip to content

Commit 495a0eb

Browse files
committed
feat(Services):Implement comments services & its composable
1 parent 0d165c6 commit 495a0eb

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { computed, ref } from "vue";
2+
import { useLoading } from "./loading.composable";
3+
import { keyBy } from "@/utils";
4+
import CommentsService from "@/services/comments.service";
5+
6+
export const useFetchComments = () => {
7+
const { isLoading, startLoading, endLoading } = useLoading();
8+
const comments = ref([]);
9+
const commentsKeyById = computed(() => keyBy(comments.value, "id"));
10+
11+
/**
12+
* @param {AxiosRequestConfig} [config]
13+
*/
14+
const fetchComments = (config) => {
15+
startLoading();
16+
return CommentsService.getAll(config)
17+
.then((response) => {
18+
comments.value = response.data;
19+
return response;
20+
})
21+
.finally(() => {
22+
endLoading();
23+
});
24+
};
25+
26+
return {
27+
comments,
28+
commentsIsLoading: isLoading,
29+
commentsKeyById,
30+
fetchComments,
31+
};
32+
};

src/services/comments.service.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import CrudService from "./crud.service";
2+
3+
class CommentsService extends CrudService {
4+
/**
5+
* Service url
6+
*
7+
* @returns {String}
8+
*/
9+
static get URL() {
10+
return "comments";
11+
}
12+
13+
/**
14+
* Get default item
15+
*
16+
* @returns {Object}
17+
*/
18+
static getDefault() {
19+
return {
20+
postId: undefined,
21+
id: undefined,
22+
name: undefined,
23+
body: undefined,
24+
email: undefined,
25+
};
26+
}
27+
}
28+
29+
export default CommentsService;

0 commit comments

Comments
 (0)