@bluemarble/bm-components 0.0.92 → 0.0.94

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/dist/index.mjs CHANGED
@@ -52,6 +52,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
52
52
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
53
53
  mod
54
54
  ));
55
+ var __async = (__this, __arguments, generator) => {
56
+ return new Promise((resolve, reject) => {
57
+ var fulfilled = (value) => {
58
+ try {
59
+ step(generator.next(value));
60
+ } catch (e) {
61
+ reject(e);
62
+ }
63
+ };
64
+ var rejected = (value) => {
65
+ try {
66
+ step(generator.throw(value));
67
+ } catch (e) {
68
+ reject(e);
69
+ }
70
+ };
71
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
72
+ step((generator = generator.apply(__this, __arguments)).next());
73
+ });
74
+ };
55
75
 
56
76
  // node_modules/prop-types/node_modules/react-is/cjs/react-is.production.min.js
57
77
  var require_react_is_production_min = __commonJS({
@@ -3815,19 +3835,286 @@ function BaseGrid({
3815
3835
  );
3816
3836
  }
3817
3837
 
3818
- // src/components/hooks/useGrid.ts
3819
- import { useMemo as useMemo3, useState as useState4 } from "react";
3838
+ // src/components/Modal/index.tsx
3839
+ import React24 from "react";
3840
+ import {
3841
+ Box as Box4,
3842
+ Modal as MuiModal
3843
+ } from "@mui/material";
3844
+ var Modal = (_a) => {
3845
+ var _b = _a, { open, onClose } = _b, rest = __objRest(_b, ["open", "onClose"]);
3846
+ return /* @__PURE__ */ React24.createElement(MuiModal, __spreadValues({ open, onClose, disableEnforceFocus: true }, rest), /* @__PURE__ */ React24.createElement(
3847
+ Box4,
3848
+ {
3849
+ sx: {
3850
+ outline: "none",
3851
+ backgroundColor: "white",
3852
+ position: "absolute",
3853
+ top: "50%",
3854
+ left: "50%",
3855
+ transform: "translate(-50%, -50%)",
3856
+ borderRadius: 1
3857
+ }
3858
+ },
3859
+ /* @__PURE__ */ React24.createElement(React24.Fragment, null, rest.children)
3860
+ ));
3861
+ };
3862
+
3863
+ // src/components/utils/GetInputLabel.ts
3864
+ function GetInputLabel(columns) {
3865
+ return (columnName) => {
3866
+ const column = columns.find((column2) => column2.name === columnName);
3867
+ return { label: column.label, name: column.name };
3868
+ };
3869
+ }
3870
+
3871
+ // src/components/Dialog/index.tsx
3872
+ import React25 from "react";
3873
+ import {
3874
+ Box as Box5,
3875
+ Dialog as DefaultDialog,
3876
+ DialogContentText,
3877
+ DialogActions,
3878
+ DialogTitle,
3879
+ Button as Button3,
3880
+ CircularProgress as CircularProgress2
3881
+ } from "@mui/material";
3882
+ var Dialog = (_a) => {
3883
+ var _b = _a, {
3884
+ open,
3885
+ title,
3886
+ loading,
3887
+ body,
3888
+ options
3889
+ } = _b, rest = __objRest(_b, [
3890
+ "open",
3891
+ "title",
3892
+ "loading",
3893
+ "body",
3894
+ "options"
3895
+ ]);
3896
+ return /* @__PURE__ */ React25.createElement(DefaultDialog, __spreadValues({ open }, rest), /* @__PURE__ */ React25.createElement(Box5, { sx: { p: 2 } }, /* @__PURE__ */ React25.createElement(DialogTitle, { sx: { fontWeight: "bold" } }, title), /* @__PURE__ */ React25.createElement(DialogContentText, { sx: { px: "10px", textAlign: "center", mb: 2 } }, body), /* @__PURE__ */ React25.createElement(DialogActions, null, options.map((option, index) => {
3897
+ return /* @__PURE__ */ React25.createElement(
3898
+ Button3,
3899
+ {
3900
+ key: index,
3901
+ onClick: () => option.cb(option.label),
3902
+ variant: option.focus ? "contained" : "text",
3903
+ sx: {
3904
+ fontWeight: option.focus ? "bold" : "normal",
3905
+ color: option.focus ? "#fff" : "primary.main"
3906
+ },
3907
+ disableElevation: true,
3908
+ disabled: loading
3909
+ },
3910
+ loading && option.focus ? /* @__PURE__ */ React25.createElement(CircularProgress2, { size: 25, color: "inherit" }) : /* @__PURE__ */ React25.createElement(React25.Fragment, null, option.label)
3911
+ );
3912
+ }))));
3913
+ };
3914
+
3915
+ // src/errors/HttpError.ts
3916
+ var HttpError = class extends Error {
3917
+ constructor(status, message) {
3918
+ super(message);
3919
+ this.message = message;
3920
+ this.stack = `HttpError: ${message}`;
3921
+ this.status = status;
3922
+ }
3923
+ };
3924
+
3925
+ // src/helpers/apiHelper/index.ts
3926
+ var ApiHelper = class _ApiHelper {
3927
+ onFinally() {
3928
+ }
3929
+ constructor(props) {
3930
+ this.middlewares = (props == null ? void 0 : props.middlewares.reverse()) || [];
3931
+ this.onFinally = props.onFinally;
3932
+ }
3933
+ setMiddlewares(middlewares) {
3934
+ this.middlewares = middlewares.reverse();
3935
+ return this;
3936
+ }
3937
+ createMethods(methods) {
3938
+ return (req, res) => __async(this, null, function* () {
3939
+ const currentMethod = methods[req.method];
3940
+ try {
3941
+ if (!currentMethod)
3942
+ throw new HttpError(405, "M\xE9todo inv\xE1lido");
3943
+ const methodWithMiddlewares = this.middlewares.reduce(
3944
+ (acc, fn) => fn(acc),
3945
+ currentMethod
3946
+ );
3947
+ yield methodWithMiddlewares(req, res);
3948
+ } catch (error) {
3949
+ if (error instanceof HttpError)
3950
+ return res.status(error.status).json(error.message);
3951
+ if (process.env.NODE_ENV === "production")
3952
+ return res.status(500).json({
3953
+ code: "internal.error",
3954
+ error: "Erro interno do servidor",
3955
+ details: error
3956
+ });
3957
+ throw new Error(error);
3958
+ } finally {
3959
+ this.onFinally();
3960
+ }
3961
+ });
3962
+ }
3963
+ static parserErrorWrapper(body, parser) {
3964
+ try {
3965
+ const object = parser.parse(body);
3966
+ return object;
3967
+ } catch (error) {
3968
+ throw new HttpError(400, {
3969
+ code: "invalid.body",
3970
+ error: "Dados inv\xE1lidos",
3971
+ details: error
3972
+ });
3973
+ }
3974
+ }
3975
+ static parseQueyFilters(filters) {
3976
+ return Object.entries(filters).reduce((acc, [key, value]) => {
3977
+ if (value)
3978
+ acc[key] = value;
3979
+ return acc;
3980
+ }, {});
3981
+ }
3982
+ static create({ onFinally }) {
3983
+ return new _ApiHelper({
3984
+ onFinally
3985
+ });
3986
+ }
3987
+ };
3988
+
3989
+ // src/hooks/useFormHelper.ts
3990
+ import axios from "axios";
3991
+ import { useEffect as useEffect5, useRef as useRef4 } from "react";
3992
+
3993
+ // src/hooks/useAlert.ts
3994
+ import { useContext as useContext2 } from "react";
3995
+
3996
+ // src/contexts/AlertContext.tsx
3997
+ import React27 from "react";
3998
+ import { createContext as createContext2, useState as useState3 } from "react";
3999
+
4000
+ // src/components/Toast/index.tsx
4001
+ import React26 from "react";
4002
+ import { Alert, IconButton as IconButton4, Snackbar } from "@mui/material";
4003
+ import { MdClose as MdClose2 } from "react-icons/md";
4004
+
4005
+ // src/contexts/AlertContext.tsx
4006
+ var AlertContext = createContext2({});
4007
+
4008
+ // src/hooks/useAlert.ts
4009
+ var useAlert = () => {
4010
+ return useContext2(AlertContext);
4011
+ };
4012
+
4013
+ // src/hooks/useLoading.ts
4014
+ import { useState as useState4 } from "react";
4015
+ function useLoading() {
4016
+ const [state, setState] = useState4([]);
4017
+ const isLoading = (prop) => state.includes(prop);
4018
+ const setLoading = (prop, remove) => {
4019
+ if (remove)
4020
+ setState((prevState) => prevState.filter((state2) => state2 !== prop));
4021
+ else
4022
+ setState((prevState) => [...prevState, prop]);
4023
+ };
4024
+ return { isLoading, setLoading };
4025
+ }
4026
+
4027
+ // src/hooks/useFormHelper.ts
4028
+ function useFormHelper({ formatErrorMessage, api }) {
4029
+ const alertProps = useAlert();
4030
+ const loadingProps = useLoading();
4031
+ const { createAlert } = alertProps;
4032
+ const { setLoading } = loadingProps;
4033
+ const sourceRef = useRef4();
4034
+ function onSubmitWrapper(fn, { name }) {
4035
+ return (fields, methods) => __async(this, null, function* () {
4036
+ const LOADING_NAME = name;
4037
+ setLoading(LOADING_NAME);
4038
+ try {
4039
+ yield fn(fields, methods);
4040
+ } catch (error) {
4041
+ errorHandler(error, methods.setErrors);
4042
+ } finally {
4043
+ setLoading(LOADING_NAME, true);
4044
+ }
4045
+ });
4046
+ }
4047
+ function onRequestWrapper(fn, { name }) {
4048
+ return (...params) => __async(this, null, function* () {
4049
+ if (name.includes("get:"))
4050
+ sourceRef.current = axios.CancelToken.source();
4051
+ const LOADING_NAME = name;
4052
+ setLoading(LOADING_NAME);
4053
+ api.interceptors.request.use(
4054
+ (config) => {
4055
+ var _a;
4056
+ if (!config.cancelToken && sourceRef.current && config.method === "get") {
4057
+ config.cancelToken = (_a = sourceRef.current) == null ? void 0 : _a.token;
4058
+ }
4059
+ return config;
4060
+ },
4061
+ (error) => {
4062
+ console.log("[middleware]");
4063
+ return Promise.reject(error);
4064
+ }
4065
+ );
4066
+ try {
4067
+ const response = yield fn(...params);
4068
+ return response;
4069
+ } catch (error) {
4070
+ errorHandler(error);
4071
+ } finally {
4072
+ setLoading(LOADING_NAME, true);
4073
+ }
4074
+ });
4075
+ }
4076
+ function errorHandler(error, callback) {
4077
+ if ((error == null ? void 0 : error.message) === "cancel.navigation")
4078
+ return;
4079
+ if (callback) {
4080
+ if (error.response.data.code === "invalid.body") {
4081
+ const errors = error.response.data.details.issues;
4082
+ const currentErrors = errors.reduce((acc, item) => {
4083
+ acc[item.path.join(".")] = item.message;
4084
+ return acc;
4085
+ }, {});
4086
+ callback(currentErrors);
4087
+ }
4088
+ }
4089
+ createAlert(formatErrorMessage(error), "error");
4090
+ }
4091
+ useEffect5(() => {
4092
+ return () => {
4093
+ var _a;
4094
+ (_a = sourceRef.current) == null ? void 0 : _a.cancel("cancel.navigation");
4095
+ };
4096
+ }, []);
4097
+ return __spreadProps(__spreadValues(__spreadValues({}, alertProps), loadingProps), {
4098
+ onSubmitWrapper,
4099
+ onRequestWrapper
4100
+ });
4101
+ }
4102
+
4103
+ // src/hooks/useGrid.ts
4104
+ import { useMemo as useMemo3, useState as useState6 } from "react";
4105
+
4106
+ // src/hooks/useFilter.ts
4107
+ import moment2 from "moment";
4108
+ import { useState as useState5 } from "react";
3820
4109
 
3821
4110
  // src/components/utils/getObjectValue.ts
3822
4111
  function getObjectValue(obj) {
3823
4112
  return (prop) => prop.split(".").reduce((o, k) => o[k], obj);
3824
4113
  }
3825
4114
 
3826
- // src/components/hooks/useFilter.ts
3827
- import moment2 from "moment";
3828
- import { useState as useState3 } from "react";
4115
+ // src/hooks/useFilter.ts
3829
4116
  function useFilter() {
3830
- const [selectedFilters, setSelectedFilters] = useState3([]);
4117
+ const [selectedFilters, setSelectedFilters] = useState5([]);
3831
4118
  const filterBy = (newFilter) => {
3832
4119
  const propToCompare = newFilter.id ? "id" : "prop";
3833
4120
  function removeRepeatedFilters(filter) {
@@ -3897,18 +4184,18 @@ function createFilter(filters) {
3897
4184
  };
3898
4185
  }
3899
4186
 
3900
- // src/components/hooks/useGrid.ts
4187
+ // src/hooks/useGrid.ts
3901
4188
  function useGrid({
3902
4189
  columns,
3903
4190
  filters = [],
3904
4191
  search,
3905
4192
  rowsPerPageOptions = [30, 60, 100]
3906
4193
  }) {
3907
- const [defaultData, setDefaultData] = useState4([]);
3908
- const [sortedBy, setSortedBy] = useState4("");
3909
- const [sortedDirection, setSortedDirection] = useState4("desc");
3910
- const [currentPage, setCurrentPage] = useState4(0);
3911
- const [rowsPerPage, setRowsPerPage] = useState4(rowsPerPageOptions[0]);
4194
+ const [defaultData, setDefaultData] = useState6([]);
4195
+ const [sortedBy, setSortedBy] = useState6("");
4196
+ const [sortedDirection, setSortedDirection] = useState6("desc");
4197
+ const [currentPage, setCurrentPage] = useState6(0);
4198
+ const [rowsPerPage, setRowsPerPage] = useState6(rowsPerPageOptions[0]);
3912
4199
  const toggleSortedDirection = () => {
3913
4200
  if (sortedDirection === "desc")
3914
4201
  setSortedDirection("asc");
@@ -4030,108 +4317,19 @@ function createSearch(options) {
4030
4317
  };
4031
4318
  }
4032
4319
 
4033
- // src/components/hooks/useEvent.ts
4034
- import { useEffect as useEffect5 } from "react";
4320
+ // src/hooks/useEvent.ts
4321
+ import { useEffect as useEffect6 } from "react";
4035
4322
  function useEvent(event, handler, passive = false) {
4036
- useEffect5(() => {
4323
+ useEffect6(() => {
4037
4324
  window.addEventListener(event, handler, passive);
4038
4325
  return function cleanup() {
4039
4326
  window.removeEventListener(event, handler);
4040
4327
  };
4041
4328
  });
4042
4329
  }
4043
-
4044
- // src/components/hooks/useLoading.ts
4045
- import { useState as useState5 } from "react";
4046
- function useLoading() {
4047
- const [state, setState] = useState5([]);
4048
- const isLoading = (prop) => state.includes(prop);
4049
- const setLoading = (prop, remove) => {
4050
- if (remove)
4051
- setState((prevState) => prevState.filter((state2) => state2 !== prop));
4052
- else
4053
- setState((prevState) => [...prevState, prop]);
4054
- };
4055
- return { isLoading, setLoading };
4056
- }
4057
-
4058
- // src/components/Modal/index.tsx
4059
- import React25 from "react";
4060
- import {
4061
- Box as Box4,
4062
- Modal as MuiModal
4063
- } from "@mui/material";
4064
- var Modal = (_a) => {
4065
- var _b = _a, { open, onClose } = _b, rest = __objRest(_b, ["open", "onClose"]);
4066
- return /* @__PURE__ */ React25.createElement(MuiModal, __spreadValues({ open, onClose, disableEnforceFocus: true }, rest), /* @__PURE__ */ React25.createElement(
4067
- Box4,
4068
- {
4069
- sx: {
4070
- outline: "none",
4071
- backgroundColor: "white",
4072
- position: "absolute",
4073
- top: "50%",
4074
- left: "50%",
4075
- transform: "translate(-50%, -50%)",
4076
- borderRadius: 1
4077
- }
4078
- },
4079
- /* @__PURE__ */ React25.createElement(React25.Fragment, null, rest.children)
4080
- ));
4081
- };
4082
-
4083
- // src/components/utils/GetInputLabel.ts
4084
- function GetInputLabel(columns) {
4085
- return (columnName) => {
4086
- const column = columns.find((column2) => column2.name === columnName);
4087
- return { label: column.label, name: column.name };
4088
- };
4089
- }
4090
-
4091
- // src/components/Dialog/index.tsx
4092
- import React26 from "react";
4093
- import {
4094
- Box as Box5,
4095
- Dialog as DefaultDialog,
4096
- DialogContentText,
4097
- DialogActions,
4098
- DialogTitle,
4099
- Button as Button3,
4100
- CircularProgress as CircularProgress2
4101
- } from "@mui/material";
4102
- var Dialog = (_a) => {
4103
- var _b = _a, {
4104
- open,
4105
- title,
4106
- loading,
4107
- body,
4108
- options
4109
- } = _b, rest = __objRest(_b, [
4110
- "open",
4111
- "title",
4112
- "loading",
4113
- "body",
4114
- "options"
4115
- ]);
4116
- return /* @__PURE__ */ React26.createElement(DefaultDialog, __spreadValues({ open }, rest), /* @__PURE__ */ React26.createElement(Box5, { sx: { p: 2 } }, /* @__PURE__ */ React26.createElement(DialogTitle, { sx: { fontWeight: "bold" } }, title), /* @__PURE__ */ React26.createElement(DialogContentText, { sx: { px: "10px", textAlign: "center", mb: 2 } }, body), /* @__PURE__ */ React26.createElement(DialogActions, null, options.map((option, index) => {
4117
- return /* @__PURE__ */ React26.createElement(
4118
- Button3,
4119
- {
4120
- key: index,
4121
- onClick: () => option.cb(option.label),
4122
- variant: option.focus ? "contained" : "text",
4123
- sx: {
4124
- fontWeight: option.focus ? "bold" : "normal",
4125
- color: option.focus ? "#fff" : "primary.main"
4126
- },
4127
- disableElevation: true,
4128
- disabled: loading
4129
- },
4130
- loading && option.focus ? /* @__PURE__ */ React26.createElement(CircularProgress2, { size: 25, color: "inherit" }) : /* @__PURE__ */ React26.createElement(React26.Fragment, null, option.label)
4131
- );
4132
- }))));
4133
- };
4134
4330
  export {
4331
+ AlertContext,
4332
+ ApiHelper,
4135
4333
  Autocomplete2 as Autocomplete,
4136
4334
  BaseGrid,
4137
4335
  Checkbox,
@@ -4139,6 +4337,7 @@ export {
4139
4337
  EditableTableCell,
4140
4338
  GetInputLabel,
4141
4339
  Grid_default as Grid,
4340
+ HttpError,
4142
4341
  Input,
4143
4342
  InputMask2 as InputMask,
4144
4343
  LargeButton,
@@ -4152,8 +4351,10 @@ export {
4152
4351
  createFilter,
4153
4352
  filterData,
4154
4353
  getTabProps,
4354
+ useAlert,
4155
4355
  useEvent,
4156
4356
  useFilter,
4357
+ useFormHelper,
4157
4358
  useGrid,
4158
4359
  useLoading
4159
4360
  };