umwd-components 0.1.661 → 0.1.663

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.
@@ -1,9 +1,11 @@
1
1
  import React from "react";
2
2
  import { VendorName } from "../../../types/logistics/Vendor";
3
3
  import { SxProps, Theme } from "@mui/material/styles";
4
- export declare function AddIPOForm({ vendorNamesArr, sx, revalidateCallback, handleClose, }: {
4
+ interface AddIPOFormProps {
5
5
  vendorNamesArr: VendorName[];
6
6
  sx?: SxProps<Theme>;
7
7
  revalidateCallback?: () => void;
8
8
  handleClose?: () => void;
9
- }): React.JSX.Element;
9
+ }
10
+ export declare function AddIPOForm({ vendorNamesArr, sx, revalidateCallback, handleClose, }: AddIPOFormProps): React.JSX.Element;
11
+ export {};
@@ -1,6 +1,11 @@
1
1
  import React from "react";
2
2
  import { ProductName } from "../../../types/e-commerce/product/types";
3
- export declare function AddVendorForm({ productNamesArr, revalidateCallback, }: {
3
+ import { SxProps } from "@mui/system";
4
+ interface AddVendorFormProps {
4
5
  productNamesArr: ProductName[];
5
6
  revalidateCallback?: () => void;
6
- }): React.JSX.Element;
7
+ handleClose?: () => void;
8
+ sx?: SxProps;
9
+ }
10
+ export declare function AddVendorForm({ productNamesArr, revalidateCallback, handleClose, sx, }: AddVendorFormProps): React.JSX.Element;
11
+ export {};
@@ -2,9 +2,12 @@ import React from "react";
2
2
  import { Vendor } from "../../../types/logistics/Vendor";
3
3
  import { ProductName } from "../../../types/e-commerce/product/types";
4
4
  import { SxProps } from "@mui/material/styles";
5
- export declare function EditVendorForm({ data, productNamesArr, revalidateCallback, sx, }: {
5
+ interface EditVendorFormProps {
6
6
  data: Vendor;
7
7
  productNamesArr: ProductName[];
8
8
  revalidateCallback?: () => void;
9
+ handleClose?: () => void;
9
10
  sx?: SxProps;
10
- }): React.JSX.Element;
11
+ }
12
+ export declare function EditVendorForm({ data, productNamesArr, revalidateCallback, handleClose, sx, }: EditVendorFormProps): React.JSX.Element;
13
+ export {};
@@ -1 +1,9 @@
1
- export declare function updateVendorAction(prevState: any, formData: FormData): Promise<any>;
1
+ interface UpdateVendorState {
2
+ data: any | null;
3
+ message: string | null;
4
+ severity: "success" | "error" | "info" | "warning" | null;
5
+ strapiErrors: any | null;
6
+ zodErrors: any | null;
7
+ }
8
+ export declare function updateVendorAction(prevState: any, formData: FormData): Promise<UpdateVendorState>;
9
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "umwd-components",
3
- "version": "0.1.661",
3
+ "version": "0.1.663",
4
4
  "description": "UMWD Component library",
5
5
  "main": "dist/src/index.js",
6
6
  "module": "dist/src/index.js",
@@ -1,10 +1,9 @@
1
1
  "use client";
2
2
 
3
3
  import React, { useEffect, useState } from "react";
4
- import Paper from "@mui/material/Paper";
5
4
  import Box from "@mui/material/Box";
6
5
  import Stack from "@mui/material/Stack";
7
- import Alert from "@mui/material/Alert";
6
+ import Paper from "@mui/material/Paper";
8
7
  import TextField from "@mui/material/TextField";
9
8
  import Typography from "@mui/material/Typography";
10
9
  import Divider from "@mui/material/Divider";
@@ -12,7 +11,6 @@ import Button from "@mui/material/Button";
12
11
 
13
12
  import { SubmitButton } from "../../SubmitButton";
14
13
  import { useFormState } from "react-dom";
15
- import { StrapiErrors } from "../../StrapiErrors";
16
14
  import { createIPOAction } from "../../../data/actions/logistics/ipo/createIPOAction";
17
15
  import Grid from "@mui/material/Grid";
18
16
 
@@ -40,17 +38,19 @@ const INITIAL_STATE = {
40
38
  severity: null,
41
39
  };
42
40
 
41
+ interface AddIPOFormProps {
42
+ vendorNamesArr: VendorName[];
43
+ sx?: SxProps<Theme>;
44
+ revalidateCallback?: () => void;
45
+ handleClose?: () => void;
46
+ }
47
+
43
48
  export function AddIPOForm({
44
49
  vendorNamesArr,
45
50
  sx,
46
51
  revalidateCallback,
47
52
  handleClose,
48
- }: {
49
- vendorNamesArr: VendorName[];
50
- sx?: SxProps<Theme>;
51
- revalidateCallback?: () => void;
52
- handleClose?: () => void;
53
- }) {
53
+ }: AddIPOFormProps) {
54
54
  const [formState, formAction] = useFormState(createIPOAction, INITIAL_STATE);
55
55
  const [productsArr, setProductsArr] = useState<VendorProductName[]>([]);
56
56
  const [vendorID, setVendorID] = useState<number | null>(null);
@@ -88,15 +88,12 @@ export function AddIPOForm({
88
88
  const vendor = await getSingleVendor(selectedVendorID);
89
89
  setProductsArr(vendor?.products?.data);
90
90
  };
91
-
92
91
  return (
93
92
  <Box
94
93
  sx={[
95
- { p: 2 },
96
94
  // You cannot spread `sx` directly because `SxProps` (typeof sx) can be an array.
97
95
  ...(Array.isArray(sx) ? sx : [sx]),
98
96
  ]}
99
- component={Paper}
100
97
  >
101
98
  <form action={formAction}>
102
99
  <Grid container spacing={2}>
@@ -682,11 +682,23 @@ export default function TextualManageIPOForm({
682
682
  </Paper>
683
683
  );
684
684
  })}
685
- <Paper sx={{ p: 2 }}>
686
- <Stack direction="row" spacing={2} justifyContent={"end"}>
687
- <SubmitButton text="Update items" loadingText="Loading..." />
688
- </Stack>
689
- </Paper>
685
+
686
+ <Stack
687
+ direction="row"
688
+ spacing={2}
689
+ justifyContent={"space-between"}
690
+ >
691
+ {handleClose && (
692
+ <Button onClick={handleClose} variant="outlined">
693
+ Cancel
694
+ </Button>
695
+ )}
696
+ <SubmitButton
697
+ variant="contained"
698
+ text="Update items"
699
+ loadingText="Loading..."
700
+ />
701
+ </Stack>
690
702
  <input
691
703
  type="hidden"
692
704
  name="items"
@@ -1,15 +1,14 @@
1
1
  "use client";
2
2
 
3
- import React, { useState, useEffect, use } from "react";
4
- import Paper from "@mui/material/Paper";
3
+ import React, { useState, useEffect } from "react";
5
4
  import Box from "@mui/material/Box";
6
5
  import Stack from "@mui/material/Stack";
7
- import Alert from "@mui/material/Alert";
6
+ import Button from "@mui/material/Button";
8
7
  import TextField from "@mui/material/TextField";
9
8
  import Typography from "@mui/material/Typography";
10
9
  import { SubmitButton } from "../../SubmitButton";
11
10
  import { useFormState } from "react-dom";
12
- import { StrapiErrors } from "../../StrapiErrors";
11
+ import { useSnackbar } from "../../../context/common/SnackbarContext";
13
12
  import { createVendorAction } from "../../../data/actions/logistics/vendor/createVendorAction";
14
13
  import Grid from "@mui/material/Grid";
15
14
  import { BusinessCredentialsFields } from "../../../components/e-commerce/customer/BusinessCredentials";
@@ -17,8 +16,8 @@ import { AddressFields } from "../../../components/common/Address";
17
16
  import { ProductName } from "../../../types/e-commerce/product/types";
18
17
  import ProductSelector from "../../../components/e-commerce/products/ProductSelector";
19
18
  import { ContactsFields } from "../../../components/common/Contacts";
20
- import Button from "@mui/material/Button";
21
19
  import AddIcon from "@mui/icons-material/Add";
20
+ import { SxProps } from "@mui/system";
22
21
  import {
23
22
  ContactsFieldsProps,
24
23
  ContactType,
@@ -30,48 +29,70 @@ const INITIAL_STATE = {
30
29
  strapiErrors: null,
31
30
  data: null,
32
31
  message: null,
32
+ severity: null,
33
33
  };
34
34
 
35
+ interface AddVendorFormProps {
36
+ productNamesArr: ProductName[];
37
+ revalidateCallback?: () => void;
38
+ handleClose?: () => void;
39
+ sx?: SxProps;
40
+ }
41
+
35
42
  export function AddVendorForm({
36
43
  productNamesArr,
37
44
  revalidateCallback,
38
- }: {
39
- productNamesArr: ProductName[];
40
- revalidateCallback?: () => void;
41
- }) {
45
+ handleClose,
46
+ sx,
47
+ }: AddVendorFormProps) {
42
48
  const [formState, formAction] = useFormState(
43
49
  createVendorAction,
44
50
  INITIAL_STATE
45
51
  );
46
-
52
+ const { handleAddMessage } = useSnackbar();
47
53
  const [newContacts, setNewContacts] = useState<ContactsFieldsProps[]>([]);
48
54
 
49
55
  const handleDeleteContact = (index: number) => {
50
- console.log("delete", index);
51
56
  const newContactsArr = newContacts.filter((_, i) => i !== index);
52
-
53
57
  setNewContacts(newContactsArr);
54
58
  };
55
59
 
56
60
  useEffect(() => {
57
- if (formState?.data && revalidateCallback) {
58
- revalidateCallback();
61
+ if (formState?.message) {
62
+ handleAddMessage({
63
+ message: formState.message,
64
+ severity: formState.severity || "error",
65
+ });
66
+
67
+ if (formState.severity === "success") {
68
+ revalidateCallback && revalidateCallback();
69
+ handleClose && handleClose();
70
+ }
59
71
  }
60
- }, [formState.data]);
72
+ }, [formState?.message]);
73
+
74
+ useEffect(() => {
75
+ if (formState?.strapiErrors) {
76
+ handleAddMessage({
77
+ message: formState.strapiErrors.message || "Error creating vendor",
78
+ severity: "error",
79
+ });
80
+ }
81
+ }, [formState?.strapiErrors]);
61
82
 
62
83
  return (
63
- <Box
64
- /* sx={[
65
- // You cannot spread `sx` directly because `SxProps` (typeof sx) can be an array.
66
- ...(Array.isArray(sx) ? sx : [sx]),
67
- ]} */
68
- component={Paper}
69
- sx={{ p: 2 }}
70
- >
84
+ <Box sx={[...(Array.isArray(sx) ? sx : [sx])]}>
71
85
  <form action={formAction}>
72
86
  <Grid container spacing={2}>
73
87
  <Grid item xs={12}>
74
- <Typography variant="h5">Add Vendor</Typography>
88
+ <Stack spacing={2}>
89
+ <Typography variant="h3" component="h1">
90
+ Add Vendor
91
+ </Typography>
92
+ <Typography variant="body1">
93
+ Create a new vendor for your business
94
+ </Typography>
95
+ </Stack>
75
96
  </Grid>
76
97
  <Grid item xs={12}>
77
98
  <ExplanatoryFoldOut
@@ -176,24 +197,24 @@ export function AddVendorForm({
176
197
  </Button>
177
198
  </Grid>
178
199
  </Grid>
179
- </Grid>
200
+ </Grid>{" "}
180
201
  <Grid item xs={12}>
181
202
  <Stack
182
- direction="row-reverse"
203
+ direction="row"
183
204
  spacing={2}
184
- alignItems={"center"}
185
- sx={{
186
- py: 1,
187
- }}
205
+ justifyContent="flex-end"
206
+ sx={{ mt: 2 }}
188
207
  >
189
- <SubmitButton text="create new vendor" loadingText="loading" />
190
- {formState?.strapiErrors && (
191
- <StrapiErrors error={formState?.strapiErrors} />
192
- )}
193
-
194
- {formState?.message && (
195
- <Alert severity="error">{formState?.message}</Alert>
208
+ {handleClose && (
209
+ <Button onClick={handleClose} variant="outlined">
210
+ Cancel
211
+ </Button>
196
212
  )}
213
+ <SubmitButton
214
+ text="Create Vendor"
215
+ loadingText="Creating..."
216
+ variant="contained"
217
+ />
197
218
  </Stack>
198
219
  </Grid>
199
220
  </Grid>
@@ -1,15 +1,15 @@
1
1
  "use client";
2
2
 
3
- import React, { useState } from "react";
3
+ import React, { useState, useEffect } from "react";
4
4
  import Paper from "@mui/material/Paper";
5
5
  import Box from "@mui/material/Box";
6
6
  import Stack from "@mui/material/Stack";
7
- import Alert from "@mui/material/Alert";
7
+ import Button from "@mui/material/Button";
8
8
  import TextField from "@mui/material/TextField";
9
9
  import Typography from "@mui/material/Typography";
10
10
  import { SubmitButton } from "../../SubmitButton";
11
11
  import { useFormState } from "react-dom";
12
- import { StrapiErrors } from "../../StrapiErrors";
12
+ import { useSnackbar } from "../../../context/common/SnackbarContext";
13
13
  import { updateVendorAction } from "../../../data/actions/logistics/vendor/updateVendorAction";
14
14
  import Grid from "@mui/material/Grid";
15
15
  import { Vendor } from "../../../types/logistics/Vendor";
@@ -34,19 +34,24 @@ const INITIAL_STATE = {
34
34
  strapiErrors: null,
35
35
  data: null,
36
36
  message: null,
37
+ severity: null as "success" | "error" | "info" | "warning" | null,
37
38
  };
38
39
 
40
+ interface EditVendorFormProps {
41
+ data: Vendor;
42
+ productNamesArr: ProductName[];
43
+ revalidateCallback?: () => void;
44
+ handleClose?: () => void;
45
+ sx?: SxProps;
46
+ }
47
+
39
48
  export function EditVendorForm({
40
49
  data,
41
50
  productNamesArr,
42
51
  revalidateCallback,
52
+ handleClose,
43
53
  sx,
44
- }: {
45
- data: Vendor;
46
- productNamesArr: ProductName[];
47
- revalidateCallback?: () => void;
48
- sx?: SxProps;
49
- }) {
54
+ }: EditVendorFormProps) {
50
55
  const {
51
56
  id,
52
57
  website_url,
@@ -64,6 +69,30 @@ export function EditVendorForm({
64
69
  updateVendorAction,
65
70
  INITIAL_STATE
66
71
  );
72
+ const { handleAddMessage } = useSnackbar();
73
+
74
+ useEffect(() => {
75
+ if (formState?.message) {
76
+ handleAddMessage({
77
+ message: formState.message,
78
+ severity: formState.severity || "error",
79
+ });
80
+
81
+ if (formState.severity === "success") {
82
+ revalidateCallback && revalidateCallback();
83
+ handleClose && handleClose();
84
+ }
85
+ }
86
+ }, [formState?.message]);
87
+
88
+ useEffect(() => {
89
+ if (formState?.strapiErrors) {
90
+ handleAddMessage({
91
+ message: formState.strapiErrors.message || "Error updating vendor",
92
+ severity: "error",
93
+ });
94
+ }
95
+ }, [formState?.strapiErrors]);
67
96
 
68
97
  const initialProducts = products?.data || [];
69
98
 
@@ -93,14 +122,7 @@ export function EditVendorForm({
93
122
  const [newContacts, setNewContacts] = useState<ContactsFieldsProps[]>([]);
94
123
 
95
124
  return (
96
- <Box
97
- sx={[
98
- // You cannot spread `sx` directly because `SxProps` (typeof sx) can be an array.
99
- { p: 2 },
100
- ...(Array.isArray(sx) ? sx : [sx]),
101
- ]}
102
- component={Paper}
103
- >
125
+ <Box sx={[...(Array.isArray(sx) ? sx : [sx])]}>
104
126
  <form action={formAction}>
105
127
  {id && <input id="id" type="hidden" name="id" value={id} />}
106
128
  <Grid container spacing={2}>
@@ -172,11 +194,9 @@ export function EditVendorForm({
172
194
  currentValue={initialProducts.map((prod) => prod.id)}
173
195
  />
174
196
  </Grid>
175
-
176
197
  <Grid item xs={12} sx={{ mt: 1 }}>
177
198
  <Typography variant="h6">Contacts</Typography>
178
199
  </Grid>
179
-
180
200
  {oldContacts?.map((contact, index) => {
181
201
  if (contact.data?.id) {
182
202
  if (open.includes(contact.data?.id)) {
@@ -219,7 +239,6 @@ export function EditVendorForm({
219
239
  }
220
240
  return null;
221
241
  })}
222
-
223
242
  {newContacts.map((contact, index) => {
224
243
  return (
225
244
  <Grid item xs={12} md={6} lg={4} key={contact.data?.uuid}>
@@ -245,7 +264,6 @@ export function EditVendorForm({
245
264
  </Grid>
246
265
  );
247
266
  })}
248
-
249
267
  <Grid
250
268
  item
251
269
  xs={12}
@@ -285,25 +303,20 @@ export function EditVendorForm({
285
303
  ) : (
286
304
  <Typography>No notes to display</Typography>
287
305
  )}
288
- </Grid>
289
-
306
+ </Grid>{" "}
290
307
  <Grid item xs={12}>
291
308
  <Stack
292
- direction="row-reverse"
309
+ direction="row"
293
310
  spacing={2}
294
- alignItems={"center"}
295
- sx={{
296
- py: 1,
297
- }}
311
+ justifyContent="flex-end"
312
+ sx={{ mt: 2 }}
298
313
  >
299
- <SubmitButton text="update vendor" loadingText="loading" />
300
- {formState?.strapiErrors && (
301
- <StrapiErrors error={formState?.strapiErrors} />
302
- )}
303
-
304
- {formState?.message && (
305
- <Alert severity="error">{formState?.message}</Alert>
314
+ {handleClose && (
315
+ <Button onClick={handleClose} variant="outlined">
316
+ Cancel
317
+ </Button>
306
318
  )}
319
+ <SubmitButton text="Update Vendor" loadingText="Updating..." />
307
320
  </Stack>
308
321
  </Grid>
309
322
  </Grid>
@@ -5,44 +5,60 @@ import { flattenAttributes } from "../../../../lib/utils";
5
5
  import { parseFormData } from "../../../../lib/parseFormData";
6
6
 
7
7
  export async function createVendorAction(prevState: any, formData: FormData) {
8
- const rawFormData = Object.fromEntries(formData);
8
+ try {
9
+ const parsedFormData = parseFormData(formData);
9
10
 
10
- const parsedFormData = parseFormData(formData);
11
+ if (parsedFormData.data.products) {
12
+ parsedFormData.data.products = JSON.parse(parsedFormData.data.products);
13
+ }
11
14
 
12
- if (parsedFormData.data.products) {
13
- parsedFormData.data.products = JSON.parse(parsedFormData.data.products);
14
- }
15
+ const responseData = await mutateData(
16
+ "POST",
17
+ `/api/vendor-profiles`,
18
+ parsedFormData
19
+ );
20
+
21
+ if (!responseData) {
22
+ return {
23
+ ...prevState,
24
+ strapiErrors: null,
25
+ zodErrors: null,
26
+ message:
27
+ "Something went wrong while creating the vendor. Please try again.",
28
+ severity: "error" as const,
29
+ };
30
+ }
15
31
 
16
- const responseData = await mutateData(
17
- "POST",
18
- `/api/vendor-profiles`,
19
- parsedFormData
20
- );
32
+ if (responseData.error) {
33
+ return {
34
+ ...prevState,
35
+ strapiErrors: responseData.error,
36
+ zodErrors: null,
37
+ message:
38
+ "Failed to create vendor. Please check the form and try again.",
39
+ severity: "error" as const,
40
+ };
41
+ }
21
42
 
22
- console.dir(responseData, { depth: null });
43
+ const flattenedData = flattenAttributes(responseData);
23
44
 
24
- if (!responseData) {
25
45
  return {
26
46
  ...prevState,
47
+ data: flattenedData,
27
48
  strapiErrors: null,
28
- message: "Ops! Something went wrong. Please try again.",
49
+ zodErrors: null,
50
+ message: "Vendor created successfully!",
51
+ severity: "success" as const,
29
52
  };
30
- }
31
-
32
- if (responseData.error) {
53
+ } catch (error) {
54
+ console.error("Error creating vendor:", error);
33
55
  return {
34
56
  ...prevState,
35
- strapiErrors: responseData.error,
36
- message: "Failed to Create Vendor.",
57
+ data: null,
58
+ strapiErrors: null,
59
+ zodErrors: null,
60
+ message: "An unexpected error occurred. Please try again later.",
61
+ severity: "error" as const,
37
62
  };
38
63
  }
39
-
40
- const flattenedData = flattenAttributes(responseData);
41
-
42
- return {
43
- ...prevState,
44
- message: "New Vendor Created",
45
- data: flattenedData,
46
- strapiErrors: null,
47
- };
48
64
  }
@@ -4,43 +4,75 @@ import { mutateData } from "../../../services/mutate-data";
4
4
  import { flattenAttributes } from "../../../../lib/utils";
5
5
  import { parseFormData } from "../../../../lib/parseFormData";
6
6
 
7
- export async function updateVendorAction(prevState: any, formData: FormData) {
8
- const rawFormData = Object.fromEntries(formData);
7
+ interface UpdateVendorState {
8
+ data: any | null;
9
+ message: string | null;
10
+ severity: "success" | "error" | "info" | "warning" | null;
11
+ strapiErrors: any | null;
12
+ zodErrors: any | null;
13
+ }
9
14
 
10
- const parsedFormData = parseFormData(formData);
15
+ export async function updateVendorAction(
16
+ prevState: any,
17
+ formData: FormData
18
+ ): Promise<UpdateVendorState> {
19
+ try {
20
+ const rawFormData = Object.fromEntries(formData);
21
+ const parsedFormData = parseFormData(formData);
11
22
 
12
- if (parsedFormData.data.products) {
13
- parsedFormData.data.products = JSON.parse(parsedFormData.data.products);
14
- }
23
+ if (parsedFormData.data.products) {
24
+ parsedFormData.data.products = JSON.parse(parsedFormData.data.products);
25
+ }
26
+
27
+ const responseData = await mutateData(
28
+ "PUT",
29
+ `/api/vendor-profiles/${rawFormData.id}`,
30
+ parsedFormData
31
+ );
32
+
33
+ if (!responseData) {
34
+ return {
35
+ ...prevState,
36
+ data: null,
37
+ strapiErrors: null,
38
+ zodErrors: null,
39
+ message:
40
+ "Something went wrong while updating the vendor. Please try again.",
41
+ severity: "error" as const,
42
+ };
43
+ }
44
+
45
+ if (responseData.error) {
46
+ return {
47
+ ...prevState,
48
+ data: null,
49
+ strapiErrors: responseData.error,
50
+ zodErrors: null,
51
+ message:
52
+ "Failed to update vendor. Please check the form and try again.",
53
+ severity: "error" as const,
54
+ };
55
+ }
15
56
 
16
- const responseData = await mutateData(
17
- "PUT",
18
- `/api/vendor-profiles/${rawFormData.id}`,
19
- parsedFormData
20
- );
57
+ const flattenedData = flattenAttributes(responseData);
21
58
 
22
- if (!responseData) {
23
59
  return {
24
60
  ...prevState,
61
+ data: flattenedData,
25
62
  strapiErrors: null,
26
- message: "Ops! Something went wrong. Please try again.",
63
+ zodErrors: null,
64
+ message: "Vendor updated successfully!",
65
+ severity: "success" as const,
27
66
  };
28
- }
29
-
30
- if (responseData.error) {
67
+ } catch (error) {
68
+ console.error("Error updating vendor:", error);
31
69
  return {
32
70
  ...prevState,
33
- strapiErrors: responseData.error,
34
- message: "Failed to Update Vendor.",
71
+ data: null,
72
+ strapiErrors: null,
73
+ zodErrors: null,
74
+ message: "An unexpected error occurred. Please try again later.",
75
+ severity: "error" as const,
35
76
  };
36
77
  }
37
-
38
- const flattenedData = flattenAttributes(responseData);
39
-
40
- return {
41
- ...prevState,
42
- message: "Vendor Updated",
43
- data: flattenedData,
44
- strapiErrors: null,
45
- };
46
78
  }