Files
Steve Galili bd98be05c6 docs(cookbook): network requests recipes (#1655)
* squash prev commits

* remove unneeded axios mock

* set maxWorkers=2

* run with a slow test reporter

* revert: run with a slow test reporter

* Add url check to mock and further reading and alternatives

* use MSW for all API calls in cookbook test suits

* Comments with implem. explanation

* Arrange docs initially to reflect new scenario and remove jest.setTimeout

* updating docs (1)

* updating docs (2)

* updating docs with global guarding and conclusion

---------

Co-authored-by: stevegalili <steve.galili@mywheels.nl>
2024-09-18 09:51:53 +02:00

53 lines
1.4 KiB
TypeScript

import React, { useEffect, useState } from 'react';
import { Text } from 'react-native';
import { User } from './types';
import ContactsList from './components/ContactsList';
import FavoritesList from './components/FavoritesList';
import getAllContacts from './api/getAllContacts';
import getAllFavorites from './api/getAllFavorites';
export default () => {
const [usersData, setUsersData] = useState<User[]>([]);
const [favoritesData, setFavoritesData] = useState<User[]>([]);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const _getAllContacts = async () => {
const _data = await getAllContacts();
setUsersData(_data);
};
const _getAllFavorites = async () => {
const _data = await getAllFavorites();
setFavoritesData(_data);
};
const run = async () => {
try {
await Promise.all([_getAllContacts(), _getAllFavorites()]);
} catch (e) {
const message = isErrorWithMessage(e) ? e.message : 'Something went wrong';
setError(message);
}
};
void run();
}, []);
if (error) {
return <Text>An error occurred: {error}</Text>;
}
return (
<>
<FavoritesList users={favoritesData} />
<ContactsList users={usersData} />
</>
);
};
const isErrorWithMessage = (
e: unknown,
): e is {
message: string;
} => typeof e === 'object' && e !== null && 'message' in e;