From cda6b43c3c757d655557dea81e3cc8759b72a799 Mon Sep 17 00:00:00 2001 From: Rui Shan Date: Sat, 7 Oct 2023 10:10:51 +0800 Subject: [PATCH] Update providers --- providers/AxiosProvider.js | 27 ++++------------ providers/NotificationProvider.js | 54 ------------------------------- 2 files changed, 6 insertions(+), 75 deletions(-) diff --git a/providers/AxiosProvider.js b/providers/AxiosProvider.js index e41b435..75f15ca 100644 --- a/providers/AxiosProvider.js +++ b/providers/AxiosProvider.js @@ -2,8 +2,7 @@ import React, { createContext, useContext } from "react"; import axios from "axios"; import { NotificationContext } from "./NotificationProvider"; -import { API_URL, API_KEY, FAKE_API } from "../config/config"; -import { fakeData } from "../config/fakeData"; +import { SPOON_API_URL, API_KEY, FAKE_API } from "../config/config"; const AxiosContext = createContext(); const { Provider } = AxiosContext; @@ -11,34 +10,20 @@ const { Provider } = AxiosContext; function AxiosProvider({ children }) { const { showNotification } = useContext(NotificationContext); - const publicAxios = axios.create({ - baseURL: `${API_URL}`, - }); + const publicAxios = axios.create(); publicAxios.interceptors.request.use( (config) => { - if (FAKE_API) { - throw { isLocal: true, data: fakeData }; - } else { - config.headers.Accept = "application/json"; - config.headers["x-api-key"] = API_KEY; - } + config.headers.Accept = "application/json"; + config.headers["x-api-key"] = API_KEY; return config; }, - (error) => { - return error?.isLocal ? Promise.resolve(error) : Promise.reject(error); - } + (error) => Promise.reject(error) ); publicAxios.interceptors.response.use( - (response) => { - return response.data; - }, + (response) => response.data, (error) => { - if (error?.isLocal) { - return Promise.resolve(error.data); - } - showNotification({ title: "Request failed", description: diff --git a/providers/NotificationProvider.js b/providers/NotificationProvider.js index 3e1bc70..a0fc948 100644 --- a/providers/NotificationProvider.js +++ b/providers/NotificationProvider.js @@ -9,7 +9,6 @@ import { NotifierWrapper, } from "react-native-notifier"; import * as Device from "expo-device"; -import { LoadingIcon } from "../components/LoadingIcon"; import { getScheduledNotifications, setScheduledNotifications, @@ -27,54 +26,6 @@ Notifications.setNotificationHandler({ }); function NotificationProvider({ children }) { - const [loading, setLoading] = useState(true); - useEffect(() => { - async function registerForPushNotificationsAsync() { - if (Platform.OS === "android") { - await Notifications.setNotificationChannelAsync("default", { - name: "default", - importance: Notifications.AndroidImportance.MAX, - vibrationPattern: [0, 250, 250, 250], - lightColor: "#FF231F7C", - }); - } - if (Device.isDevice) await Notifications.getPermissionsAsync(); - setLoading(false); - } - registerForPushNotificationsAsync(); - }, []); - - async function scheduleEventNotification(event) { - const scheduledNotifications = await getScheduledNotifications(); - if (!scheduledNotifications[event.id]) { - const reminderDate = new Date(event.date); - reminderDate.setDate(reminderDate.getDate() - 1); - await Notifications.scheduleNotificationAsync({ - content: { - title: "Event Reminder", - body: `Reminder: ${event.name} is due tomorrow!`, - }, - trigger: { date: reminderDate }, - }); - scheduledNotifications[event.id] = true; - await setScheduledNotifications(scheduledNotifications); - } - } - - async function schedulePushNotification(content, delay = 2) { - await Notifications.scheduleNotificationAsync({ - content, - trigger: { seconds: delay }, - }); - } - - async function pushNotification(content) { - await Notifications.scheduleNotificationAsync({ - content, - trigger: { seconds: 0 }, - }); - } - function showNotification(content) { Notifier.showNotification({ duration: 3000, @@ -91,17 +42,12 @@ function NotificationProvider({ children }) { }); } - if (loading) return ; - return ( {children}