umwd-components 0.1.642 → 0.1.644

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,12 +1,6 @@
1
1
  "use client";
2
2
 
3
- import React, {
4
- createContext,
5
- useContext,
6
- useReducer,
7
- useEffect,
8
- ReactNode,
9
- } from "react";
3
+ import React, { createContext, useContext, useReducer, ReactNode } from "react";
10
4
  import {
11
5
  snackbarMessage,
12
6
  SnackbarState,
@@ -18,7 +12,6 @@ const initialState: SnackbarState = {
18
12
  messages: [],
19
13
  };
20
14
 
21
- // TODO this is a work in progress
22
15
  const SnackbarContext = createContext<{
23
16
  state: SnackbarState;
24
17
  dispatch: React.Dispatch<any>;
@@ -26,32 +19,23 @@ const SnackbarContext = createContext<{
26
19
  message: string;
27
20
  severity: "success" | "error" | "info" | "warning";
28
21
  }) => void;
29
- /* messages: snackbarMessage[];
30
- handleAddMessage: (value: {
31
- message: string;
32
- severity: "success" | "error" | "info" | "warning";
33
- }) => void;
34
22
  handleAddMessages: (
35
23
  value: {
36
24
  message: string;
37
25
  severity: "success" | "error" | "info" | "warning";
38
26
  }[]
39
27
  ) => void;
40
- handleOpen: (value: boolean, index: number) => void;
41
28
  handleClose: (
42
29
  event: Event | React.SyntheticEvent<any, Event>,
43
30
  reason: SnackbarCloseReason,
44
31
  id: string
45
- ) => void; */
32
+ ) => void;
46
33
  }>({
47
34
  state: initialState,
48
35
  dispatch: () => undefined,
49
36
  handleAddMessage: () => undefined,
50
- /* messages: [],
51
- handleAddMessage: () => undefined,
52
37
  handleAddMessages: () => undefined,
53
- handleOpen: () => undefined,
54
- handleClose: () => undefined, */
38
+ handleClose: () => undefined,
55
39
  });
56
40
 
57
41
  const snackbarReducer = (state: SnackbarState, action: any): SnackbarState => {
@@ -62,6 +46,8 @@ const snackbarReducer = (state: SnackbarState, action: any): SnackbarState => {
62
46
  };
63
47
  case "ADD_MESSAGE":
64
48
  return { ...state, messages: [...state.messages, action.payload] };
49
+ case "UPDATE_MESSAGES":
50
+ return { ...state, messages: action.payload };
65
51
  default:
66
52
  throw new Error(`No case for type ${action.type}`);
67
53
  }
@@ -70,30 +56,22 @@ const snackbarReducer = (state: SnackbarState, action: any): SnackbarState => {
70
56
  export const SnackbarProvider = ({ children }: { children: ReactNode }) => {
71
57
  const [state, dispatch] = useReducer(snackbarReducer, initialState);
72
58
 
73
- /* const handleOpen = (value: boolean, index: number) => {
74
- const newMessages = [...state.messages];
75
- newMessages[index].open = value;
76
-
77
- dispatch({ type: "ADD_MESSAGE", payload: newMessages });
78
- }; */
79
-
80
- /* const handleClose = (
59
+ const handleClose = (
81
60
  event: Event | React.SyntheticEvent<any, Event>,
82
61
  reason: SnackbarCloseReason,
83
62
  id: string
84
63
  ) => {
85
- console.log(event, reason, id);
86
64
  if (reason === "clickaway") {
87
65
  return;
88
66
  }
89
67
 
90
68
  const newMessages = [...state.messages];
91
-
92
69
  const index = newMessages.findIndex((message) => message.id === id);
93
- newMessages[index].open = false;
94
-
95
- dispatch({ type: "ADD_MESSAGE", payload: newMessages });
96
- }; */
70
+ if (index !== -1) {
71
+ newMessages[index].open = false;
72
+ dispatch({ type: "UPDATE_MESSAGES", payload: newMessages });
73
+ }
74
+ };
97
75
 
98
76
  const handleAddMessage = React.useCallback(
99
77
  (value: {
@@ -106,35 +84,31 @@ export const SnackbarProvider = ({ children }: { children: ReactNode }) => {
106
84
  [dispatch]
107
85
  );
108
86
 
109
- /* const handleAddMessages = (
110
- value: {
111
- message: string;
112
- severity: "success" | "error" | "info" | "warning";
113
- }[]
114
- ) => {
115
- const newMessages = [
116
- ...state.messages,
117
- ...value.map((item) => ({ ...item, open: true, id: uuidV4() })),
118
- ];
119
-
120
- console.log("new messages from contextV2", newMessages);
121
-
122
- dispatch({ type: "ADD_MESSAGE", payload: newMessages });
123
- }; */
124
-
125
- /* useEffect(() => {
126
- dispatch({ type: "SET_STATE", payload: initialState });
127
- }, [initialState]); */
87
+ const handleAddMessages = React.useCallback(
88
+ (
89
+ value: {
90
+ message: string;
91
+ severity: "success" | "error" | "info" | "warning";
92
+ }[]
93
+ ) => {
94
+ const newMessages = value.map((item) => ({
95
+ ...item,
96
+ open: true,
97
+ id: uuidV4(),
98
+ }));
99
+ newMessages.forEach((message) => {
100
+ dispatch({ type: "ADD_MESSAGE", payload: message });
101
+ });
102
+ },
103
+ [dispatch]
104
+ );
128
105
 
129
106
  const value = {
130
107
  state,
131
108
  dispatch,
132
109
  handleAddMessage,
133
- /* messages: state.messages,
134
- handleAddMessage,
135
110
  handleAddMessages,
136
- handleOpen,
137
- handleClose, */
111
+ handleClose,
138
112
  };
139
113
 
140
114
  return (
@@ -41,6 +41,7 @@ export async function cancelIroAction(prevState: any, formData: FormData) {
41
41
 
42
42
  return {
43
43
  ...prevState,
44
+ severity: "success",
44
45
  message: "Iro Cancelled Successfully.",
45
46
  data: flattenedData,
46
47
  strapiErrors: null,