-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
988c380
commit fef438c
Showing
9 changed files
with
268 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ReactNode, createContext, useContext } from 'react' | ||
|
||
const AdminContext = createContext(false) | ||
|
||
export const AdminProvider = ({ | ||
children, | ||
isAdmin, | ||
}: { | ||
children: ReactNode | ||
isAdmin: boolean | ||
}) => <AdminContext.Provider value={isAdmin}>{children}</AdminContext.Provider> | ||
|
||
export const useAdminContext = () => useContext(AdminContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import cookie from 'cookie' | ||
import { ReactElement, useState } from 'react' | ||
import { GetServerSideProps } from 'next' | ||
import { serverSideTranslations } from 'next-i18next/serverSideTranslations' | ||
import { useTranslation } from 'next-i18next' | ||
import { Flex } from '@chakra-ui/react' | ||
import { useRouter } from 'next/router' | ||
import { ObituaryGrid } from 'components/ObituaryGrid' | ||
import { AdminProvider } from 'contexts/AdminContext' | ||
import { useObituaries } from 'hooks/reactQuery/queries' | ||
import { ProgressBar } from 'components/ProgessBar' | ||
import { Contained } from 'components/Contained/Contained' | ||
import { SearchInput } from 'components/SearchInput' | ||
|
||
interface Props {} | ||
|
||
export default function Admin(): ReactElement { | ||
const { t } = useTranslation() | ||
const { query: routerQuery } = useRouter() | ||
const [query, setQuery] = useState((routerQuery?.search as string) ?? '') | ||
const { | ||
isLoading, | ||
isFetchingNextPage, | ||
hasNextPage, | ||
isFetching, | ||
fetchNextPage, | ||
data, | ||
} = useObituaries({ query }) | ||
|
||
return ( | ||
<AdminProvider isAdmin> | ||
<Flex backgroundColor="gray.300"> | ||
<Contained | ||
display="flex" | ||
justifyContent="center" | ||
height={32} | ||
py={[2, 4, 6]} | ||
alignItems="center" | ||
> | ||
<SearchInput | ||
title={t('search')} | ||
value={query} | ||
onChange={setQuery} | ||
placeholder={t('search-placeholder')} | ||
/> | ||
</Contained> | ||
</Flex> | ||
<ProgressBar show={isFetching} /> | ||
<ObituaryGrid | ||
isLoading={isLoading} | ||
isLoadingNext={isFetchingNextPage} | ||
obituaries={data.pages.flatMap((page) => page.data)} | ||
hasMore={hasNextPage} | ||
onLoadMore={fetchNextPage} | ||
/> | ||
</AdminProvider> | ||
) | ||
} | ||
|
||
export const getServerSideProps: GetServerSideProps<Props> = async ({ | ||
locale, | ||
query, | ||
req, | ||
res, | ||
}) => { | ||
if ( | ||
req.cookies?.['admin-session'] === 'true' || | ||
query?.secret === process.env.ADMIN_SECRET | ||
) { | ||
res.setHeader( | ||
'Set-Cookie', | ||
cookie.serialize('admin-session', 'true', { | ||
httpOnly: true, | ||
secure: true, | ||
expires: new Date(Date.now() + 1000 * 15 * 60), | ||
path: '/', | ||
sameSite: 'strict', | ||
}) | ||
) | ||
return { | ||
props: { | ||
...(await serverSideTranslations(locale, ['common'])), | ||
}, | ||
} | ||
} | ||
return { | ||
notFound: true, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import cookie from 'cookie' | ||
import { NextApiRequest, NextApiResponse } from 'next' | ||
|
||
export default async ( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
): Promise<void> => { | ||
switch (req.method) { | ||
case 'GET': { | ||
try { | ||
res.setHeader( | ||
'Set-Cookie', | ||
cookie.serialize('admin-session', '', { | ||
httpOnly: true, | ||
secure: true, | ||
sameSite: 'strict', | ||
path: '/', | ||
expires: new Date(0), | ||
}) | ||
) | ||
res.status(200).end() | ||
return | ||
} catch (err) { | ||
console.error(err) | ||
res.status(400).end() | ||
return | ||
} | ||
} | ||
default: { | ||
res.status(404).end() | ||
return | ||
} | ||
} | ||
} |
Oops, something went wrong.
fef438c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
preminuli – ./
preminuli.ba
preminuli-the-kwisatz-haderach.vercel.app
preminuli-git-master-the-kwisatz-haderach.vercel.app
osmrtnice.vercel.app
www.preminuli.ba