Skip to content

Commit

Permalink
Update providers
Browse files Browse the repository at this point in the history
  • Loading branch information
ruishanteo committed Oct 7, 2023
1 parent 17a7453 commit cda6b43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 75 deletions.
27 changes: 6 additions & 21 deletions providers/AxiosProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,28 @@ 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;

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:
Expand Down
54 changes: 0 additions & 54 deletions providers/NotificationProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
NotifierWrapper,
} from "react-native-notifier";
import * as Device from "expo-device";
import { LoadingIcon } from "../components/LoadingIcon";
import {
getScheduledNotifications,
setScheduledNotifications,
Expand All @@ -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,
Expand All @@ -91,17 +42,12 @@ function NotificationProvider({ children }) {
});
}

if (loading) return <LoadingIcon fullSize={true} />;

return (
<GestureHandlerRootView style={{ flex: 1 }}>
<NotifierWrapper queueMode="reset">
<Provider
value={{
schedulePushNotification,
pushNotification,
showNotification,
scheduleEventNotification,
}}
>
{children}
Expand Down

0 comments on commit cda6b43

Please sign in to comment.