Skip to content

Commit 170f648

Browse files
author
Thiago Lopes
committed
Trying to keep context state on user device
1 parent 256cf85 commit 170f648

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@expo-google-fonts/poppins": "^0.2.3",
1414
"@expo/metro-config": "^0.17.4",
1515
"@expo/vector-icons": "^14.0.0",
16+
"@react-native-async-storage/async-storage": "1.21.0",
1617
"@react-navigation/bottom-tabs": "^6.5.12",
1718
"@react-navigation/drawer": "^6.6.11",
1819
"@react-navigation/material-bottom-tabs": "^6.2.19",

src/context/AppReducer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ export enum ActionTypes {
1414
INCREASE_QTY = 'INCREASE_QTY',
1515
DECREASE_QTY = 'DECREASE_QTY',
1616
DELETE_ALL_FROM_CART = 'DELETE_ALL_FROM_CART',
17-
DELETE_ALL_FROM_FAVORITES = 'DELETE_ALL_FROM_FAVORITES'
17+
DELETE_ALL_FROM_FAVORITES = 'DELETE_ALL_FROM_FAVORITES',
18+
RESTORE_STATE = 'RESTORE_STATE'
1819
}
1920

2021
type Action =
@@ -25,6 +26,7 @@ type Action =
2526
| { type: ActionTypes.DECREASE_QTY; payload: { id: string; color: string } }
2627
| { type: ActionTypes.DELETE_ALL_FROM_CART }
2728
| { type: ActionTypes.DELETE_ALL_FROM_FAVORITES }
29+
| { type: ActionTypes.RESTORE_STATE; payload: AppState }
2830

2931
export const AppReducer = (
3032
state: AppState = initialState,

src/context/GlobalState.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { createContext, useReducer, ReactNode, Dispatch } from 'react'
1+
import { createContext, useReducer, ReactNode, useEffect } from 'react'
2+
import AsyncStorage from '@react-native-async-storage/async-storage'
23
import { AppReducer, ActionTypes } from '@Context/AppReducer'
34
import { Product, Order, AppState, GlobalStateContextProps } from '@Data/types'
45
import allProducts from '@Data/data'
@@ -33,6 +34,36 @@ interface GlobalStateProps {
3334
export const GlobalProvider = ({ children }: GlobalStateProps) => {
3435
const [state, dispatch] = useReducer(AppReducer, initialState)
3536

37+
useEffect(() => {
38+
const loadState = async () => {
39+
try {
40+
const storedState = await AsyncStorage.getItem('appState')
41+
if (storedState !== null) {
42+
dispatch({
43+
type: ActionTypes.RESTORE_STATE,
44+
payload: JSON.parse(storedState)
45+
})
46+
}
47+
} catch (error) {
48+
console.error('Error loading state from AsyncStorage:', error)
49+
}
50+
}
51+
52+
loadState()
53+
}, [])
54+
55+
useEffect(() => {
56+
const saveState = async () => {
57+
try {
58+
await AsyncStorage.setItem('appState', JSON.stringify(state))
59+
} catch (error) {
60+
console.error('Error saving state to AsyncStorage:', error)
61+
}
62+
}
63+
64+
saveState()
65+
}, [state])
66+
3667
const isOnFavorite = (id: string) => {
3768
return state.favorites.some((product) => product.id === id)
3869
}

yarn.lock

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1747,6 +1747,13 @@
17471747
mkdirp "^1.0.4"
17481748
rimraf "^3.0.2"
17491749

1750+
"@react-native-async-storage/async-storage@1.21.0":
1751+
version "1.21.0"
1752+
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.21.0.tgz#d7e370028e228ab84637016ceeb495878b7a44c8"
1753+
integrity sha512-JL0w36KuFHFCvnbOXRekqVAUplmOyT/OuCQkogo6X98MtpSaJOKEAeZnYO8JB0U/RIEixZaGI5px73YbRm/oag==
1754+
dependencies:
1755+
merge-options "^3.0.4"
1756+
17501757
"@react-native-community/cli-clean@12.3.6":
17511758
version "12.3.6"
17521759
resolved "https://registry.yarnpkg.com/@react-native-community/cli-clean/-/cli-clean-12.3.6.tgz#e8a7910bebc97266fd5068649013a03958021fc4"
@@ -4221,6 +4228,11 @@ is-path-inside@^3.0.2:
42214228
resolved "https://registry.npmjs.org/is-path-inside/-/is-path-inside-3.0.3.tgz"
42224229
integrity sha512-Fd4gABb+ycGAmKou8eMftCupSir5lRxqf4aD/vd0cD2qc4HL07OjCeuHMr8Ro4CoMaeCKDB0/ECBOVWjTwUvPQ==
42234230

4231+
is-plain-obj@^2.1.0:
4232+
version "2.1.0"
4233+
resolved "https://registry.yarnpkg.com/is-plain-obj/-/is-plain-obj-2.1.0.tgz#45e42e37fccf1f40da8e5f76ee21515840c09287"
4234+
integrity sha512-YWnfyRwxL/+SsrWYfOpUtz5b3YD+nyfkHvjbcanzk8zgyO4ASD67uVMRt8k5bM4lLMDnXfriRhOpemw+NfT1eA==
4235+
42244236
is-plain-object@^2.0.4:
42254237
version "2.0.4"
42264238
resolved "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.4.tgz"
@@ -4731,6 +4743,13 @@ memory-cache@~0.2.0:
47314743
resolved "https://registry.npmjs.org/memory-cache/-/memory-cache-0.2.0.tgz"
47324744
integrity sha512-OcjA+jzjOYzKmKS6IQVALHLVz+rNTMPoJvCztFaZxwG14wtAW7VRZjwTQu06vKCYOxh4jVnik7ya0SXTB0W+xA==
47334745

4746+
merge-options@^3.0.4:
4747+
version "3.0.4"
4748+
resolved "https://registry.yarnpkg.com/merge-options/-/merge-options-3.0.4.tgz#84709c2aa2a4b24c1981f66c179fe5565cc6dbb7"
4749+
integrity sha512-2Sug1+knBjkaMsMgf1ctR1Ujx+Ayku4EdJN4Z+C2+JzoeF7A3OZ9KM2GY0CpQS51NR61LTurMJrRKPhSs3ZRTQ==
4750+
dependencies:
4751+
is-plain-obj "^2.1.0"
4752+
47344753
merge-stream@^2.0.0:
47354754
version "2.0.0"
47364755
resolved "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz"

0 commit comments

Comments
 (0)