umwd-components 0.1.829 → 0.1.831

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.
@@ -5,14 +5,43 @@
5
5
  * @license MIT
6
6
  */
7
7
 
8
- import { mutateData } from '../../../services/mutate-data.js';
8
+ import { z } from 'zod';
9
9
  import { flattenAttributes } from '../../../../lib/utils.js';
10
10
  import { parseFormData } from '../../../../lib/parseFormData.js';
11
11
 
12
+ const contactFormSchema = z.object({
13
+ email: z.string().email(),
14
+ name: z.string().min(2).max(100),
15
+ subject: z.string().min(2).max(100),
16
+ message: z.string().min(2).max(1000),
17
+ });
12
18
  async function contactFormAction(prevState, formData) {
13
19
  Object.fromEntries(formData);
14
20
  const parsedFormData = parseFormData(formData);
15
- const responseData = await mutateData("POST", `/api/leads/createFromContactform`, parsedFormData);
21
+ console.log("parsedFormData", parsedFormData);
22
+ const validatedFields = contactFormSchema.safeParse(parsedFormData);
23
+ if (!validatedFields.success) {
24
+ return {
25
+ ...prevState,
26
+ severity: "error",
27
+ strapiErrors: validatedFields.error.format(),
28
+ message: "Validation Error. Please check your input.",
29
+ };
30
+ }
31
+ console.log("validatedFields", validatedFields);
32
+ const { data } = validatedFields;
33
+ /**
34
+ * won't work with mutateData because the contactForm is publicly available and mutateData
35
+ * will fail when it cannot find the authentication token
36
+ **/
37
+ const responseData = await fetch(`${process.env.NEXT_PUBLIC_STRAPI_URL}/api/leads/createFromContactform`, {
38
+ method: "POST",
39
+ headers: {
40
+ "Content-Type": "application/json",
41
+ },
42
+ body: JSON.stringify(data),
43
+ }).then((res) => res.json());
44
+ console.log("responseData", responseData);
16
45
  if (!responseData) {
17
46
  return {
18
47
  ...prevState,