umwd-components 0.1.686 → 0.1.687

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 +1 @@
1
- export declare function createReportAction(related: any, prevState: any, formData: FormData): Promise<any>;
1
+ export declare function createReportAction(related: any, type: string, prevState: any, formData: FormData): Promise<any>;
@@ -0,0 +1,10 @@
1
+ import { User } from "@/types/user";
2
+ export declare function getUserMeLoader(query?: string): Promise<{
3
+ ok: boolean;
4
+ data: null;
5
+ error: any;
6
+ } | {
7
+ ok: boolean;
8
+ data: User;
9
+ error: null;
10
+ }>;
@@ -16,6 +16,8 @@ export type Report = {
16
16
  type: ReportType;
17
17
  related: PolymorphicRelation[];
18
18
  author: any;
19
+ signature_username?: string;
20
+ signature_email?: string;
19
21
  };
20
22
  export interface ReportMakingComponentProps {
21
23
  author?: string;
@@ -0,0 +1,14 @@
1
+ import { CustomerProfileProps } from "./e-commerce/customer/types";
2
+ import { EnduserProfileProps } from "./e-commerce/enduser/types";
3
+ import { DispatcherProfileProps } from "./logistics/dispatcher/types";
4
+ export type User = {
5
+ id: string;
6
+ username: string;
7
+ email: string;
8
+ role?: "customer" | "enduser" | "dispatcher";
9
+ created_at: Date;
10
+ updated_at: Date;
11
+ customer_profile?: CustomerProfileProps;
12
+ enduser_profile?: EnduserProfileProps;
13
+ dispatcher_profile?: DispatcherProfileProps;
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "umwd-components",
3
- "version": "0.1.686",
3
+ "version": "0.1.687",
4
4
  "description": "UMWD Component library",
5
5
  "main": "dist/src/index.js",
6
6
  "module": "dist/src/index.js",
@@ -26,6 +26,7 @@ import Button from "@mui/material/Button";
26
26
  import AmountIndicator from "../../../components/common/AmountIndicator";
27
27
  import { IpoItem } from "../../../types/logistics/Ipo";
28
28
  import { OpoItem } from "../../../types/e-commerce/opo/types";
29
+ import { alignProperty } from "@mui/material/styles/cssUtils";
29
30
 
30
31
  const INITIAL_STATE = {
31
32
  zodErrors: null,
@@ -384,13 +385,15 @@ export function EditStockForm({
384
385
  not forget to click the button!
385
386
  </Typography>
386
387
  <ReportsDisplay reports={reports} />
387
- <ReportMakingComponent
388
- content={"content"}
389
- quantity={0}
390
- type="stock"
391
- related={[{ id: id, __type: "api::e-commerce.product" }]}
392
- revalidateCallback={revalidateCallback}
393
- />
388
+ <Stack direction="row" justifyContent={"flex-end"}>
389
+ <ReportMakingComponent
390
+ content={"content"}
391
+ quantity={0}
392
+ type="stock"
393
+ related={[{ id: id, __type: "api::e-commerce.product" }]}
394
+ revalidateCallback={revalidateCallback}
395
+ />
396
+ </Stack>
394
397
  </Stack>
395
398
  </Grid>
396
399
  <Grid item xs={12}>
@@ -19,7 +19,8 @@ import { ReportMakingComponentProps } from "../../../types/logistics/Report";
19
19
  import MarkdownEditor from "../../../components/common/markdown/MarkdownEditor";
20
20
  import AnnouncementIcon from "@mui/icons-material/Announcement";
21
21
  import { Box, Tooltip } from "@mui/material";
22
- import { Block } from "@mui/icons-material";
22
+ import { getUserMeLoader } from "../../../data/loaders/user/getUserMeLoader";
23
+ import { useSnackbar } from "../../../context/common/SnackbarContext";
23
24
 
24
25
  const INITIAL_STATE = {
25
26
  zodErrors: null,
@@ -40,9 +41,17 @@ export default function ReportMakingComponent({
40
41
  }: ReportMakingComponentProps) {
41
42
  const [open, setOpen] = useState(false);
42
43
 
44
+ interface User {
45
+ username: string;
46
+ email: string;
47
+ }
48
+
49
+ const [currentUser, setCurrentUser] = useState<User | null>(null);
50
+
43
51
  const createReportWithRelationsAction = createReportAction.bind(
44
52
  null,
45
- related
53
+ related,
54
+ type
46
55
  );
47
56
 
48
57
  const [formState, formAction] = useFormState(
@@ -50,11 +59,35 @@ export default function ReportMakingComponent({
50
59
  INITIAL_STATE
51
60
  );
52
61
 
62
+ const { handleAddMessage } = useSnackbar();
63
+
64
+ useEffect(() => {
65
+ if (formState?.message) {
66
+ handleAddMessage({
67
+ message: formState.message,
68
+ severity: formState.severity || "error",
69
+ });
70
+
71
+ if (formState.severity === "success") {
72
+ revalidateCallback && revalidateCallback();
73
+ setOpen(false);
74
+ }
75
+ }
76
+ }, [formState?.message]);
77
+
53
78
  useEffect(() => {
54
- if (formState?.data && revalidateCallback) {
55
- revalidateCallback();
79
+ async function fetchUser() {
80
+ const { ok, data, error } = await getUserMeLoader();
81
+ if (ok && data) {
82
+ console.log("Current User:", data);
83
+ setCurrentUser(data);
84
+ } else {
85
+ setCurrentUser(null);
86
+ console.error("Error fetching current user:", error);
87
+ }
56
88
  }
57
- }, [formState.data]);
89
+ fetchUser();
90
+ }, []);
58
91
 
59
92
  return (
60
93
  <>
@@ -69,8 +102,8 @@ export default function ReportMakingComponent({
69
102
  <Dialog open={open} fullWidth={true} maxWidth={"lg"}>
70
103
  <form action={formAction}>
71
104
  <DialogContent sx={{ p: 1 }}>
105
+ {" "}
72
106
  <Stack spacing={2}>
73
- <input type="hidden" name="type" value={type} />
74
107
  <Typography variant="body1" textAlign={"right"}>
75
108
  {dayjs(Date.now()).format("DD-MM-YYYY")}
76
109
  </Typography>
@@ -111,6 +144,16 @@ export default function ReportMakingComponent({
111
144
  label="The report"
112
145
  defaultValue={content}
113
146
  />
147
+ {currentUser !== null && (
148
+ <>
149
+ <Typography variant="body1" textAlign={"right"}>
150
+ {currentUser.username}
151
+ </Typography>
152
+ <Typography variant="body2" textAlign={"right"}>
153
+ {currentUser.email}
154
+ </Typography>
155
+ </>
156
+ )}
114
157
  </Stack>
115
158
  </DialogContent>
116
159
  <DialogActions>
@@ -15,23 +15,64 @@ const ReportsDisplay = ({ reports }: { reports: Report[] }) => {
15
15
  reports.map((report, index) => {
16
16
  return (
17
17
  <Grid container key={index}>
18
- <Grid item xs={1}>
18
+ <Grid
19
+ item
20
+ xs={1}
21
+ sx={{
22
+ display: "flex",
23
+ alignItems: "center",
24
+ justifyContent: "flex-start",
25
+ }}
26
+ >
19
27
  <Chip
20
28
  label={`${report.quantity > 0 ? "+" : ""} ${report.quantity}`}
21
29
  color={report.quantity > 0 ? "success" : "error"}
22
30
  size="small"
23
31
  />
24
32
  </Grid>
25
- <Grid item xs={7}>
33
+ <Grid
34
+ item
35
+ xs={7}
36
+ sx={{
37
+ display: "flex",
38
+ alignItems: "center",
39
+ justifyContent: "flex-start",
40
+ }}
41
+ >
26
42
  <MarkdownDisplay>{report.content}</MarkdownDisplay>
27
43
  </Grid>
28
- <Grid item xs={2}>
44
+ <Grid
45
+ item
46
+ xs={2}
47
+ sx={{
48
+ display: "flex",
49
+ alignItems: "center",
50
+ justifyContent: "flex-start",
51
+ }}
52
+ >
29
53
  <Typography variant="body2"> {report.date}</Typography>
30
54
  </Grid>
31
- <Grid item xs={2}>
32
- <Typography variant="body2" align="right">
33
- {report.author?.username ? report.author.username : "Unknown"}
34
- </Typography>
55
+ <Grid
56
+ item
57
+ xs={2}
58
+ sx={{
59
+ display: "flex",
60
+ alignItems: "center",
61
+ justifyContent: "flex-start",
62
+ }}
63
+ >
64
+ <Stack direction="column" alignItems={"flex-end"}>
65
+ {report.signature_username && (
66
+ <Typography variant="body1">
67
+ {report.signature_username}
68
+ </Typography>
69
+ )}
70
+ {report.signature_username && (
71
+ <Typography variant="body2">
72
+ {report.signature_email}
73
+ </Typography>
74
+ )}
75
+ </Stack>
35
76
  </Grid>
36
77
 
37
78
  <Grid item xs={12}>
@@ -7,15 +7,16 @@ import { parseFormData } from "../../../../lib/parseFormData";
7
7
 
8
8
  export async function createReportAction(
9
9
  related: any,
10
+ type: string,
10
11
  prevState: any,
11
12
  formData: FormData
12
13
  ) {
13
14
  const parsedFormData = parseFormData(formData);
14
-
15
15
  const reportData = {
16
16
  data: {
17
17
  ...parsedFormData.data,
18
18
  related,
19
+ type,
19
20
  },
20
21
  };
21
22
 
@@ -46,6 +47,7 @@ export async function createReportAction(
46
47
  return {
47
48
  ...prevState,
48
49
  message: "New Report Created",
50
+ severity: "success",
49
51
  data: flattenedData,
50
52
  strapiErrors: null,
51
53
  };
@@ -0,0 +1,49 @@
1
+ "use server";
2
+
3
+ import { getAuthToken } from "../../services/get-token";
4
+ import qs from "qs";
5
+ import { flattenAttributes, getStrapiURL } from "../../../lib/utils";
6
+ import { unstable_noStore as noStore } from "next/cache";
7
+ import { User } from "@/types/user";
8
+
9
+ const baseUrl = getStrapiURL();
10
+
11
+ const defaultQuery = qs.stringify({
12
+ fields: ["id", "username", "email", "createdAt", "updatedAt"],
13
+ });
14
+
15
+ export async function getUserMeLoader(query: string = defaultQuery) {
16
+ noStore();
17
+ const url = new URL("/api/users/me", baseUrl);
18
+ url.search = query;
19
+
20
+ const authToken = await getAuthToken();
21
+ if (!authToken) return { ok: false, data: null, error: null };
22
+
23
+ try {
24
+ const response = await fetch(url.href, {
25
+ method: "GET",
26
+ headers: {
27
+ "Content-Type": "application/json",
28
+ Authorization: `Bearer ${authToken}`,
29
+ },
30
+ cache: "no-cache", // Ensure the response is not cached and revalidated on every render
31
+ });
32
+
33
+ // Check if the response status is not OK (i.e., not in the range 200-299)
34
+ if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
35
+
36
+ const data = await response.json();
37
+
38
+ // Check if the data contains an error field
39
+ if (data.error) return { ok: false, data: null, error: data.error };
40
+
41
+ // If everything is fine, return the data
42
+ // return { ok: true, data: data, error: null };
43
+ const user: User = flattenAttributes(data);
44
+ return { ok: true, data: user, error: null };
45
+ } catch (error) {
46
+ console.error("getUserMeLoader Error:", error);
47
+ return { ok: false, data: null, error: error };
48
+ }
49
+ }
@@ -28,6 +28,8 @@ export type Report = {
28
28
  type: ReportType;
29
29
  related: PolymorphicRelation[];
30
30
  author: any;
31
+ signature_username?: string; // name of the person who made the report, should be set by the cms
32
+ signature_email?: string; // email of the person who made the report, should be set by the cms
31
33
  };
32
34
 
33
35
  export interface ReportMakingComponentProps {
@@ -0,0 +1,15 @@
1
+ import { CustomerProfileProps } from "./e-commerce/customer/types";
2
+ import { EnduserProfileProps } from "./e-commerce/enduser/types";
3
+ import { DispatcherProfileProps } from "./logistics/dispatcher/types";
4
+
5
+ export type User = {
6
+ id: string;
7
+ username: string;
8
+ email: string;
9
+ role?: "customer" | "enduser" | "dispatcher";
10
+ created_at: Date;
11
+ updated_at: Date;
12
+ customer_profile?: CustomerProfileProps;
13
+ enduser_profile?: EnduserProfileProps;
14
+ dispatcher_profile?: DispatcherProfileProps;
15
+ };