From 8e53e8dd4d6aadde8cd6790579481b710b83b2e8 Mon Sep 17 00:00:00 2001 From: Zhang Ziqing <69516975+ziqing26@users.noreply.github.com> Date: Thu, 17 Nov 2022 18:38:24 +0800 Subject: [PATCH] [offers][feat] add admin page to navbar (#553) * [offers][feat] add admin page to navbar * [offers][chore] remove comments --- .../portal/src/components/global/AppShell.tsx | 14 ++++++++-- .../components/offers/OffersNavigation.tsx | 13 +++++++++ .../{admin_temp => admin}/OffersHeader.tsx | 4 +-- .../{admin_temp => admin}/OffersRow.tsx | 0 .../{admin_temp => admin}/OffersTable.tsx | 21 +++++++------- .../OffersTablePagination.tsx | 0 .../offers/{admin_temp => admin}/types.ts | 0 .../offers/{admin_temp.tsx => admin.tsx} | 28 +++++++------------ 8 files changed, 47 insertions(+), 33 deletions(-) rename apps/portal/src/components/offers/{admin_temp => admin}/OffersHeader.tsx (97%) rename apps/portal/src/components/offers/{admin_temp => admin}/OffersRow.tsx (100%) rename apps/portal/src/components/offers/{admin_temp => admin}/OffersTable.tsx (94%) rename apps/portal/src/components/offers/{admin_temp => admin}/OffersTablePagination.tsx (100%) rename apps/portal/src/components/offers/{admin_temp => admin}/types.ts (100%) rename apps/portal/src/pages/offers/{admin_temp.tsx => admin.tsx} (89%) diff --git a/apps/portal/src/components/global/AppShell.tsx b/apps/portal/src/components/global/AppShell.tsx index 6c9a7469..20bd1b53 100644 --- a/apps/portal/src/components/global/AppShell.tsx +++ b/apps/portal/src/components/global/AppShell.tsx @@ -11,11 +11,14 @@ import { Button } from '@tih/ui'; import GlobalNavigation from '~/components/global/GlobalNavigation'; import HomeNavigation from '~/components/global/HomeNavigation'; import OffersNavigation, { + OffersNavigationAdmin, OffersNavigationAuthenticated, } from '~/components/offers/OffersNavigation'; import QuestionsNavigation from '~/components/questions/QuestionsNavigation'; import ResumesNavigation from '~/components/resumes/ResumesNavigation'; +import { trpc } from '~/utils/trpc'; + import GoogleAnalytics from './GoogleAnalytics'; import MobileNavigation from './MobileNavigation'; import type { ProductNavigationItems } from './ProductNavigation'; @@ -131,7 +134,12 @@ export default function AppShell({ children }: Props) { const [mobileMenuOpen, setMobileMenuOpen] = useState(false); const router = useRouter(); const { data: session } = useSession(); - + const { isLoading: isOffersAdminResultsLoading, data: isOffersAdmin } = + trpc.useQuery(['offers.admin.isAdmin'], { + onError: () => { + router.push('/offers'); + }, + }); const currentProductNavigation: Readonly<{ googleAnalyticsMeasurementID: string; logo?: React.ReactNode; @@ -149,7 +157,9 @@ export default function AppShell({ children }: Props) { if (session == null) { return OffersNavigation; } - return OffersNavigationAuthenticated; + return !isOffersAdminResultsLoading && isOffersAdmin + ? OffersNavigationAdmin + : OffersNavigationAuthenticated; } if (path.startsWith('/questions')) { diff --git a/apps/portal/src/components/offers/OffersNavigation.tsx b/apps/portal/src/components/offers/OffersNavigation.tsx index 53e4b4c8..467f7785 100644 --- a/apps/portal/src/components/offers/OffersNavigation.tsx +++ b/apps/portal/src/components/offers/OffersNavigation.tsx @@ -13,6 +13,14 @@ const navigationAuthenticated: ProductNavigationItems = [ { href: '/offers/about', name: 'About' }, ]; +const navigationAdmin: ProductNavigationItems = [ + { href: '/offers/submit', name: 'Analyze your offers' }, + { href: '/offers/dashboard', name: 'My dashboard' }, + { href: '/offers/features', name: 'Features' }, + { href: '/offers/about', name: 'About' }, + { href: '/offers/admin', name: 'Admin' }, +]; + const config = { googleAnalyticsMeasurementID: 'G-34XRGLEVCF', logo: ( @@ -28,6 +36,11 @@ const config = { titleHref: '/offers', }; +export const OffersNavigationAdmin = { + ...config, + navigation: navigationAdmin, +}; + export const OffersNavigationAuthenticated = { ...config, navigation: navigationAuthenticated, diff --git a/apps/portal/src/components/offers/admin_temp/OffersHeader.tsx b/apps/portal/src/components/offers/admin/OffersHeader.tsx similarity index 97% rename from apps/portal/src/components/offers/admin_temp/OffersHeader.tsx rename to apps/portal/src/components/offers/admin/OffersHeader.tsx index 99ce8143..9754178e 100644 --- a/apps/portal/src/components/offers/admin_temp/OffersHeader.tsx +++ b/apps/portal/src/components/offers/admin/OffersHeader.tsx @@ -1,10 +1,10 @@ import clsx from 'clsx'; -import type { OfferTableSortType } from '~/components/offers/admin_temp/types'; +import type { OfferTableSortType } from '~/components/offers/admin/types'; import { getOppositeSortOrder, OFFER_TABLE_SORT_ORDER, -} from '~/components/offers/admin_temp/types'; +} from '~/components/offers/admin/types'; export type OffersTableHeaderProps = Readonly<{ header: string; diff --git a/apps/portal/src/components/offers/admin_temp/OffersRow.tsx b/apps/portal/src/components/offers/admin/OffersRow.tsx similarity index 100% rename from apps/portal/src/components/offers/admin_temp/OffersRow.tsx rename to apps/portal/src/components/offers/admin/OffersRow.tsx diff --git a/apps/portal/src/components/offers/admin_temp/OffersTable.tsx b/apps/portal/src/components/offers/admin/OffersTable.tsx similarity index 94% rename from apps/portal/src/components/offers/admin_temp/OffersTable.tsx rename to apps/portal/src/components/offers/admin/OffersTable.tsx index 56b1d81e..4058907c 100644 --- a/apps/portal/src/components/offers/admin_temp/OffersTable.tsx +++ b/apps/portal/src/components/offers/admin/OffersTable.tsx @@ -4,20 +4,20 @@ import { JobType } from '@prisma/client'; import { DropdownMenu, Spinner, useToast } from '@tih/ui'; import { useGoogleAnalytics } from '~/components/global/GoogleAnalytics'; -import OffersRow from '~/components/offers/admin_temp//OffersRow'; -import OffersHeader from '~/components/offers/admin_temp/OffersHeader'; -import OffersTablePagination from '~/components/offers/admin_temp/OffersTablePagination'; +import OffersHeader from '~/components/offers/admin/OffersHeader'; +import OffersRow from '~/components/offers/admin/OffersRow'; +import OffersTablePagination from '~/components/offers/admin/OffersTablePagination'; import type { OfferTableColumn, OfferTableSortType, -} from '~/components/offers/admin_temp/types'; +} from '~/components/offers/admin/types'; import { FullTimeOfferTableColumns, InternOfferTableColumns, OFFER_TABLE_SORT_ORDER, OfferTableYoeOptions, YOE_CATEGORY_PARAM, -} from '~/components/offers/admin_temp/types'; +} from '~/components/offers/admin/types'; import { getCurrencyForCountry } from '~/utils/offers/currency/CurrencyEnum'; import CurrencySelector from '~/utils/offers/currency/CurrencySelector'; @@ -294,12 +294,11 @@ export default function OffersTable({ )} - {(!isLoading && !offers) || - (offers.length === 0 && ( -
-
No data yet 🥺
-
- ))} + {!isLoading && (!offers || offers.length === 0) && ( +
+
No data yet 🥺
+
+ )} { return { @@ -41,23 +40,16 @@ export default function OffersHomePage({ const [selectedJobTitleId, setSelectedJobTitleId] = useSearchParamSingle('jobTitleId'); - const { data: session, status } = useSession(); - - const authoizedPeople = [ - '8b4550989cb7fe9ea7649b5538178b8d19aba0f3e5944dbff0b8d0e2ffe3911f', - '0544d5d2be7815b5347dd2233c4d08a52120e52ac529f21b1a5c2005db3c42ab', - '9934698c65bc72876018350a02910acdb27b7974dc757a320057588b67c5422b', - '5cd57c9d1cc00d1010c3548ea3941941c04d18f7cf50766cdec30b12630e69ac', - ]; - - const isAuthorized = authoizedPeople.includes( - crypto - .createHash('sha256') - .update(session?.user?.email ?? '') - .digest('hex'), + const { isLoading, data: isAuthorized } = trpc.useQuery( + ['offers.admin.isAdmin'], + { + onError: () => { + router.push('/offers'); + }, + }, ); - if (!isAuthorized && status !== 'loading') { + if (!isLoading && !isAuthorized) { router.push('/offers'); } return (