mktcms 0.1.32 → 0.1.34

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "mktcms",
3
3
  "configKey": "mktcms",
4
- "version": "0.1.32",
4
+ "version": "0.1.34",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "1.0.2",
7
7
  "unbuild": "3.6.1"
package/dist/module.mjs CHANGED
@@ -51,6 +51,11 @@ const module$1 = defineNuxtModule({
51
51
  name: "useTxtContents",
52
52
  as: "useTxtContents",
53
53
  from: resolver.resolve("runtime/app/composables/useTxtContents")
54
+ },
55
+ {
56
+ name: "useForm",
57
+ as: "useForm",
58
+ from: resolver.resolve("runtime/app/composables/useForm")
54
59
  }
55
60
  ]);
56
61
  addServerImports({
@@ -0,0 +1,11 @@
1
+ import { z } from 'zod';
2
+ export declare function useForm(endpoint: string, successMessageText: string, errorMessageText: string, validationSchema: z.ZodObject<any>): {
3
+ isSending: import("vue").Ref<boolean, boolean>;
4
+ successMessage: import("vue").Ref<string, string>;
5
+ errorMessage: import("vue").Ref<string, string>;
6
+ validationErrors: import("vue").Ref<Record<string, string[] | undefined>, Record<string, string[] | undefined>>;
7
+ fieldsTouched: import("vue").Ref<Record<string, boolean>, Record<string, boolean>>;
8
+ validationErrorsFor: (field: string) => string[];
9
+ validate: (body: Record<string, any>) => boolean;
10
+ send: (body: Record<string, any>) => Promise<void>;
11
+ };
@@ -0,0 +1,53 @@
1
+ import { z } from "zod";
2
+ import { ref } from "vue";
3
+ export function useForm(endpoint, successMessageText, errorMessageText, validationSchema) {
4
+ const isSending = ref(false);
5
+ const successMessage = ref("");
6
+ const errorMessage = ref("");
7
+ const validationErrors = ref({});
8
+ const fieldsTouched = ref({});
9
+ function validate(body) {
10
+ const parseResult = validationSchema.safeParse(body);
11
+ if (parseResult.success) {
12
+ validationErrors.value = {};
13
+ return true;
14
+ } else {
15
+ validationErrors.value = z.flattenError(parseResult.error).fieldErrors;
16
+ return false;
17
+ }
18
+ }
19
+ function validationErrorsFor(field) {
20
+ if (!fieldsTouched.value[field]) return [];
21
+ return validationErrors.value[field] || [];
22
+ }
23
+ async function send(body) {
24
+ if (isSending.value) return;
25
+ successMessage.value = "";
26
+ errorMessage.value = "";
27
+ validate(body);
28
+ if (Object.keys(validationErrors.value).length > 0) {
29
+ return;
30
+ }
31
+ isSending.value = true;
32
+ try {
33
+ await $fetch(endpoint, {
34
+ method: "POST",
35
+ body
36
+ });
37
+ successMessage.value = successMessageText;
38
+ } catch {
39
+ errorMessage.value = errorMessageText;
40
+ }
41
+ isSending.value = false;
42
+ }
43
+ return {
44
+ isSending,
45
+ successMessage,
46
+ errorMessage,
47
+ validationErrors,
48
+ fieldsTouched,
49
+ validationErrorsFor,
50
+ validate,
51
+ send
52
+ };
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mktcms",
3
- "version": "0.1.32",
3
+ "version": "0.1.34",
4
4
  "description": "Simple CMS module for Nuxt",
5
5
  "repository": "mktcode/mktcms",
6
6
  "license": "MIT",