diff --git a/media_commons_booking_app/src/client/admin-page/components/Admin.tsx b/media_commons_booking_app/src/client/admin-page/components/Admin.tsx index 4e7194e7..aa3f448b 100644 --- a/media_commons_booking_app/src/client/admin-page/components/Admin.tsx +++ b/media_commons_booking_app/src/client/admin-page/components/Admin.tsx @@ -4,97 +4,152 @@ import React, { useState, useEffect } from 'react'; import { SafetyTraining } from './SafetyTraining'; import { Ban } from './Ban'; -import { AdminUsers } from './AdminUsers'; +import { ADMIN_USER_SHEET_NAME, AdminUser, AdminUsers } from './AdminUsers'; import { Liaisons } from './Liaisons'; import { Bookings } from './Bookings'; import { PAUsers } from './PAUsers'; +import { serverFunctions } from '../../utils/serverFunctions'; +import { Loading } from '../../utils/Loading'; const Admin = () => { const [tab, setTab] = useState('bookings'); + const [adminUsers, setAdminUsers] = useState([]); + const [adminEmails, setAdminEmails] = useState([]); + const [userEmail, setUserEmail] = useState(); + + useEffect(() => { + fetchAdminUsers(); + getActiveUserEmail(); + }, []); + const getActiveUserEmail = () => { + serverFunctions.getActiveUserEmail().then((response) => { + console.log('userEmail response', response); + setUserEmail(response); + }); + }; + useEffect(() => { + const mappings = adminUsers + .map((adminUser, index) => { + if (index !== 0) { + return mappingAdminUserRows(adminUser); + } + }) + .filter((adminUser) => adminUser !== undefined); + const emails = mappings.map((mapping) => { + return mapping.email; + }); + setAdminEmails(emails); + }, [adminUsers]); + + const fetchAdminUsers = async () => { + serverFunctions.fetchRows(ADMIN_USER_SHEET_NAME).then((rows) => { + setAdminUsers(rows); + }); + }; + + const mappingAdminUserRows = (values: string[]): AdminUser => { + return { + email: values[0], + createdAt: values[1], + }; + }; + const userHasPermission = adminEmails.includes(userEmail); + if (adminEmails.length === 0 || userEmail === null) { + return ; + } + return (
- - {tab === 'safety_training' && } - {tab === 'ban' && } - {tab === 'adminUsers' && } - {tab === 'paUsers' && } - {tab === 'liaesons' && } - {tab === 'bookings' && } + {!userHasPermission ? ( +
+ You do not have permission to view this page. +
+ ) : ( +
+ + {tab === 'safety_training' && } + {tab === 'ban' && } + {tab === 'adminUsers' && } + {tab === 'paUsers' && } + {tab === 'liaesons' && } + {tab === 'bookings' && } +
+ )}
); }; diff --git a/media_commons_booking_app/src/client/admin-page/components/AdminUsers.tsx b/media_commons_booking_app/src/client/admin-page/components/AdminUsers.tsx index 9fe10906..82835de7 100644 --- a/media_commons_booking_app/src/client/admin-page/components/AdminUsers.tsx +++ b/media_commons_booking_app/src/client/admin-page/components/AdminUsers.tsx @@ -5,9 +5,9 @@ import { serverFunctions } from '../../utils/serverFunctions'; import { formatDate } from '../../utils/date'; import { Loading } from '../../utils/Loading'; -const ADMIN_USER_SHEET_NAME = 'admin_users'; +export const ADMIN_USER_SHEET_NAME = 'admin_users'; -type AdminUser = { +export type AdminUser = { email: string; createdAt: string; };