aboutsummaryrefslogtreecommitdiff
path: root/front-end/src/views/Blog/form-helpers/channels.ts
blob: cd33a20f34d639fc132b15570382087c73381bcf (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// 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 { MaybeRef, computed, watch, Ref } from 'vue'
import { helpers, required, maxLength, numeric } from "@vuelidate/validators"
import { useVuelidateWrapper } from '@vnuge/vnlib.browser';
import { BlogChannel, ChannelFeed } from '@vnuge/cmnext-admin';
import useVuelidate from "@vuelidate/core"

export const getChannelForm = (editMode?: Ref<boolean>) => {
    const channelSchema = computed(() => {
        return {
            fields: [
                {
                    id: 'channel-name',
                    type: 'text',
                    label: 'Channel Name',
                    name: 'name',
                    placeholder: 'Enter the name of the channel',
                    description: 'A simple human readable name for the channel'
                },
                {
                    id: 'channel-path',
                    type: 'text',
                    label: 'Root Path',
                    name: 'path',
                    placeholder: 'Enter the root path to the channel',
                    description: editMode?.value ? 'You may not edit the channel directory' : 'The path in your bucket to the working directory for the channel',
                    disabled: editMode?.value
                },
                {
                    id: 'channel-index',
                    type: 'text',
                    label: 'Index File',
                    name: 'index',
                    placeholder: 'Enter the index file for the channel',
                    description: editMode?.value ?
                        'You may not edit the index file path'
                        : 'The name or path of the post index file, stored under the root directory of the channel',
                    disabled: editMode?.value
                },
                {
                    id: 'channel-content-dir',
                    type: 'text',
                    label: 'Content Directory',
                    name: 'content',
                    placeholder: 'Enter the content directory for the channel',
                    description: editMode?.value ?
                        'You may not edit the content directory path'
                        : 'The name or path of the content directory, stored under the root directory of the channel',
                    disabled: editMode?.value
                },
                {
                    id: 'index-file-example',
                    type: 'text',
                    label: 'Index Path',
                    name: 'example',
                    placeholder: 'Your index file path',
                    description: 'This is the location within your bucket where the index file will be stored',
                    disabled: true,
                }
            ]
        }
    });

    const feedSchema = {
        fields: [
            {
                id: 'channel-feed-url',
                type: 'text',
                label: 'Publish Url',
                name: 'url',
                placeholder: 'Enter the feed url for the channel',
                description: 'The rss syndication url for your blog channel, the http url your blog resides at.'
            },
            {
                id: 'channel-feed-path',
                type: 'text',
                label: 'Feed File',
                name: 'path',
                placeholder: 'feed.xml',
                description: 'The path to the feed xml file within the channel directory'
            },
            {
                id: 'channel-feed-image',
                type: 'text',
                label: 'Image Url',
                name: 'image',
                placeholder: 'Enter the url for the default feed image',
                description: 'The full http url to the default feed image'
            },
            {
                id: 'channel-feed-author',
                type: 'text',
                label: 'Feed Author',
                name: 'author',
                placeholder: 'Your name',
                description: 'The author name for the feed'
            },
            {
                id: 'channel-feed-contact',
                type: 'text',
                label: 'Feed Contact',
                name: 'contact',
                placeholder: 'Your contact email address',
                description: 'The webmaster contact email address'
            },
            {
                id: 'channel-feed-max-items',
                type: 'number',
                label: 'Feed Max Items',
                name: 'maxItems',
                placeholder: 'Enter the feed max items for the channel',
                description: 'The maximum number of posts to publish in the feed'
            },
            {
                id: 'channel-feed-description',
                type: 'textarea',
                label: 'Feed Description',
                name: 'description',
                placeholder: 'Enter the feed description for the channel',
            }
        ]
    }

    const alphaNumSpace = helpers.regex(/^[a-zA-Z0-9 ]*$/);
    const httpUrl = helpers.regex(/^(http|https):\/\/[^ "]+$/);

    const channelRules = {
        name: {
            required: helpers.withMessage('Channel name is required', required),
            maxlength: helpers.withMessage('Channel name must be less than 50 characters', maxLength(50)),
            alphaNumSpace: helpers.withMessage('Channel name must be alphanumeric', alphaNumSpace),
        },
        path: {
            required: helpers.withMessage('Channel path is required', required),
            maxlength: helpers.withMessage('Channel path must be less than 50 characters', maxLength(50)),
        },
        index: {
            required: helpers.withMessage('Channel index is required', required),
            maxlength: helpers.withMessage('Channel index must be less than 50 characters', maxLength(50)),
        },
        content: {
            required: helpers.withMessage('Channel content directory is required', required),
            maxlength: helpers.withMessage('Channel content directory must be less than 50 characters', maxLength(50)),
        },
        example: {}
    }

    const feedRules = {
        url: {
            required: helpers.withMessage('Channel feed url is required', required),
            maxlength: helpers.withMessage('Channel feed url must be less than 100 characters', maxLength(100)),
            url: helpers.withMessage('Channel feed url must be a valid url', httpUrl),
        },
        path: {
            required: helpers.withMessage('Channel feed path is required', required),
            maxlength: helpers.withMessage('Channel feed path must be less than 50 characters', maxLength(50)),
        },
        image: {
            maxlength: helpers.withMessage('Channel feed image must be less than 200 characters', maxLength(200)),
        },
        contact: {
            maxlength: helpers.withMessage('Channel feed contact must be less than 50 characters', maxLength(50)),
        },
        description: {
            alphaNumSpace: helpers.withMessage('Channel feed description must be alphanumeric', alphaNumSpace),
            maxlength: helpers.withMessage('Channel feed description must be less than 50 characters', maxLength(200)),
        },
        maxItems: {
            numeric: helpers.withMessage('Channel feed max items must be a number', numeric),
        },
        author: {
            alphaNumSpace: helpers.withMessage('Channel feed author must be alphanumeric', alphaNumSpace),
            maxlength: helpers.withMessage('Channel feed author must be less than 50 characters', maxLength(50)),
        }
    }

    const getChannelValidator = <T extends BlogChannel>(buffer: MaybeRef<T | undefined>) => {

        const v$ = useVuelidate(channelRules, buffer, { $lazy: true, $autoDirty: true });

        const updateExample = () => {
            if (!v$.value.path.$model || !v$.value.index.$model) {
                v$.value.example.$model = '';
                return;
            }
            //Update the example path
            v$.value.example.$model = `${v$.value.path.$model}/${v$.value.index.$model}`;
        }

        watch(v$, updateExample);

        updateExample();

        const { validate } = useVuelidateWrapper(v$);
        return { v$, validate, reset: v$.value.$reset };
    }

    const getFeedValidator = <T extends ChannelFeed>(buffer: MaybeRef<T | undefined>) => {
        const v$ = useVuelidate(feedRules, buffer, { $lazy: true, $autoDirty: true });
        const { validate } = useVuelidateWrapper(v$);
        return { v$, validate, reset: v$.value.$reset };
    }

    return {
        channelSchema,
        feedSchema,
        channelRules,
        feedRules,
        getChannelValidator,
        getFeedValidator
    };
}