diff --git a/frontend/occupi-mobile4/screens/Dashboard/Dashboard.tsx b/frontend/occupi-mobile4/screens/Dashboard/Dashboard.tsx index d721d28f..99fc78b1 100644 --- a/frontend/occupi-mobile4/screens/Dashboard/Dashboard.tsx +++ b/frontend/occupi-mobile4/screens/Dashboard/Dashboard.tsx @@ -22,7 +22,7 @@ import { widthPercentageToDP as wp, heightPercentageToDP as hp } from 'react-nat // import { number } from 'zod'; const getRandomNumber = () => { - return Math.floor(Math.random() * 20)+300; + return Math.floor(Math.random() * 20) + 300; }; const Dashboard = () => { @@ -30,6 +30,7 @@ const Dashboard = () => { const [numbers, setNumbers] = useState(Array.from({ length: 15 }, getRandomNumber)); const [isDarkMode, setIsDarkMode] = useState(colorScheme === 'dark'); const [checkedIn, setCheckedIn] = useState(false); + const [name, setName] = useState("User"); const toast = useToast() useEffect(() => { const intervalId = setInterval(() => { @@ -38,50 +39,22 @@ const Dashboard = () => { return newNumbers; }); }, 3000); - // console.log(numbers); setIsDarkMode(colorScheme === 'dark'); return () => clearInterval(intervalId); }, [colorScheme]); useEffect(() => { const getUserDetails = async () => { - console.log("heree"); - try { - const response = await fetch('https://dev.occupi.tech/api/user-details?email=kamogelomoeketse@gmail.com') - const data = await response.json(); - if (response.ok) { - saveUserData(JSON.stringify(data)); - console.log(data); - } else { - console.log(data); - toast.show({ - placement: 'top', - render: ({ id }) => { - return ( - - {data.error.message} - - ); - }, - }); - } - } catch (error) { - console.error('Error:', error); - toast.show({ - placement: 'top', - render: ({ id }) => { - return ( - - Network Error: {error.message} - - ); - }, - }); + let result = await SecureStore.getItemAsync('UserData'); + console.log(result); + if (result !== undefined) { + let jsonresult = JSON.parse(result); + setName(String(jsonresult?.data?.details?.name)); } }; getUserDetails(); - }, [toast]); - + }, []); + const checkIn = () => { if (checkedIn === false) { setCheckedIn(true); @@ -109,9 +82,7 @@ const Dashboard = () => { async function saveUserEmail(value) { await SecureStore.setItemAsync('email', value); } - async function saveUserData(value) { - await SecureStore.setItemAsync('UserData', value); - } + saveUserEmail('kamogelomoeketse@gmail.com'); @@ -126,7 +97,7 @@ const Dashboard = () => { - Hi Sabrina 👋 + Hi {name} 👋 Welcome to Occupi @@ -144,7 +115,7 @@ const Dashboard = () => { {numbers[0]} - {numbers[0]/10+5}% + {numbers[0] / 10 + 5}% @@ -169,10 +140,10 @@ const Dashboard = () => { Occupancy levels { } ] }} - width={Dimensions.get("window").width -30} // from react-native + width={Dimensions.get("window").width - 30} // from react-native height={220} // yAxisLabel="" // yAxisSuffix="k" diff --git a/frontend/occupi-mobile4/screens/Dashboard/__tests__/Dashboard-test.tsx b/frontend/occupi-mobile4/screens/Dashboard/__tests__/Dashboard-test.tsx index 0eca36a2..ae402883 100644 --- a/frontend/occupi-mobile4/screens/Dashboard/__tests__/Dashboard-test.tsx +++ b/frontend/occupi-mobile4/screens/Dashboard/__tests__/Dashboard-test.tsx @@ -12,6 +12,11 @@ jest.mock('@gluestack-ui/themed', () => ({ }), })); +jest.mock('expo-secure-store', () => ({ + getItemAsync: jest.fn(), + setItemAsync: jest.fn(), +})); + const renderWithProvider = (component) => { return render( @@ -26,9 +31,24 @@ describe('Dashboard component', () => { // expect(tree).toMatchSnapshot(); // }); + const mockedData = { + data: { + details: { + name: 'John Doe', + contactNo: '1234567890', + pronouns: 'He/Him', + dob: '1990-01-01T00:00:00Z', // Make sure the format matches your component's expectations + }, + email: 'johndoe@example.com', + occupiId: 'EMP12345', + }, + }; + + // Mock SecureStore getItemAsync to resolve with the mocked data + require('expo-secure-store').getItemAsync.mockResolvedValueOnce(JSON.stringify(mockedData)); + it('renders text correctly', () => { const { getByText } = renderWithProvider(); - expect(getByText('Hi Sabrina 👋')).toBeTruthy(); expect(getByText('Welcome to Occupi')).toBeTruthy(); }); diff --git a/frontend/occupi-mobile4/screens/Login/SignIn.tsx b/frontend/occupi-mobile4/screens/Login/SignIn.tsx index 39e3a427..4a6c1dcf 100644 --- a/frontend/occupi-mobile4/screens/Login/SignIn.tsx +++ b/frontend/occupi-mobile4/screens/Login/SignIn.tsx @@ -85,19 +85,14 @@ const SignInForm = () => { console.log('Biometric hardware available:', isBiometricAvailable); }; - const storeData = async (value) => { - try { - await AsyncStorage.setItem('email', value); - } catch (e) { - // saving error - console.log(e); - } - }; - - async function saveUserData(value) { + async function storeUserData(value) { await SecureStore.setItemAsync('UserData', value); } + async function storeToken(value) { + await SecureStore.setItemAsync('Token', value); + } + const handleBiometricSignIn = async () => { const biometricType = await LocalAuthentication.supportedAuthenticationTypesAsync(); @@ -160,7 +155,7 @@ const SignInForm = () => { const onSubmit = async (_data: SignInSchemaType) => { setLoading(true); try { - const response = await fetch('https://dev.occupi.tech/auth/login', { + const response = await fetch('https://dev.occupi.tech/auth/login-mobile', { method: 'POST', headers: { Accept: 'application/json', @@ -173,16 +168,10 @@ const SignInForm = () => { credentials: "include" }); const data = await response.json(); - const cookies = response.headers.get('Accept'); - // CookieManager.get('https://dev.occupi.tech') - // .then((cookies) => { - // console.log('CookieManager.get =>', cookies); - // }); - console.log(cookies); if (response.ok) { + console.log(data.data.token); setLoading(false); - storeData(_data.email); - saveUserData(_data.email); + storeToken(data.data.token); toast.show({ placement: 'top', render: ({ id }) => { @@ -193,6 +182,50 @@ const SignInForm = () => { ); }, }); + try { + let authToken = await SecureStore.getItemAsync('Token'); + // console.log(authToken); + + const response = await fetch(`https://dev.occupi.tech/api/user-details?email=${_data.email}`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + 'Authorization': `${authToken}` + }, + credentials: "include" + }); + const data = await response.json(); + console.log("here"); + if (response.ok) { + storeUserData(JSON.stringify(data)); + console.log(`Data of ${_data.email}: `,data); + } else { + console.log(data); + toast.show({ + placement: 'top', + render: ({ id }) => { + return ( + + {data.error.message} + + ); + }, + }); + } + } catch (error) { + console.error('Error:', error); + toast.show({ + placement: 'top', + render: ({ id }) => { + return ( + + Network Error + + ); + }, + }); + } router.replace('/home'); } else { setLoading(false); @@ -210,9 +243,7 @@ const SignInForm = () => { } } catch (error) { console.error('Error:', error); - // setResponse('An error occurred'); } - // }, 3000); setLoading(false); }; diff --git a/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx b/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx index 78e123f5..1c5dce6c 100644 --- a/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx +++ b/frontend/occupi-mobile4/screens/Login/SplashScreen.tsx @@ -93,7 +93,7 @@ export default function SplashScreen() { useEffect(() => { const timer = setTimeout(() => { setSelectedIndex(1); // Assuming Onboarding1 is at index 1 - router.replace('/home'); // Navigate to Onboarding1 screen + router.replace('/login'); // Navigate to Onboarding1 screen }, 5000); // 8 seconds return () => clearTimeout(timer); // Clean up timer on component unmount diff --git a/frontend/occupi-mobile4/screens/Settings/Profile.tsx b/frontend/occupi-mobile4/screens/Settings/Profile.tsx index e0e26423..66d1980e 100644 --- a/frontend/occupi-mobile4/screens/Settings/Profile.tsx +++ b/frontend/occupi-mobile4/screens/Settings/Profile.tsx @@ -61,9 +61,9 @@ const Profile = () => { useEffect(() => { const getUserDetails = async () => { let result = await SecureStore.getItemAsync('UserData'); - console.log(result); + console.log("UserData:",result); // setUserDetails(JSON.parse(result).data); - let jsonresult = JSON.parse(result | "{}"); + let jsonresult = JSON.parse(result); // console.log(jsonresult.data.details.name); setName(String(jsonresult?.data?.details?.name)); setEmail(String(jsonresult?.data?.email)); diff --git a/frontend/occupi-mobile4/screens/Settings/Settings.tsx b/frontend/occupi-mobile4/screens/Settings/Settings.tsx index d846c04b..6d2a7f95 100644 --- a/frontend/occupi-mobile4/screens/Settings/Settings.tsx +++ b/frontend/occupi-mobile4/screens/Settings/Settings.tsx @@ -26,25 +26,23 @@ const Settings = () => { useEffect(() => { const getUserDetails = async () => { let result = await SecureStore.getItemAsync('UserData'); - // setUserDetails(JSON.parse(result).data); let jsonresult = JSON.parse(result); - // console.log(jsonresult.data.details.name); setName(String(jsonresult.data.details.name)); setPosition(String(jsonresult.data.position)); - // console.log(JSON.parse(result).data.details.name); }; getUserDetails(); }, []); const handleLogout = async () => { + let authToken = await SecureStore.getItemAsync('Token'); try { const response = await fetch('https://dev.occupi.tech/auth/logout', { method: 'POST', headers: { Accept: 'application/json', - 'Content-Type': 'application/json' + 'Content-Type': 'application/json', + 'Authorization': `${authToken}` }, - body: JSON.stringify({}), credentials: "include" }); const data = await response.json(); @@ -53,6 +51,7 @@ const Settings = () => { alert("logged out siccessfully"); router.replace('/login'); } else { + console.log(data); alert("unable to logout"); } } catch (error) {