Untitled
unknown
javascript
3 years ago
3.8 kB
6
Indexable
import { setLoadingDeepLinkFalse, setUrlDeepLink, } from "../../reduxs/actions/deepLinkAsync"; import { showToast } from "hooks/useShowToast"; import { Linking } from "react-native"; import { getAxiosWithToken } from "ultis/axiosHelper"; import Endpoint from "ultis/axiosHelper/endPoint"; import { push } from "ultis/navigationRef"; import { t } from "i18n-js"; import { linkGuideUse } from "config/const"; import { NavigationProp } from "@react-navigation/native"; import { RootStackParamList } from "hooks/useRootStack"; const listenDeepLink = () => { Linking.addEventListener("url", (e) => { if (e && !!e.url) { handleDeepLink(e.url); } }); }; enum typeLink { category = "category", product = "product", topic = "topic", brand = "brand", manufacturer = "manufacturer", userprofile = "userprofile", newuserguide = "newuserguide", guide = "guide", commissionmanager = "commissionmanager", } const regexp = new RegExp( /((category|product|topic|brand|manufacturer)(\/|%2F)\d+|userprofile|newuserguide|guide|commissionmanager)/g ); const getLinks = (url: string = "") => { const matches = url.match(regexp); if (!matches || matches.length === 0) { return null; } const route = matches[matches.length - 1]; let x: string[] = []; if (route.includes("/")) { x = route.split("/"); } if (route.includes("%2F")) { x = route.split("%2F"); } return { id: x[x.length - 1], type: x[0], }; }; const getInitialURL = (callback?: (route: any) => void) => { // setLoadingDeepLinkTrue(); return new Promise((resolve, reject) => { Linking.getInitialURL() .then((res: string | null | undefined) => { if (res) { setUrlDeepLink(res); } setLoadingDeepLinkFalse(); resolve(true); }) .catch((e) => { console.log("e handleDeepLink", e); setLoadingDeepLinkFalse(); resolve(true); }); }); }; const handleDeepLink = ( res: string, navigation?: NavigationProp<RootStackParamList>, callback?: (route: any) => void ) => { if (res) { const route = getLinks(res); if (!route) { return; } const { id, type } = route; const _push = navigation ? navigation.navigate : push; if (callback) { callback(route); } else { switch (type) { case typeLink.product: _push("DetailProduct", { id: id, }); break; case typeLink.category: _push("CollectionProduct", { endpoint: Endpoint.product_category(`${id}`), }); break; case typeLink.brand: case typeLink.manufacturer: _push("CollectionProduct", { endpoint: Endpoint.get_product_of_brand(`${id}`), }); break; case typeLink.topic: getAxiosWithToken(Endpoint.get_webview(id)).then((res) => { if (res.status === 200) { _push("WebViewTopic", { title: res.data.Data.Title, body: res.data.Data.Body, }); } else { showToast(res.data.ErrorList[0]); } }); break; case typeLink.userprofile: _push("File"); break; case typeLink.newuserguide: _push("NewbieGuide"); break; case typeLink.guide: _push("WebViewTopic", { title: t("guide_use"), uri: linkGuideUse, }); break; case typeLink.commissionmanager: _push("ManageCommission"); break; default: break; } } } }; export { getInitialURL, getLinks, listenDeepLink, handleDeepLink };
Editor is loading...