diff options
Diffstat (limited to 'lib/admin/src/posts')
-rw-r--r-- | lib/admin/src/posts/computedPosts.ts | 56 | ||||
-rw-r--r-- | lib/admin/src/posts/index.ts | 3 | ||||
-rw-r--r-- | lib/admin/src/posts/usePost.ts | 68 |
3 files changed, 38 insertions, 89 deletions
diff --git a/lib/admin/src/posts/computedPosts.ts b/lib/admin/src/posts/computedPosts.ts deleted file mode 100644 index 640226f..0000000 --- a/lib/admin/src/posts/computedPosts.ts +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (C) 2023 Vaughn Nugent -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see <https://www.gnu.org/licenses/>. - -import { computed, ref } from "vue"; -import { isEqual, find } from 'lodash-es'; -import { apiCall } from "@vnuge/vnlib.browser"; -import { PostMeta, ComputedPosts, BlogAdminContext } from "../types"; -import { usePostApi } from "./usePost"; -import { watchAndCompute } from "../helpers"; - -/** - * Creates a computed post api for reactive blog apis - * @param context The blog admin context - * @returns The computed post api - */ -export const useComputedPosts = (context: BlogAdminContext): ComputedPosts => { - //Post api around the post url and channel - const postApi = usePostApi(context); - const trigger = ref(0); - - const { channel, post } = context.getQuery(); - - //Get all posts - const items = watchAndCompute([channel, post, trigger], async () => { - return channel.value ? await apiCall(postApi.getPosts) ?? [] : []; - }, []) - - const selectedItem = computed<PostMeta | undefined>(() => { - return find(items.value, p => isEqual(p.id, post.value)); - }) - - const refresh = () => { - trigger.value++; - } - - return { - ...postApi, - items, - selectedItem, - selectedId:post, - getQuery: context.getQuery, - refresh - }; -}
\ No newline at end of file diff --git a/lib/admin/src/posts/index.ts b/lib/admin/src/posts/index.ts index 0105265..9bb70d4 100644 --- a/lib/admin/src/posts/index.ts +++ b/lib/admin/src/posts/index.ts @@ -13,5 +13,4 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. -export * from './usePost' -export * from './computedPosts'
\ No newline at end of file +export * from './usePost'
\ No newline at end of file diff --git a/lib/admin/src/posts/usePost.ts b/lib/admin/src/posts/usePost.ts index 1d93b6b..421ff83 100644 --- a/lib/admin/src/posts/usePost.ts +++ b/lib/admin/src/posts/usePost.ts @@ -14,28 +14,23 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. import { isArray, orderBy } from 'lodash-es'; -import { WebMessage } from "@vnuge/vnlib.browser" -import { PostMeta, PostApi, BlogAdminContext } from "../types"; +import { get } from '@vueuse/core'; +import { type WebMessage } from "@vnuge/vnlib.browser" +import { type MaybeRef } from 'vue'; +import type { PostMeta, PostApi, BlogAdminContext } from "../types"; /** * Gets a reactive post api for the given channel * @param context The blog admin context * @returns The configured post api */ -export const usePostApi = (context : BlogAdminContext): PostApi => { +export const usePosts = (context: BlogAdminContext, channel: MaybeRef<string>): PostApi => { const axios = context.getAxios(); - const { channel } = context.getQuery(); - const getUrl = (): string => { const url = context.getPostUrl(); //Return the url with the channel id query - return `${url}?channel=${channel.value}`; - } - - const getPosts = async (): Promise<PostMeta[]> => { - const { data } = await axios.get(getUrl()); - return isArray(data) ? orderBy(data, 'date', 'desc') : []; + return `${url}?channel=${get(channel)}`; } const deletePost = (post: PostMeta): Promise<void> => { @@ -43,28 +38,39 @@ export const usePostApi = (context : BlogAdminContext): PostApi => { return axios.delete(`${getUrl()}&post=${post.id}`); } - const publishPost = async (post: PostMeta): Promise<PostMeta> => { - //Call post with the post data - const { data } = await axios.post<WebMessage<PostMeta>>(getUrl(), post); - return data.getResultOrThrow(); - } + return { + + async delete(item: PostMeta | PostMeta[]){ + //invoke delete for each item + if(isArray(item)){ + await Promise.all(item.map(deletePost)); + } + else{ + //Call delete with the post id query + await deletePost(item) + } + }, - const updatePost = async (post: PostMeta): Promise<PostMeta> => { - //Call patch with the updated post content, must have an id set as an existing post - const { data } = await axios.patch<WebMessage<PostMeta>>(getUrl(), post); - return data.getResultOrThrow(); - } + async add(item: PostMeta) { + //Call post with the post data + const { data } = await axios.post<WebMessage<PostMeta>>(getUrl(), item); + return data.getResultOrThrow(); + }, - const getSinglePost = async (postId: string): Promise<PostMeta> => { - const { data } = await axios.get(`${getUrl()}&post=${postId}`); - return data; - } + async getAllItems(){ + const { data } = await axios.get(getUrl()); + return isArray(data) ? orderBy(data, 'date', 'desc') : []; + }, - return { - getPosts, - deletePost, - publishPost, - updatePost, - getSinglePost + async update(item: PostMeta) { + //Call patch with the updated post content, must have an id set as an existing post + const { data } = await axios.patch<WebMessage<PostMeta>>(getUrl(), item); + return data.getResultOrThrow(); + }, + + async getSinglePost(postId: string) { + const { data } = await axios.get(`${getUrl()}&post=${postId}`); + return data; + } }; }
\ No newline at end of file |