Fakultas Ilmu Komputer UI

index.tsx 941 Bytes
Newer Older
1
2
3
4
import React, { FC, useContext } from 'react';
import { UserContext } from 'provider';
import { Button, Icon } from 'react-native-elements';
import { StyleSheet, View } from 'react-native';
5
6
import { useNavigation } from '@react-navigation/native';
import * as ROUTES from 'constants/routes';
7

Wulan Mantiri's avatar
Wulan Mantiri committed
8
const LogoutButton: FC<{
9
  tintColor?: string;
Wulan Mantiri's avatar
Wulan Mantiri committed
10
}> = () => {
11
  const { logout, isAuthenticated } = useContext(UserContext);
12
13
14
15
16
17
18
19
20
  const navigation = useNavigation();

  const handlePress = async () => {
    await logout();
    navigation.reset({
      index: 0,
      routes: [{ name: ROUTES.initial }],
    });
  };
21
22
23
24
25

  return isAuthenticated ? (
    <Button
      icon={<Icon name="logout" type="material" />}
      buttonStyle={styles.button}
26
      onPress={handlePress}
27
28
29
30
31
32
33
34
35
36
37
38
39
40
      testID="logoutButton"
    />
  ) : (
    <View />
  );
};

const styles = StyleSheet.create({
  button: {
    backgroundColor: '#fff',
  },
});

export default LogoutButton;