Skip to content

Commit

Permalink
Return toastId when calling toastr
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmathew900 committed May 7, 2024
1 parent 7837bcc commit 9122428
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 29 deletions.
35 changes: 10 additions & 25 deletions src/components/Toastr/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ const withUniqueCheck =
customConfig = {},
} = parseToastrConfig(toastrConfig);

let toastId = null;

if (toastrList.add({ type, message, buttonLabel })) {
const config = {
...TOAST_CONFIG,
icon: TOAST_ICON[type],
onClose: () => toastrList.remove({ type, message, buttonLabel }),
...customConfig,
};
toastFunc({ message, buttonLabel, onClick, config });

toastId = toastFunc({ message, buttonLabel, onClick, config });
}

return toastId;
};

const showSuccessToastr = withUniqueCheck(
"success",
({ message, buttonLabel, onClick, config }) =>
toast.success(
<Toast
buttonLabel={buttonLabel}
message={message}
type="success"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="success" />,
config
)
);
Expand All @@ -102,12 +102,7 @@ const showInfoToastr = withUniqueCheck(
"info",
({ message, buttonLabel, onClick, config }) =>
toast.info(
<Toast
buttonLabel={buttonLabel}
message={message}
type="info"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="info" />,
config
)
);
Expand All @@ -116,12 +111,7 @@ const showWarningToastr = withUniqueCheck(
"warning",
({ message, buttonLabel, onClick, config }) =>
toast.warning(
<Toast
buttonLabel={buttonLabel}
message={message}
type="warning"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="warning" />,
config
)
);
Expand Down Expand Up @@ -194,12 +184,7 @@ const withParsedErrorMsg =
const showErrorToastr = withParsedErrorMsg(
withUniqueCheck("error", ({ message, buttonLabel, onClick, config }) =>
toast.error(
<Toast
buttonLabel={buttonLabel}
message={message}
type="error"
onClick={onClick}
/>,
<Toast {...{ buttonLabel, message, onClick }} type="error" />,
config
)
)
Expand Down
11 changes: 8 additions & 3 deletions tests/Toastr.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const renderToastrButton = (
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand All @@ -40,7 +40,7 @@ const renderCustomConfigToastrButton = type => {
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand All @@ -52,7 +52,7 @@ const renderCustomMessageToastrButton = (type, message) => {
render(
<>
<ToastContainer />
<Button label={`${type} Toastr`} onClick={onClick} />
<Button {...{ onClick }} label={`${type} Toastr`} />
</>
);

Expand Down Expand Up @@ -374,4 +374,9 @@ describe("Toastr", () => {
const expectedMessage = "This is a Error toastr.";
testToastrErrorMessages(errorResponse, expectedMessage);
});

it("should return toastId when toastr is called", () => {
const toastId = Toastr.success("This is a success toastr.");
expect(toastId).toBeDefined();
});
});
2 changes: 1 addition & 1 deletion types/Toastr.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const Toastr: {
message: React.ReactNode | Error,
buttonLabel?: React.ReactNode,
onClick?: () => void
) => void;
) => string | null;
warning: ToastrFunction;
};
export default Toastr;

0 comments on commit 9122428

Please sign in to comment.