Merging branch 'userrole' to implement role management
This commit is contained in:
@@ -8,25 +8,31 @@ import { StyleSheet, View,Text } from 'react-native';
|
|||||||
import { useChantier } from '../ContextChantier';
|
import { useChantier } from '../ContextChantier';
|
||||||
import Anomaly from '@/components/anomaly';
|
import Anomaly from '@/components/anomaly';
|
||||||
|
|
||||||
|
import { useUser } from '../ContextUser';
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const { chantier, setChantier } = useChantier();
|
const { chantier, setChantier } = useChantier();
|
||||||
|
const { role } = useUser();
|
||||||
|
|
||||||
|
|
||||||
return(
|
return(
|
||||||
<ThemedView lvl={3} style={styles.back}>
|
<ThemedView lvl={3} style={styles.back}>
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
|
<ChantierSummary style={styles.summary} data={{ chantier }} />
|
||||||
<View style={{width:"100%", position: 'absolute',marginLeft:"50%"}}>
|
|
||||||
<SetStatus></SetStatus>
|
|
||||||
</View>
|
|
||||||
<View style={{width:"100%", position: 'absolute'}}>
|
<View style={{width:"100%", position: 'absolute'}}>
|
||||||
<SelectChantier></SelectChantier>
|
<SelectChantier></SelectChantier>
|
||||||
</View>
|
</View>
|
||||||
<ChantierSummary style={styles.summary} data={{chantier}}/>
|
<ChantierSummary style={styles.summary} data={{chantier}}/>
|
||||||
<Anomaly style={styles.anomaly} data={{chantier}}/>
|
<Anomaly style={styles.anomaly} data={{chantier}}/>
|
||||||
|
{role === "chef" && (
|
||||||
|
<View style={{width:"100%", position: 'absolute',marginLeft:"50%"}}>
|
||||||
|
<SetStatus></SetStatus>
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
{role === "resp"}
|
||||||
|
|
||||||
</View>
|
</View>
|
||||||
</ThemedView>
|
</ThemedView>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -42,12 +42,15 @@ const region = {
|
|||||||
style={styles.map}
|
style={styles.map}
|
||||||
region={region}
|
region={region}
|
||||||
>
|
>
|
||||||
{chantiers.map(chantier => (
|
{Array.isArray(chantiers) &&
|
||||||
<Marker
|
chantiers.map(chantier => (
|
||||||
key={chantier.id}
|
<Marker
|
||||||
coordinate={{ latitude: chantier.latitude, longitude: chantier.longitude }}
|
key = {chantier.id}
|
||||||
/>
|
coordinate={{ latitude: chantier.latitude, longitude: chantier.longitude }}
|
||||||
))}
|
title={chantier.adresse}
|
||||||
|
description={chantier.etat}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
</ThemedMapView>
|
</ThemedMapView>
|
||||||
</View>
|
</View>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
import { User } from "@/class/class";
|
import { User as FirebaseUser } from "firebase/auth";
|
||||||
import { createContext, ReactNode, useContext, useMemo, useState } from "react";
|
import { createContext, ReactNode, useContext, useMemo, useState } from "react";
|
||||||
|
|
||||||
type UserContextType = {
|
type UserContextType = {
|
||||||
user: User[];
|
user: FirebaseUser | null;
|
||||||
setUser: (list: User[]) => void;
|
role: string | null;
|
||||||
|
setUser: (user: FirebaseUser | null) => void;
|
||||||
|
setRole: (role: string | null) => void;
|
||||||
};
|
};
|
||||||
|
|
||||||
const UserContext = createContext<UserContextType | null>(null);
|
const UserContext = createContext<UserContextType | null>(null);
|
||||||
@@ -13,19 +15,20 @@ type UserProviderProps = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const UserProvider = ({ children }: UserProviderProps) => {
|
export const UserProvider = ({ children }: UserProviderProps) => {
|
||||||
const [user, setUser] = useState<User[]>([]);
|
const [user, setUser] = useState<FirebaseUser | null>(null);
|
||||||
|
const [role, setRole] = useState<string | null>(null);
|
||||||
|
|
||||||
const value = useMemo(() => ({ user, setUser }), [user]);
|
const value = useMemo(
|
||||||
|
() => ({ user, role, setUser, setRole }),
|
||||||
return (
|
[user, role]
|
||||||
<UserContext.Provider value={value}>
|
|
||||||
{children}
|
|
||||||
</UserContext.Provider>
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
return <UserContext.Provider value={value}>{children}</UserContext.Provider>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useUser = () => {
|
export const useUser = () => {
|
||||||
const context = useContext(UserContext);
|
const context = useContext(UserContext);
|
||||||
|
|
||||||
if (!context) {
|
if (!context) {
|
||||||
throw new Error("useUser doit être utilisé dans <UserProvider>");
|
throw new Error("useUser doit être utilisé dans <UserProvider>");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import { auth, db } from "../firebase_config";
|
|||||||
|
|
||||||
import { Platform, UIManager } from 'react-native';
|
import { Platform, UIManager } from 'react-native';
|
||||||
import { ChantierProvider } from "./ContextChantier";
|
import { ChantierProvider } from "./ContextChantier";
|
||||||
|
import { UserProvider } from "./ContextUser";
|
||||||
|
|
||||||
|
|
||||||
export const unstable_settings = {
|
export const unstable_settings = {
|
||||||
@@ -29,8 +30,9 @@ export default function RootLayout() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = onAuthStateChanged(auth, async (currentUser) => {
|
const unsubscribe = onAuthStateChanged(auth, async (currentUser) => {
|
||||||
if (!currentUser) {
|
if (!currentUser) {
|
||||||
router.replace("/login/login");
|
|
||||||
setUser(null);
|
setUser(null);
|
||||||
|
setUserRole(null);
|
||||||
|
router.replace("/login/login");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,40 +42,34 @@ export default function RootLayout() {
|
|||||||
if (!userDoc.exists()) {
|
if (!userDoc.exists()) {
|
||||||
router.replace("/login/login");
|
router.replace("/login/login");
|
||||||
setUser(null);
|
setUser(null);
|
||||||
|
setUserRole(null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { role } = userDoc.data();
|
const { role } = userDoc.data();
|
||||||
setUser(currentUser);
|
setUser(currentUser);
|
||||||
setUserRole(role);
|
setUserRole(role);
|
||||||
|
router.replace("/(tabs)/home");
|
||||||
if (role === "chef") {
|
|
||||||
router.replace("/(tabs)");
|
|
||||||
} else if (role === "resp") {
|
|
||||||
router.replace("/(tabs)");
|
|
||||||
} else if (role === "ouvrier") {
|
|
||||||
router.replace("/(tabs)");
|
|
||||||
} else {
|
|
||||||
router.replace("/login/login");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return unsubscribe;
|
return unsubscribe;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChantierProvider>
|
<UserProvider>
|
||||||
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
|
<ChantierProvider>
|
||||||
<Stack>
|
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
|
||||||
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
<Stack>
|
||||||
<Stack.Screen name="selectChantier" options={{ headerShown: false }}/>
|
<Stack.Screen name="(tabs)" options={{ headerShown: false }} />
|
||||||
<Stack.Screen
|
<Stack.Screen name="selectChantier" options={{ headerShown: false }}/>
|
||||||
name="modal"
|
<Stack.Screen
|
||||||
options={{ presentation: "modal", title: "Modal" }}
|
name="modal"
|
||||||
/>
|
options={{ presentation: "modal", title: "Modal" }}
|
||||||
<Stack.Screen name="login" options={{ headerShown: false }} />
|
/>
|
||||||
</Stack>
|
<Stack.Screen name="login" options={{ headerShown: false }} />
|
||||||
<StatusBar style="auto" />
|
</Stack>
|
||||||
</ThemeProvider>
|
<StatusBar style="auto" />
|
||||||
</ChantierProvider>
|
</ThemeProvider>
|
||||||
|
</ChantierProvider>
|
||||||
|
</UserProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user