@campxdev/campx-web-utils 0.1.6 → 0.1.8

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/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@campxdev/campx-web-utils",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "main": "./exports.ts",
5
5
  "private": false,
6
6
  "dependencies": {
7
- "@campxdev/react-blueprint": "^1.0.6",
7
+ "@campxdev/react-blueprint": "^1.0.14",
8
8
  "@mui/x-date-pickers": "^7.11.0",
9
9
  "@testing-library/jest-dom": "^5.14.1",
10
10
  "@testing-library/react": "^13.0.0",
@@ -2,7 +2,15 @@ import { useQuery } from "react-query";
2
2
  import { axios } from "./config/axios";
3
3
 
4
4
  const fetchClubRequests = () => {
5
- return axios.get(`/square/clubs`).then((res) => res.data);
5
+ return axios
6
+ .get(`/hrms/departments`, {
7
+ headers: {
8
+ "x-tenant-id": "aupulse",
9
+ "X-Institution-Code": "aupulse",
10
+ campx_session_key: "66a36c752e28b1200876b034",
11
+ },
12
+ })
13
+ .then((res) => res.data);
6
14
  };
7
15
 
8
16
  function AppContent() {
@@ -1,9 +1,8 @@
1
- import { PageNotFound } from "@campxdev/react-blueprint";
2
1
  import { InternalServerError } from "@campxdev/react-blueprint/src/components/Assets/ErrorPages/InternalServerError";
3
2
  import { NoInterneConnection } from "@campxdev/react-blueprint/src/components/Assets/ErrorPages/NoInternetConnection";
4
3
  import { UnAuthorized } from "@campxdev/react-blueprint/src/components/Assets/ErrorPages/UnAuthorized";
5
- import { Typography } from "@campxdev/react-blueprint/src/components/DataDisplay/Typography/Typography";
6
4
  import { Button } from "@campxdev/react-blueprint/src/components/Input/Button/Button";
5
+ import { Alert, Box, styled } from "@mui/material";
7
6
  import Cookies from "js-cookie";
8
7
  import { ReactNode } from "react";
9
8
  import { ErrorBoundary as ReactErrorBoundary } from "react-error-boundary";
@@ -16,9 +15,28 @@ export type ErrorBoundaryProps = {
16
15
  resetKey?: string;
17
16
  };
18
17
 
18
+ const StyledAlert = styled(Alert)(({ theme }) => ({
19
+ height: "60px",
20
+ border: `1px solid ${theme.palette.error.main}`,
21
+ display: "flex",
22
+ alignItems: "center",
23
+ "& .MuiAlert-message": {
24
+ padding: 0,
25
+ },
26
+ "& .MuiTypography-root": {
27
+ margin: 0,
28
+ },
29
+ position: "relative",
30
+ "& .retryBtn": {
31
+ color: "#661B2A",
32
+ position: "absolute",
33
+ right: 8,
34
+ top: 8,
35
+ },
36
+ }));
37
+
19
38
  export const ErrorBoundary = (props: ErrorBoundaryProps) => {
20
39
  const location = useLocation();
21
- console.log(location?.pathname, "kkk");
22
40
  return (
23
41
  <QueryErrorResetBoundary>
24
42
  {({ reset }) => (
@@ -35,33 +53,36 @@ export const ErrorBoundary = (props: ErrorBoundaryProps) => {
35
53
  };
36
54
 
37
55
  export const ErrorFallback = ({ error, resetErrorBoundary }: any) => {
56
+ console.log(error, "llll");
38
57
  if (error?.response?.status) {
39
- console.log(error?.response?.status);
40
58
  switch (error?.response?.status) {
41
59
  case 401:
42
60
  return <UnAuth resetErrorBoundary={resetErrorBoundary} />;
43
61
 
44
62
  case 500:
45
63
  return <InternalServerError resetBoundary={resetErrorBoundary} />;
46
-
47
- case 404:
48
- return <PageNotFound resetErrorBoundary={resetErrorBoundary} />;
49
-
50
- default:
51
- return <InternalServerError resetBoundary={resetErrorBoundary} />;
52
64
  }
53
65
  }
54
66
 
55
- if (error?.message === "Network Error") {
67
+ if (error?.code === "ERR_NETWORK") {
56
68
  return <NoInterneConnection resetBoundary={resetErrorBoundary} />;
57
69
  }
58
70
 
59
71
  return (
60
- <>
61
- <Typography>
72
+ <Box sx={{ marginTop: "16px", padding: "20px" }}>
73
+ <StyledAlert severity="error">
62
74
  {error?.response?.data?.message ?? error?.message}
63
- </Typography>
64
- </>
75
+ <Button
76
+ className="retryBtn"
77
+ onClick={() => resetErrorBoundary()}
78
+ size="small"
79
+ color="error"
80
+ variant="outlined"
81
+ >
82
+ Try Again
83
+ </Button>
84
+ </StyledAlert>
85
+ </Box>
65
86
  );
66
87
  };
67
88
 
@@ -1,12 +1,9 @@
1
1
  import { Outlet } from "react-router-dom";
2
- import { ErrorBoundary } from "../ErrorBoundary/export";
3
2
 
4
3
  const AppLayout = () => {
5
4
  return (
6
5
  <>
7
- <ErrorBoundary>
8
- <Outlet />
9
- </ErrorBoundary>
6
+ <Outlet />
10
7
  </>
11
8
  );
12
9
  };
@@ -1,5 +1,6 @@
1
1
  import Axios, { InternalAxiosRequestConfig } from "axios";
2
2
  import Cookies from "js-cookie";
3
+ import { SnackbarStore } from "../context/SnackbarProvider";
3
4
 
4
5
  const isDevelopment = process.env.NODE_ENV == "development";
5
6
 
@@ -53,9 +54,30 @@ axios.interceptors.request.use(
53
54
 
54
55
  axios.interceptors.response.use(
55
56
  function (response) {
57
+ console.log(response);
58
+ if (
59
+ response.config.method &&
60
+ ["put", "post", "delete", "patch"].includes(response.config.method) &&
61
+ [200, 202].includes(response.status)
62
+ ) {
63
+ SnackbarStore.update((s) => {
64
+ s.open = true;
65
+ s.message = response.data.message;
66
+ s.variant = "success";
67
+ });
68
+ }
69
+
56
70
  return response;
57
71
  },
58
72
  function (err) {
73
+ if ([400, 404, 422].includes(err.response.status)) {
74
+ SnackbarStore.update((s) => {
75
+ s.open = true;
76
+ s.message = err.response.data.message || "Bad Request";
77
+ s.variant = "alert";
78
+ });
79
+ }
80
+
59
81
  return Promise.reject(err);
60
82
  }
61
83
  );
@@ -2,6 +2,8 @@ import { MuiThemeProvider } from "@campxdev/react-blueprint";
2
2
  import { ReactNode } from "react";
3
3
  import { QueryClient, QueryClientProvider } from "react-query";
4
4
  import { BrowserRouter } from "react-router-dom";
5
+ import { ErrorBoundary } from "../ErrorBoundary/ErrorBoundary";
6
+ import { SnackbarProvider } from "./SnackbarProvider";
5
7
 
6
8
  export const queryClient = new QueryClient({
7
9
  defaultOptions: {
@@ -19,7 +21,11 @@ export default function Providers({ children }: { children: ReactNode }) {
19
21
  return (
20
22
  <BrowserRouter basename={baseName}>
21
23
  <QueryClientProvider client={queryClient}>
22
- <MuiThemeProvider>{children}</MuiThemeProvider>
24
+ <MuiThemeProvider>
25
+ <SnackbarProvider>
26
+ <ErrorBoundary>{children}</ErrorBoundary>
27
+ </SnackbarProvider>
28
+ </MuiThemeProvider>
23
29
  </QueryClientProvider>
24
30
  </BrowserRouter>
25
31
  );
@@ -0,0 +1,49 @@
1
+ import { Snackbar } from "@campxdev/react-blueprint";
2
+ import { Store } from "pullstate";
3
+ import { createContext, ReactNode } from "react";
4
+
5
+ type Variant = "success" | "info" | "warning" | "alert";
6
+
7
+ interface SnackbarContextProps {
8
+ showSnackbar: (message: string, variant?: Variant) => void;
9
+ }
10
+
11
+ const SnackbarContext = createContext<SnackbarContextProps | undefined>(
12
+ undefined
13
+ );
14
+
15
+ interface SnackbarProviderProps {
16
+ children: ReactNode;
17
+ }
18
+ export const SnackbarStore = new Store({
19
+ open: false,
20
+ message: "",
21
+ variant: "success" as Variant,
22
+ });
23
+
24
+ export const SnackbarProvider = ({ children }: SnackbarProviderProps) => {
25
+ const snackbar = SnackbarStore.useState((s) => s);
26
+ const showSnackbar = (message: string, variant: Variant = "info") => {
27
+ SnackbarStore.update((s) => {
28
+ s.open = true;
29
+ s.message = message;
30
+ s.variant = variant;
31
+ });
32
+ };
33
+ return (
34
+ <SnackbarContext.Provider value={{ showSnackbar }}>
35
+ {children}
36
+ <Snackbar
37
+ open={snackbar.open}
38
+ message={snackbar.message}
39
+ variant={snackbar.variant}
40
+ autoHideDuration={1500}
41
+ onClose={() => {
42
+ SnackbarStore.update((s) => {
43
+ s.open = false;
44
+ });
45
+ }}
46
+ />
47
+ </SnackbarContext.Provider>
48
+ );
49
+ };