-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added expo-notifications and react-query
- Loading branch information
Showing
23 changed files
with
300 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import axios from 'axios' | ||
|
||
const BASE_URL = 'https://icanhazdadjoke.com/' | ||
|
||
interface JokeResponse { | ||
id: string | ||
joke: string | ||
status: number | ||
} | ||
|
||
export const fetchRandomJoke = async (): Promise<JokeResponse> => { | ||
const res = await axios.get(BASE_URL, { | ||
headers: { | ||
Accept: 'application/json', | ||
}, | ||
}) | ||
return res.data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { fetchRandomJoke } from '@app/api/jokes' | ||
|
||
const useJokes = () => { | ||
return useQuery({ | ||
queryKey: ['joke'], | ||
queryFn: fetchRandomJoke, | ||
select: data => data.joke, | ||
refetchOnReconnect: 'always', | ||
experimental_prefetchInRender: true, | ||
}) | ||
} | ||
|
||
export default useJokes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import * as Notifications from 'expo-notifications' | ||
import { useEffect, useRef, useState } from 'react' | ||
import { Alert } from 'react-native' | ||
import { EAS_PROJECT_ID } from '@app/env' | ||
|
||
Notifications.setNotificationHandler({ | ||
handleNotification: async () => ({ | ||
shouldShowAlert: true, | ||
shouldPlaySound: true, | ||
shouldSetBadge: false, | ||
}), | ||
}) | ||
|
||
const registerForPushNotificationsAsync = async (): Promise<string | null> => { | ||
try { | ||
const { status: existingStatus } = await Notifications.getPermissionsAsync() | ||
let finalStatus = existingStatus | ||
|
||
if (existingStatus !== 'granted') { | ||
const { status } = await Notifications.requestPermissionsAsync() | ||
finalStatus = status | ||
} | ||
|
||
if (finalStatus !== 'granted') { | ||
Alert.alert('Failed to get push token for push notification!') | ||
return null | ||
} | ||
|
||
const projectId = EAS_PROJECT_ID | ||
if (!projectId) { | ||
throw new Error('Project ID not found') | ||
} | ||
|
||
const token = (await Notifications.getExpoPushTokenAsync({ projectId })).data | ||
return token | ||
} catch (e) { | ||
console.error('Error getting push token:', e) | ||
return null | ||
} | ||
} | ||
|
||
const useNotifications = () => { | ||
const [expoPushToken, setExpoPushToken] = useState<string | null>(null) | ||
const [notification, setNotification] = useState<Notifications.Notification | null>(null) | ||
const notificationListener = useRef<Notifications.Subscription | null>(null) | ||
const responseListener = useRef<Notifications.Subscription | null>(null) | ||
|
||
const registerPushToken = async () => { | ||
const token = await registerForPushNotificationsAsync() | ||
if (token) { | ||
setExpoPushToken(token) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
registerPushToken() | ||
|
||
notificationListener.current = Notifications.addNotificationReceivedListener(n => setNotification(n)) | ||
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { | ||
console.log('User interacted with notification:', response) | ||
}) | ||
|
||
return () => { | ||
if (notificationListener.current) { | ||
Notifications.removeNotificationSubscription(notificationListener.current) | ||
} | ||
if (responseListener.current) { | ||
Notifications.removeNotificationSubscription(responseListener.current) | ||
} | ||
} | ||
}, []) | ||
|
||
return { | ||
notification, | ||
token: expoPushToken, | ||
} | ||
} | ||
|
||
export default useNotifications |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,7 @@ | ||
import { create } from 'zustand' | ||
import { withSlices } from 'zustand-slices' | ||
import { persist } from 'zustand/middleware' | ||
import authSlice from './authSlice' | ||
import persistConfig from './persistConfig' | ||
import toastSlice from './toastSlice' | ||
import { authSlice, connectionSlice, toastSlice } from './slices' | ||
|
||
export const useStore = create(persist(withSlices(authSlice, toastSlice), persistConfig)) | ||
export const useStore = create(persist(withSlices(authSlice, toastSlice, connectionSlice), persistConfig)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,22 @@ | ||
import { createJSONStorage, PersistOptions, StateStorage } from 'zustand/middleware' | ||
import { zustandStorage } from '@app/storage' | ||
import { Store } from './types' | ||
import { RootStore } from './slices' | ||
|
||
const persistStorage: StateStorage = { | ||
setItem: (name, value) => zustandStorage.set(name, value), | ||
getItem: name => zustandStorage.getString(name) || null, | ||
removeItem: name => zustandStorage.delete(name), | ||
} | ||
|
||
const persistConfig: PersistOptions<Store> = { | ||
const persistConfig: PersistOptions<RootStore> = { | ||
name: 'rootStore', | ||
storage: createJSONStorage(() => persistStorage), | ||
partialize: state => | ||
({ | ||
auth: { | ||
user: state.auth.user, | ||
}, | ||
}) as Store, | ||
}) as RootStore, | ||
} | ||
|
||
export default persistConfig |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createSliceWithImmer } from '@app/utils/store/createSliceWithImmer' | ||
|
||
export interface ConnectionSliceState { | ||
hasInternetConnection: boolean | ||
} | ||
|
||
export interface ConnectionSliceActions { | ||
setConnectionStatus: (status: boolean) => void | ||
} | ||
|
||
const connectionSlice = createSliceWithImmer({ | ||
name: 'connection', | ||
value: { | ||
hasInternetConnection: true, | ||
} as ConnectionSliceState, | ||
actions: { | ||
setConnectionStatus: (status: boolean) => state => { | ||
state.hasInternetConnection = status | ||
}, | ||
}, | ||
}) | ||
|
||
export default connectionSlice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { default as authSlice, AuthSliceActions, AuthSliceState } from './authSlice' | ||
import { default as connectionSlice, ConnectionSliceActions, ConnectionSliceState } from './connectionSlice' | ||
import { default as toastSlice, ToastSliceActions, ToastSliceState } from './toastSlice' | ||
|
||
interface RootStore extends AuthSliceActions, ToastSliceActions, ConnectionSliceActions { | ||
auth: AuthSliceState | ||
toast: ToastSliceState | ||
connection: ConnectionSliceState | ||
} | ||
|
||
export { authSlice, connectionSlice, toastSlice, RootStore } |
File renamed without changes.
Oops, something went wrong.