aboutsummaryrefslogtreecommitdiff
path: root/front-end/src/views/Blog/components/Posts.vue
blob: 647e093bc146e8446520e0a773ffe2330111e368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
    <div id="post-editor" class="">
        <EditorTable title="Manage posts" :show-edit="showEdit" :pagination="pagination" @open-new="openNew">
            <template #table>
                <PostTable 
                    :items="items"
                    @open-edit="openEdit"
                    @delete="onDelete"
                />
            </template> 
            <template #editor>
                <PostEditor 
                    @submit="onSubmit"
                    @close="closeEdit(true)"
                    @delete="onDelete"
                />
            </template>
        </EditorTable>
    </div>
</template>

<script setup lang="ts">
import { computed, defineAsyncComponent } from 'vue';
import { isEmpty } from 'lodash-es';
import { PostMeta } from '@vnuge/cmnext-admin';
import { apiCall, debugLog, useConfirm } from '@vnuge/vnlib.browser';
import { useStore } from '../../../store';
import EditorTable from './EditorTable.vue';
import PostTable from './Posts/PostTable.vue';
const PostEditor = defineAsyncComponent(() => import('./Posts/PostEdit.vue'))

const store = useStore()

const { reveal } = useConfirm()

const showEdit = computed(() => !isEmpty(store.posts.selectedId));
const { items, pagination } = store.posts.createPages();

//Open with the post id
const openEdit = async (post: PostMeta) => store.posts.selectedId = post.id;

const refresh = () => {
    store.posts.refresh();
    store.content.refresh();
}

const closeEdit = (update?: boolean) => {
    store.posts.selectedId = ''
    //reload channels
    if (update) {
        //must refresh content and posts when a post is updated
       refresh();
    }
    //Reset page to top
    window.scrollTo(0, 0);
}

const openNew = () => {
    //Reset the edit post
    store.posts.selectedId = 'new'
    //Reset page to top
    window.scrollTo(0, 0)
}

const onSubmit = async ({post, content } : { post: PostMeta, content: string }) => {

    debugLog('submitting', post, content);

    //Check for new channel, or updating old channel
    if (store.posts.selectedId === 'new') {
        //Exec create call
        await apiCall(async ({toaster}) => {

            //endpoint returns the content
            const newMeta = await store.posts.add(post);

            //Publish the content
            await store.content.updatePostContent(newMeta, content)

            toaster.general.success({
                id: 'post-create-success',
                title: 'Created',
                text: `Post '${post.title}' created`,
            })
        })
    }
    else if (!isEmpty(store.posts.selectedId)) {
        //Exec update call
        await apiCall(async ( {toaster} ) => {
            await store.posts.update(post);
           
            //Publish the content
            await store.content.updatePostContent(post, content)

            toaster.general.info({
                id: 'post-update-success',
                title: 'Saved',
                text: `Post '${post.title}' updated`,
            })

            refresh();
        })
    }
}

const onDelete = async (post: PostMeta) => {

    //Show confirm
    const { isCanceled } = await reveal({
        title: 'Delete Post?',
        text: `Are you sure you want to delete post '${post.title}?' This action cannot be undone.`,
    })
    if (isCanceled) {
        return;
    }

    if (!confirm(`Are you sure you want to delete post '${post.id}' forever?`)) {
        return;
    }

    //Exec delete call
    await apiCall(async () => {
        await store.posts.delete(post);
        //Close the edit panel
        closeEdit(true);
    })

    store.posts.refresh();
}

</script>

<style lang="scss">

</style>