forked from MildTomato/supabase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.js
89 lines (81 loc) · 2.35 KB
/
driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { useContext, useEffect, useState } from "react";
import { AppContext } from "lib/constants";
import { supabase } from "lib/api";
import dynamic from "next/dynamic";
import styles from "styles/Map.module.css";
import SignIn from "components/SignIn";
import PageLayout from "components/PageLayout";
const MapInput = dynamic(() => import("components/MapInput"), { ssr: false });
function randomPosition() {
var array = [
{
lat: 1.3489728457596013,
lng: 103.77043978311998,
},
{
lat: 1.37462,
lng: 103.77694,
},
{
lat: 1.41006,
lng: 103.77144,
},
{
lat: 1.34313,
lng: 103.84191,
},
{
lat: 1.35454,
lng: 103.69746,
},
];
return array[Math.floor(Math.random() * array.length)];
}
const center = randomPosition();
const zoomLevel = 14;
export default function Page() {
const { session } = useContext(AppContext);
const [userRole, setRole] = useState(null);
const [username, setUsername] = useState(null);
useEffect(() => {
setRole(session?.user?.user_metadata?.role);
setUsername(session?.user?.user_metadata?.username);
}, [session]);
async function onSignOut() {
const { error } = await supabase.auth.signOut();
if (error) console.log("Error logging out:", error.message);
}
return (
<PageLayout title="Driver View">
<h1 className={styles.title}>Driver View</h1>
<p className={styles.description}>
Get started by drag-drop the marker around to simulate location update
</p>
<div className={styles.grid}>
{!session && <SignIn />}
{session && (
<div className={styles.card}>
{userRole?.toUpperCase() == "DRIVER" && (
<MapInput
clientRef={session.user?.id}
center={center}
zoom={zoomLevel}
/>
)}
{userRole?.toUpperCase() == "MANAGER" && (
<p className={styles.error}>
Sorry, You need to sign in as driver
</p>
)}
<div className={styles.profile_container}>
Signed in as {username} [{userRole}]<br />
<button className={styles.sign_out} onClick={onSignOut}>
Sign out
</button>
</div>
</div>
)}
</div>
</PageLayout>
);
}