Skip to content

Commit

Permalink
update animation
Browse files Browse the repository at this point in the history
  • Loading branch information
milad-alizadeh committed Dec 19, 2023
1 parent aa59ad3 commit 8216984
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 37 deletions.
5 changes: 3 additions & 2 deletions src/app/(tabs)/my-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import {
Text,
} from '~/components'
import { useAnalytics, useFetchMatchedRecipes, useSession } from '~/hooks'
import { useStore } from '~/providers'
import { useDetailsModal, useStore } from '~/providers'

export default function MyBarScreen() {
const { myBarPopoverDismissed, setMyBarPopoverDismissed, setCurrentIngredientId } = useStore()
const { myBarPopoverDismissed, setMyBarPopoverDismissed } = useStore()
const { setCurrentIngredientId } = useDetailsModal()
const { user } = useSession()
const { capture } = useAnalytics()
const [deleteingItemId, setDeleteingItemId] = useState<string>('')
Expand Down
21 changes: 11 additions & 10 deletions src/app/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { Feedback } from '~/components'
import { DetailsModal } from '~/components/DetailsModal'
import { useAnalytics } from '~/hooks'
import { StoreProvider } from '~/providers'
import { AppContentProvider } from '~/providers/AppContentProvider'
import { SessionProvider } from '~/providers/SessionProvider'
import { AppContentProvider, DetailsModalProvider, SessionProvider } from '~/providers'
import { api } from '~/services/api'
import { initSentry } from '~/services/sentry'
import { customFontsToLoad } from '~/theme/typography'
Expand Down Expand Up @@ -81,10 +80,11 @@ const AppWrapper = ({ children }) => {
function RootLayoutNav() {
try {
return (
<AppWrapper>
<SessionProvider>
<ApolloProvider client={api?.apolloClient}>
<AppContentProvider>
<AppWrapper>
<SessionProvider>
<ApolloProvider client={api?.apolloClient}>
<AppContentProvider>
<DetailsModalProvider>
<StoreProvider>
<DetailsModal />
<Feedback />
Expand All @@ -103,10 +103,11 @@ function RootLayoutNav() {
<Stack.Screen name="add-ingredients" />
</Stack>
</StoreProvider>
</AppContentProvider>
</ApolloProvider>
</SessionProvider>
</AppWrapper>
</DetailsModalProvider>
</AppContentProvider>
</ApolloProvider>
</SessionProvider>
</AppWrapper>
)
} catch (error) {
captureError(error.message)
Expand Down
5 changes: 3 additions & 2 deletions src/app/add-ingredients.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DELETE_FROM_MY_BAR } from '~/graphql/mutations/deleteFromMyBar'
import { useAnalytics } from '~/hooks/useAnalytics'
import { useFetchIngredients } from '~/hooks/useFetchIngredients'
import { useSession } from '~/hooks/useSession'
import { useStore } from '~/providers'
import { useDetailsModal, useStore } from '~/providers'
import { captureError } from '~/utils/captureError'

export default function AddIngredientsScreen() {
Expand All @@ -25,7 +25,8 @@ export default function AddIngredientsScreen() {
setSelectedItems,
setInitialSelectedItems,
} = useFetchIngredients()
const { setCurrentIngredientId, setEventCount } = useStore()
const { setEventCount } = useStore()
const { setCurrentIngredientId } = useDetailsModal()
const [loading, setLoading] = useState(false)
const { user } = useSession()
const [addToMyBar, { error: addError }] = useMutation(ADD_TO_MY_BAR)
Expand Down
4 changes: 2 additions & 2 deletions src/components/DetailsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useRef } from 'react'
import { BottomSheet, BottomSheetRef } from '~/components/BottomSheet'
import { useStore } from '~/providers'
import { useDetailsModal } from '~/providers'
import { EquipmentDetails } from './EquipmentDetails'
import { IngredientDetails } from './IngredientDetails'

export const DetailsModal = () => {
const modalRef = useRef<BottomSheetRef>(null)
const { currentEquipmentId, currentIngredientId, setCurrentEquipmentId, setCurrentIngredientId } =
useStore()
useDetailsModal()

useEffect(() => {
if (currentIngredientId || currentEquipmentId) {
Expand Down
12 changes: 4 additions & 8 deletions src/components/RecipeTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { GetRecipeDetailsQuery, Units } from '~/__generated__/graphql'
import { defaultJiggerSize } from '~/constants'
import { GET_UNITS } from '~/graphql/queries'
import { useAnalytics, useSession } from '~/hooks'
import { useStore } from '~/providers'
import { useDetailsModal, useStore } from '~/providers'
import { convertUnitToOtherSystem } from '~/store'
import { ListItem } from './ListItem'
import { RecipeMeasurements } from './RecipeMeasurements'
Expand Down Expand Up @@ -45,13 +45,7 @@ export const RecipeTabs = function RecipeTabs({
loading,
isPitcher,
}: RecipeTabsProps) {
const {
selectedJiggerSize,
doubleRecipe,
selectedUnitSystem,
setCurrentEquipmentId,
setCurrentIngredientId,
} = useStore()
const { selectedJiggerSize, doubleRecipe, selectedUnitSystem } = useStore()
const { capture } = useAnalytics()
const { isLoggedIn } = useSession()
const { data } = useQuery(GET_UNITS)
Expand All @@ -74,6 +68,7 @@ export const RecipeTabs = function RecipeTabs({

const renderIngredientItem = useCallback(
({ ingredient, quantity, unit, isOptional, inMyBar }: Ingredient & { inMyBar: boolean }) => {
const { setCurrentIngredientId } = useDetailsModal()
if (!units) return null
const { quantity: outputQuantity, unit: outputUnit } = convertUnitToOtherSystem({
unit: unit as Units,
Expand Down Expand Up @@ -106,6 +101,7 @@ export const RecipeTabs = function RecipeTabs({

const renderEquipmentItem = useCallback(
({ name, imageUrl, id }: Equipment) => {
const { setCurrentEquipmentId } = useDetailsModal()
return (
<ListItem
key={id}
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useFetchIngredientDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { router, useGlobalSearchParams } from 'expo-router'
import { GetRecipesByIngredientQuery } from '~/__generated__/graphql'
import { ADD_TO_MY_BAR, DELETE_FROM_MY_BAR } from '~/graphql/mutations'
import { GET_INGREDIENT_DETAILS, GET_MY_BAR, GET_RECIPES_BY_INGREDIENT } from '~/graphql/queries'
import { useStore } from '~/providers'
import { useDetailsModal } from '~/providers'
import { captureError } from '~/utils/captureError'
import { useSession } from './useSession'

Expand All @@ -20,7 +20,7 @@ export const useFetchIngredientDetails = (ingredientId: string, onClosed: () =>
fetchPolicy: 'cache-and-network',
})

const { setCurrentIngredientId } = useStore()
const { setCurrentIngredientId } = useDetailsModal()

const [addToMyBar, { loading: addLoading }] = useMutation(ADD_TO_MY_BAR)
const [deleteFromMyBar] = useMutation(DELETE_FROM_MY_BAR)
Expand Down
36 changes: 36 additions & 0 deletions src/providers/DetailsModalProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Dispatch, FC, SetStateAction, createContext, useContext, useState } from 'react'

interface DetailsModalContextType {
currentEquipmentId: string
currentIngredientId: string
setCurrentEquipmentId: Dispatch<SetStateAction<string>>
setCurrentIngredientId: Dispatch<SetStateAction<string>>
}

const DetailsModalContext = createContext<DetailsModalContextType | undefined>(undefined)

export const DetailsModalProvider: FC<{ children: any }> = ({ children }) => {
const [currentIngredientId, setCurrentIngredientId] = useState<string | null>(null)
const [currentEquipmentId, setCurrentEquipmentId] = useState<string | null>(null)

return (
<DetailsModalContext.Provider
value={{
currentEquipmentId,
currentIngredientId,
setCurrentEquipmentId,
setCurrentIngredientId,
}}
>
{children}
</DetailsModalContext.Provider>
)
}

export const useDetailsModal = () => {
const context = useContext(DetailsModalContext)
if (!context) {
throw new Error('useDetailsModal must be used within a DetailsModalProvider')
}
return context
}
12 changes: 1 addition & 11 deletions src/providers/StoreProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import AsyncStorage from '@react-native-async-storage/async-storage'
import React, { Dispatch, FC, SetStateAction, createContext, useContext, useState } from 'react'
import React, { Dispatch, FC, SetStateAction, createContext, useContext } from 'react'
import { defaultJiggerSize, defaultUnitSystem } from '~/constants'
import { clearPersistedState, usePersistedState } from '~/hooks/usePeristedState'
import { JiggerSize, UnitSystem } from '~/types'

interface StoreContextType {
currentEquipmentId: string
currentIngredientId: string
doubleRecipe: boolean
feedbackShown: boolean
eventCount: Record<string, any>
myBarPopoverDismissed: boolean
selectedJiggerSize: JiggerSize
selectedUnitSystem: UnitSystem
setCurrentEquipmentId: Dispatch<SetStateAction<string>>

setCurrentIngredientId: Dispatch<SetStateAction<string>>
setDoubleRecipe: Dispatch<SetStateAction<boolean>>
setFeedbackShown: Dispatch<SetStateAction<boolean>>
setMyBarPopoverDismissed: Dispatch<SetStateAction<boolean>>
Expand All @@ -30,8 +26,6 @@ export const StoreProvider: FC<{ children: any }> = ({ children }) => {
// AsyncStorage.clear()
// clearPersistedState()

const [currentIngredientId, setCurrentIngredientId] = useState<string | null>(null)
const [currentEquipmentId, setCurrentEquipmentId] = useState<string | null>(null)
const [feedbackShown, setFeedbackShown] = usePersistedState('feedbackShown', false)
const [eventCount, setEventCount] = usePersistedState('eventCount', {
recipeView: {},
Expand All @@ -57,16 +51,12 @@ export const StoreProvider: FC<{ children: any }> = ({ children }) => {
return (
<StoreContext.Provider
value={{
currentEquipmentId,
currentIngredientId,
doubleRecipe,
eventCount,
feedbackShown,
myBarPopoverDismissed,
selectedJiggerSize,
selectedUnitSystem,
setCurrentEquipmentId,
setCurrentIngredientId,
setDoubleRecipe,
setEventCount,
setFeedbackShown,
Expand Down
1 change: 1 addition & 0 deletions src/providers/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './StoreProvider'
export * from './SessionProvider'
export * from './AppContentProvider'
export * from './DetailsModalProvider'

0 comments on commit 8216984

Please sign in to comment.