How can I improve the efficiency of react native screen with 3 multiple api calls

30 Views Asked by At

This code is from reading screen where user progress and last read are saved. Progress is then sent to server. But using these 3 api (start read, update read 3rd is retrieveRead working same as these two) is slowing the app. What can be solution to improve this?

Also last read is saved on every font change but I need latest settings after user is done with screen and before unmount. If this can be done more efficiently it will also be helpful

 const startReadingProgress = async ()=> {
   const user = await 
   getUserFromAsyncStorage();

   if (user && user.userId) {
    axios.post(`${localHostLink}api/readingProgress`
} };

 const updateReadingProgress = 
    async (newProgress) => {        
    const user = await 
    getUserFromAsyncStorage();
  
    if (user && user.userId) {
       axios.put(  
 `${localHostLink}api/readingProgress`{
 userId: user?.userId,
 bookId: bookId,
 chapterId: ,
 progress: newProgress >= 0 ? 
 newProgress : 1,
},
{
headers: {
 Authorization: `Bearer 
 ${user?.token}`,
}


useEffect(() => {
const handleSaveLastChapter = async () => {
  try {
    const chapterId = selectedChapter && selectedChapter._id;
    if (chapterId) {
      await AsyncStorage.setItem(`${bookId}_lastChapter`,String(chapterId));
    }
  } catch (error) {}
};
handleSaveLastChapter();
}, [selectedChapter]);)

useEffect(() => {
const handleSaveLastFont = async () => {
  try {
    const chapterId = selectedChapter && selectedChapter._id;
    if (chapterId) {
      await AsyncStorage.setItem(`${bookId}_lastChapter`,String(chapterId));
    }
  } catch (error) {}
};
handleSaveLastFont();
}, [fontsize]);)

Screen doing many tasks like saving offset. Total pages count . Navigation to specific page and list of pages etc

0

There are 0 best solutions below