-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
recoil Integration and redux removal
- Loading branch information
Shamoil
authored and
Shamoil
committed
Aug 28, 2024
1 parent
9c4aa4d
commit ec198a5
Showing
11 changed files
with
191 additions
and
94 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { SIMPSONS_API } from '@env'; | ||
|
||
export const Config = { | ||
API_URL: SIMPSONS_API | ||
API_URL: 'https://thesimpsonsquoteapi.glitch.me/' | ||
}; |
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,43 @@ | ||
import { atom, selector } from 'recoil'; | ||
import { getUser } from '@app/services/userService'; | ||
import { Errors } from '@app/utils/erros'; | ||
|
||
// Atom to manage user state | ||
export const userState = atom({ | ||
key: 'userState', | ||
default: null | ||
}); | ||
|
||
// Atom to manage loading state | ||
export const userIsLoadingState = atom({ | ||
key: 'userIsLoadingState', | ||
default: false | ||
}); | ||
|
||
// Atom to manage error messages | ||
export const userErrorMessageState = atom({ | ||
key: 'userErrorMessageState', | ||
default: null | ||
}); | ||
|
||
// Atom to trigger a fetch | ||
export const fetchTriggerState = atom({ | ||
key: 'fetchTriggerState', | ||
default: 0 // This will be incremented to trigger re-fetching | ||
}); | ||
|
||
// Selector to fetch user data | ||
export const fetchUserSelector = selector({ | ||
key: 'fetchUserSelector', | ||
get: async ({ get }) => { | ||
get(fetchTriggerState); // Read the trigger state to force re-fetch | ||
|
||
const response = await getUser(); | ||
if (response.ok) { | ||
const { data } = response; | ||
|
||
return data[0]; | ||
} | ||
throw new Error(Errors.USER_FETCH_ERROR); | ||
} | ||
}); |
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,30 +1,26 @@ | ||
import { connect } from 'react-redux'; | ||
import { PropTypes } from 'prop-types'; | ||
import React, { useEffect } from 'react'; | ||
import { useRecoilValue } from 'recoil'; | ||
import { appState } from './recoilState'; | ||
import AppNavigator from '@navigators/appNavigator'; | ||
import Container from '@atoms/Container'; | ||
import React, { useEffect } from 'react'; | ||
import { setTopLevelNavigator } from '@app/services/navigationService'; | ||
import { rootScreenActions } from './reducer'; | ||
import { navigateAndReset } from '@app/services/navigationService'; | ||
|
||
const RootScreen = () => { | ||
const app = useRecoilValue(appState); | ||
|
||
const RootScreen = props => { | ||
useEffect(() => { | ||
// Run the startup function when the component mounts | ||
props.startup(); | ||
}, []); | ||
// Simulate startup effect | ||
if (!app) { | ||
setTimeout(() => navigateAndReset('MainScreen'), 1000); | ||
} | ||
}, [app]); | ||
|
||
return ( | ||
<Container testID="root-screen"> | ||
<AppNavigator ref={setTopLevelNavigator} /> | ||
<AppNavigator /> | ||
</Container> | ||
); | ||
}; | ||
|
||
RootScreen.propTypes = { | ||
startup: PropTypes.func | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
startup: () => dispatch(rootScreenActions.startup()) | ||
}); | ||
export default connect(null, mapDispatchToProps)(RootScreen); | ||
export default RootScreen; | ||
export { RootScreen as RootScreenTest }; |
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,6 @@ | ||
import { atom } from 'recoil'; | ||
|
||
export const appState = atom({ | ||
key: 'appState', | ||
default: null | ||
}); |
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,3 @@ | ||
export const Errors = { | ||
USER_FETCH_ERROR: "'There was an error while fetching user information.'" | ||
}; |
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
Oops, something went wrong.