diff --git a/src/components/Header/AfterLogin/ActionNavigate.tsx b/src/components/Header/AfterLogin/ActionNavigate.tsx index b71c94e..0a3f4cd 100644 --- a/src/components/Header/AfterLogin/ActionNavigate.tsx +++ b/src/components/Header/AfterLogin/ActionNavigate.tsx @@ -5,8 +5,9 @@ import ActionItem from "./ActionItem"; type InputProps = { text: string; to: string; + onClick: () => void; }; -const ActionNavigate = ({ text, to }: InputProps) => { +const ActionNavigate = ({ text, to, onClick }: InputProps) => { const navigate = useNavigate(); const { handleCloseNavMenu } = useHeaderCtx(); @@ -15,6 +16,7 @@ const ActionNavigate = ({ text, to }: InputProps) => { onClick={() => { navigate(to); handleCloseNavMenu(); + onClick(); }} > {text} diff --git a/src/components/Header/AfterLogin/Subscribe.tsx b/src/components/Header/AfterLogin/Subscribe.tsx index 518602d..d8dea4f 100644 --- a/src/components/Header/AfterLogin/Subscribe.tsx +++ b/src/components/Header/AfterLogin/Subscribe.tsx @@ -1,4 +1,5 @@ import { UserInfo } from "apis/api"; +import { useUnsubscribeToStripe } from "components/Stripe/useUnsubscribeToStripe"; import { pricing } from "const/urls"; import ActionItem from "./ActionItem"; import ActionNavigate from "./ActionNavigate"; @@ -8,10 +9,16 @@ type InputProps = { }; const Subscribe = ({ handleClose, userInfo }: InputProps) => { + const { openModal, renderUnsubscribe } = useUnsubscribeToStripe({ + onCloseModal: handleClose + }); return userInfo.isPremium ? ( - Unsubscribe + <> + Unsubscribe + {renderUnsubscribe} + ) : ( - + ); }; diff --git a/src/components/Header/AfterLogin/useActions.tsx b/src/components/Header/AfterLogin/useActions.tsx index 89ff7cc..e34790a 100644 --- a/src/components/Header/AfterLogin/useActions.tsx +++ b/src/components/Header/AfterLogin/useActions.tsx @@ -41,12 +41,14 @@ const useActions = () => { key="NewPlugin" text="New Plugins" to={`/${pluginBuilder("new")}`} + onClick={handleClose} />,
, ,
, { + const navigate = useNavigate(); + return ( + + ); +}; + +export default GoToContact; diff --git a/src/components/Stripe/GoToPlugins.tsx b/src/components/Stripe/GoToPlugins.tsx index a4e8b8e..56789b7 100644 --- a/src/components/Stripe/GoToPlugins.tsx +++ b/src/components/Stripe/GoToPlugins.tsx @@ -1,15 +1,10 @@ -import { Button } from "@mui/material"; +import Button from "components/Button"; import { useUserInfoCtx } from "components/UserInfo/UserInfo"; const GoToPlugins = () => { const { handleLogin } = useUserInfoCtx(); return ( - ); diff --git a/src/components/Stripe/GoToPricing.tsx b/src/components/Stripe/GoToPricing.tsx index b73bd46..3e68502 100644 --- a/src/components/Stripe/GoToPricing.tsx +++ b/src/components/Stripe/GoToPricing.tsx @@ -1,4 +1,4 @@ -import { Button } from "@mui/material"; +import Button from "components/Button"; import { pricing } from "const/urls"; import { useNavigate } from "react-router-dom"; @@ -7,7 +7,6 @@ const GoToPricing = () => { return ( + ) : ( + + {isError ? "RETRY?" : "UNSUBSCRIBE"} + + )} + + + + } + open={isLogged && !!userInfo.isPremium && showModal} + handleClose={closeModal} + titleText={ + isUnsubscribed + ? "Successful unsubscribed." + : "Do you want to unsubscribe?" + } + contentText={ + <> + {isUnsubscribed ? ( + + Please send as a  + + feedback + +   + + ) : ( + + If you unsubscribe you lost all features. Click  + + HERE + +  to know what you lost + + )} + {isError && ( + + Something went wrong, please retry. + + )} + + } + /> + ); +}; diff --git a/src/components/Stripe/useUnsubscribeToStripe.tsx b/src/components/Stripe/useUnsubscribeToStripe.tsx new file mode 100644 index 0000000..52ca724 --- /dev/null +++ b/src/components/Stripe/useUnsubscribeToStripe.tsx @@ -0,0 +1,70 @@ +import { PaymentsApi } from "apis/api"; +import { useUserInfoCtx } from "components/UserInfo/UserInfo"; +import { debugConsole } from "components/util"; +import { useCallback, useState } from "react"; +import { ModalUnsubscribe } from "./ModalUnsubscribe"; + +type InputProps = { + onCloseModal: () => void; +}; +export const useUnsubscribeToStripe = ({ onCloseModal }: InputProps) => { + const { getUserData } = useUserInfoCtx(); + + const [isAskingConfirm, setIsAskingConfirm] = useState(false); + const [{ isLoading, isError, isUnsubscribed }, setHttpState] = useState({ + isLoading: false, + isError: false, + isUnsubscribed: false + }); + + const openModal = useCallback(() => setIsAskingConfirm(true), []); + const closeModal = useCallback(() => { + setIsAskingConfirm(false); + onCloseModal(); + }, [onCloseModal]); + + const unsubscribe = useCallback(async () => { + setHttpState({ isLoading: true, isError: false, isUnsubscribed: false }); + try { + const paymentsApi = new PaymentsApi(); + await paymentsApi + .apiPaymentsUnsubscribePost() + .then(async (res) => { + await getUserData(); + // unsubscribed + setHttpState({ + isLoading: false, + isError: false, + isUnsubscribed: true + }); + }) + .catch(() => + setHttpState({ + isLoading: false, + isError: false, + isUnsubscribed: false + }) + ); + } catch (err) { + setHttpState({ + isLoading: false, + isError: true, + isUnsubscribed: false + }); + debugConsole(err); + } + }, []); + + const renderUnsubscribe = ( + + ); + + return { openModal, closeModal, renderUnsubscribe }; +}; diff --git a/src/components/Typography.tsx b/src/components/Typography.tsx index 49163e6..1743e11 100644 --- a/src/components/Typography.tsx +++ b/src/components/Typography.tsx @@ -1,21 +1,32 @@ import { Typography as TypographyCore } from "@mui/material"; +import { memo } from "react"; interface InputProps { children?: React.ReactNode; variant?: "t1" | "t2" | "t3" | "t4" | "t5" | "t6"; className?: string; + component?: "p" | "span" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6"; } const Typography = ({ children = "change me", variant = "t4", - className + className, + component }: InputProps) => { - return ( + return component ? ( + + {children} + + ) : ( {children} ); }; -export default Typography; +export default memo(Typography); diff --git a/src/components/UserInfo/UserInfo.tsx b/src/components/UserInfo/UserInfo.tsx index bf3a598..f907087 100644 --- a/src/components/UserInfo/UserInfo.tsx +++ b/src/components/UserInfo/UserInfo.tsx @@ -9,6 +9,7 @@ type ctxType = { manualLogin: ReturnType["manualLogin"]; handleLogin: ReturnType["handleLogin"]; handleLogout: ReturnType["handleLogout"]; + getUserData: ReturnType["getUserData"]; }; const Ctx = createContext({ @@ -22,7 +23,10 @@ const Ctx = createContext({ handleLogin: async () => { throw new Error("Outside context"); }, - handleLogout: async () => {} + handleLogout: async () => {}, + getUserData: async () => { + throw new Error("Outside context"); + } }); type InputProps = { @@ -35,7 +39,8 @@ const UserInfo = ({ children }: InputProps) => { manualLogin, isLoadingUser, userInfo, - user + user, + getUserData } = useHandlerAuth(); const providerValue: ctxType = useMemo( @@ -46,9 +51,18 @@ const UserInfo = ({ children }: InputProps) => { handleLogin, handleLogout, userInfo, - manualLogin + manualLogin, + getUserData }), - [handleLogin, handleLogout, manualLogin, user, userInfo, isLoadingUser] + [ + handleLogin, + handleLogout, + manualLogin, + user, + userInfo, + isLoadingUser, + getUserData + ] ); const memoChildren = useMemo(() => children, [children]); // To avoid unless rerender diff --git a/src/components/UserInfo/useHandlerAuth.tsx b/src/components/UserInfo/useHandlerAuth.tsx index 306e566..e3cb68c 100644 --- a/src/components/UserInfo/useHandlerAuth.tsx +++ b/src/components/UserInfo/useHandlerAuth.tsx @@ -9,24 +9,33 @@ import { debugConsole } from "../util"; const useHandlerAuth = () => { const [isLoadingUser, setIsLoadingUser] = useState(true); const [user, setUser] = useState(null); - const [userInfo, setUserInfo] = useState(); + const [userInfo, setUserInfo] = useState({}); const userInfoRef = useRef(userInfo); userInfoRef.current = userInfo; const navigate = useNavigate(); + const getUserData = useCallback(async () => { + try { + const userApi = new UserApi(); + const response = await userApi.apiUserGet(); + setUserInfo(response.data); + userInfoRef.current = response.data; + return response.data; + } catch (error) { + debugConsole("Error fetching users:", error); + } + }, []); + const afterLogin = useCallback( async (noRedirect?: boolean | ((user: UserInfo) => boolean)) => { try { if (Object.keys(userInfoRef.current || {}).length <= 0) { - const userApi = new UserApi(); - const response = await userApi.apiUserGet(); - setUserInfo(response.data); - userInfoRef.current = response.data; + await getUserData(); } if (typeof noRedirect === "function") { - !noRedirect(userInfoRef.current!) && navigate(`/${yourPlugins}`); + !noRedirect(userInfoRef.current) && navigate(`/${yourPlugins}`); } else !noRedirect && navigate(`/${yourPlugins}`); } catch (error) { debugConsole("Error fetching users:", error); @@ -67,6 +76,7 @@ const useHandlerAuth = () => { manualLogin, handleLogout: logout, isLoadingUser, + getUserData, user, userInfo: userInfo || {} };