Skip to content

Commit

Permalink
refactor: moved toast message clear logic from toast component to toa…
Browse files Browse the repository at this point in the history
…st context provider
  • Loading branch information
sachin-into committed Feb 22, 2024
1 parent 3508852 commit b52efdd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
23 changes: 4 additions & 19 deletions packages/components/MiniToast.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from "react";
import React from "react";
import { View, useWindowDimensions } from "react-native";

import { Spinner } from "./Spinner";
Expand Down Expand Up @@ -30,7 +30,7 @@ type ToastVariantType = "solid" | "outline";

export type MiniToastProps = {
status?: ToastStatusType;
message?: string;
message: string | null;
variant?: ToastVariantType;
duration?: number;
topOffSet?: number;
Expand Down Expand Up @@ -75,23 +75,8 @@ export const MiniToast = ({
showAlways = false,
}: MiniToastProps) => {
const { width: windowWidth } = useWindowDimensions();
const [toastMessage, setToastMessage] = useState("");

useEffect(() => {
if (message) {
setToastMessage(message);
}
//always show toast when showAlways flag is enabled or status is loading
if (!showAlways && status !== "loading") {
const timeoutID = setTimeout(() => {
setToastMessage("");
}, duration || 5000);

return () => clearTimeout(timeoutID);
}
}, [message, duration, status, showAlways]);

if (!toastMessage) {
if (!message) {
return null;
}

Expand Down Expand Up @@ -134,7 +119,7 @@ export const MiniToast = ({
},
]}
>
{toastMessage}
{message}
</BrandText>
</View>
);
Expand Down
28 changes: 22 additions & 6 deletions packages/context/ToastProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
import { ReactNode, createContext, useContext, useState } from "react";
import {
ReactNode,
createContext,
useContext,
useEffect,
useState,
} from "react";

import { MiniToastProps, MiniToast } from "@/components/MiniToast";

interface InitailValues {
interface DefaultValues {
toast: MiniToastProps;
setToast: (data: MiniToastProps) => void;
}

const initialValues: InitailValues = {
toast: {},
const defaultValues: DefaultValues = {
toast: { message: "" },
setToast: () => {},
};

const ToastContext = createContext(initialValues);
const ToastContext = createContext(defaultValues);

export const useToast = () => useContext(ToastContext);

export const ToastContextProvider: React.FC<{ children: ReactNode }> = ({
children,
}) => {
const [toast, setToast] = useState<MiniToastProps>(initialValues.toast);
const [toast, setToast] = useState<MiniToastProps>(defaultValues.toast);

useEffect(() => {
//always show toast when showAlways flag is enabled or status is loading
if (!toast.showAlways && toast.status !== "loading") {
const timeoutID = setTimeout(() => {
setToast(defaultValues.toast);
}, toast.duration || 5000);

return () => clearTimeout(timeoutID);
}
}, [toast]);
return (
<ToastContext.Provider value={{ setToast, toast }}>
<MiniToast {...toast} />
Expand Down

0 comments on commit b52efdd

Please sign in to comment.