Skip to content

Commit

Permalink
initalized frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Feb 20, 2024
1 parent a7e37ae commit 0e7aa42
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 305 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ func (u *UserController) DeleteUser(c *fiber.Ctx) error {
}

return c.SendStatus(fiber.StatusNoContent)
}
}
2 changes: 1 addition & 1 deletion cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func MigrateCommand() *cli.Command {
command := cli.Command{
Name: "migrate",
Aliases: []string{"m"},
Aliases: []string{"m"},
Usage: "Migrate the database, creating tables and relationships",
Category: "Database Operations",
Action: func(c *cli.Context) error {
Expand Down
93 changes: 0 additions & 93 deletions cli/go.sum

This file was deleted.

10 changes: 4 additions & 6 deletions frontend/sac-mobile/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"plugins": [
"@trivago/prettier-plugin-sort-imports"
],
"plugins": ["@trivago/prettier-plugin-sort-imports"],
"printWidth": 80,
"tabWidth": 4,
"semi": true,
Expand All @@ -18,7 +16,7 @@
"^@/",
"^src",
"^[./]"
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true
}
3 changes: 2 additions & 1 deletion frontend/sac-mobile/app/(app)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React from 'react';

import { MaterialCommunityIcons } from '@expo/vector-icons';
import { Tabs } from 'expo-router';

import { MaterialCommunityIcons } from '@expo/vector-icons';

const HomeTabBarIcon = ({ color }: { color: string }) => (
<MaterialCommunityIcons name="home" size={24} color={color} />
);
Expand Down
3 changes: 2 additions & 1 deletion frontend/sac-mobile/app/(app)/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useAuthStore } from '@/hooks/use-auth';
import React from 'react';
import { Button, Text, View } from 'react-native';

import { useAuthStore } from '@/hooks/use-auth';

const Home = () => {
const { logout } = useAuthStore();
return (
Expand Down
1 change: 0 additions & 1 deletion frontend/sac-mobile/app/(auth)/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';

import { Stack } from 'expo-router';
import { SafeAreaView } from 'react-native';

const AuthLayout = () => {
return (
Expand Down
5 changes: 3 additions & 2 deletions frontend/sac-mobile/app/(auth)/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Controller, useForm } from 'react-hook-form';
import { Alert, Button, Text, TextInput, View } from 'react-native';

import { router } from 'expo-router';

import { ZodError, z } from 'zod';

import { useAuthStore } from '@/hooks/use-auth';
Expand Down Expand Up @@ -55,7 +56,7 @@ const Login = () => {
control={control}
render={({ field: { onChange, value } }) => (
<TextInput
autoCapitalize='none'
autoCapitalize="none"
autoCorrect={false}
className="p-2 border border-gray-300"
placeholder="[email protected]"
Expand All @@ -76,7 +77,7 @@ const Login = () => {
control={control}
render={({ field: { onChange, value } }) => (
<TextInput
autoCapitalize='none'
autoCapitalize="none"
autoCorrect={false}
className="p-2 border border-gray-300"
placeholder="Password"
Expand Down
2 changes: 1 addition & 1 deletion frontend/sac-mobile/app/(auth)/register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Controller, useForm } from 'react-hook-form';
import { Alert, Button, Text, TextInput, View } from 'react-native';

import { router } from 'expo-router';

import { ZodError, z } from 'zod';

type RegisterFormData = {
Expand Down Expand Up @@ -33,7 +34,6 @@ const Register = () => {
} = useForm<RegisterFormData>();

const onSubmit = (data: RegisterFormData) => {
// Validate form data using Zod schema
try {
registerSchema.parse(data);
Alert.alert('Form Submitted', JSON.stringify(data));
Expand Down
26 changes: 9 additions & 17 deletions frontend/sac-mobile/app/_layout.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEffect } from 'react';
import { usePathname, Redirect } from 'expo-router';
import { View } from 'react-native';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import View.

import FontAwesome from '@expo/vector-icons/FontAwesome';
import { useFonts } from 'expo-font';
import { Stack, router } from 'expo-router';
import { deleteItemAsync, getItemAsync } from 'expo-secure-store';
import { getItemAsync } from 'expo-secure-store';
import * as SplashScreen from 'expo-splash-screen';

import FontAwesome from '@expo/vector-icons/FontAwesome';

import { useAuthStore } from '@/hooks/use-auth';
import { User } from '@/types/user';

Expand Down Expand Up @@ -49,44 +50,35 @@ export default function RootLayout() {

function RootLayoutNav() {
const { isLoggedIn, login } = useAuthStore();
const previousRoute = usePathname();

useEffect(() => {
const checkLoginStatus = async () => {
// deleteItemAsync('accessToken');
// deleteItemAsync('refreshToken');
// deleteItemAsync('user');
const accessToken = await getItemAsync('accessToken');
const refreshToken = await getItemAsync('refreshToken');
const savedUser = await getItemAsync('user');

const user: User = savedUser ? JSON.parse(savedUser) : null;

if (accessToken && refreshToken) {
// Update the Zustand store on successful login
login({ accessToken, refreshToken }, user);
}

console.log('accessToken', accessToken);
};

checkLoginStatus();
}, [login]); // Only depend on login
}, [login]);

useEffect(() => {
if (isLoggedIn === null) return;

if (!isLoggedIn) {
// Store the previous route before redirecting to the login page
// const currentRoute = navigationRef.getCurrentRoute();
// console.log('currentRoute', currentRoute);
router.push('/(auth)/login');
// } else if (previousRoute.current) {
// // Redirect back to the previous route if available
// router.push(previousRoute.current);
} else {
router.push('/(app)/');
}
}, [isLoggedIn]);


// Log out console to check if the isLoggedIn state is updating correctly
console.log('isLoggedIn', isLoggedIn);

return (
Expand Down
5 changes: 3 additions & 2 deletions frontend/sac-mobile/hooks/use-auth.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { deleteItemAsync, setItemAsync } from 'expo-secure-store';

import { create } from 'zustand';

import { Tokens, User } from '@/types/user';

export type AuthStore = {
isLoggedIn: boolean;
isLoggedIn: boolean | null;
accessToken: string | null;
refreshToken: string | null;
user: User | null;
Expand All @@ -14,7 +15,7 @@ export type AuthStore = {
};

export const useAuthStore = create<AuthStore>((set) => ({
isLoggedIn: false,
isLoggedIn: null,
accessToken: null,
refreshToken: null,
user: null,
Expand Down
17 changes: 10 additions & 7 deletions frontend/sac-mobile/hooks/use-storage-state.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import * as SecureStore from 'expo-secure-store';
import * as React from 'react';
import { Platform } from 'react-native';

import * as SecureStore from 'expo-secure-store';

type UseStateHook<T> = [[boolean, T | null], (value: T | null) => void];

function useAsyncState<T>(
initialValue: [boolean, T | null] = [true, null],
initialValue: [boolean, T | null] = [true, null]
): UseStateHook<T> {
return React.useReducer(
(state: [boolean, T | null], action: T | null = null): [boolean, T | null] => [false, action],
(
state: [boolean, T | null],
action: T | null = null
): [boolean, T | null] => [false, action],
initialValue
) as UseStateHook<T>;
}
Expand All @@ -32,7 +36,6 @@ export async function setStorageItemAsync(key: string, value: string | null) {
}
}
}

export function useStorageState(key: string): UseStateHook<string> {
// Public
const [state, setState] = useAsyncState<string>();
Expand All @@ -48,19 +51,19 @@ export function useStorageState(key: string): UseStateHook<string> {
console.error('Local storage is unavailable:', e);
}
} else {
SecureStore.getItemAsync(key).then(value => {
SecureStore.getItemAsync(key).then((value) => {
setState(value);
});
}
}, [key]);
}, [key, setState]);

// Set
const setValue = React.useCallback(
(value: string | null) => {
setState(value);
setStorageItemAsync(key, value);
},
[key]
[key, setState]
);

return [state, setValue];
Expand Down
Loading

0 comments on commit 0e7aa42

Please sign in to comment.