Fakultas Ilmu Komputer UI

index.ts 1.17 KB
Newer Older
1
import { useContext, useEffect, useCallback, useState } from 'react';
2
3
4
import { useNavigation } from '@react-navigation/native';
import { UserContext } from 'provider';
import * as ROUTES from 'constants/routes';
5
6
import CACHE_KEYS from 'constants/cacheKeys';
import { getCache } from 'utils/cache';
7

Wulan Mantiri's avatar
Wulan Mantiri committed
8
9
const useAuthEffect = () => {
  const { isFirstLoading } = useContext(UserContext);
10
  const navigation = useNavigation();
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  const [isLoading, setIsLoading] = useState(false);

  const checkCart = useCallback(async () => {
    setIsLoading(true);
    const dietProfileId = await getCache(CACHE_KEYS.dietProfileId);
    const cartId = await getCache(CACHE_KEYS.cartId);
    if (!dietProfileId) {
      navigation.reset({
        index: 0,
        routes: [
          { name: ROUTES.initial },
          { name: ROUTES.allAccessQuestionnaire },
        ],
      });
    } else if (!cartId) {
      navigation.reset({
        index: 0,
        routes: [{ name: ROUTES.initial }, { name: ROUTES.choosePlan }],
      });
    }
    setIsLoading(false);
  }, [navigation]);
33
34

  useEffect(() => {
Wulan Mantiri's avatar
Wulan Mantiri committed
35
36
    checkCart();
  }, [checkCart]);
37
38

  return isFirstLoading || isLoading;
39
40
41
};

export default useAuthEffect;