-
Notifications
You must be signed in to change notification settings - Fork 11
Mui admin app auth #134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mui admin app auth #134
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
"use client"; | ||
import LanguageButtonBar from "@/components/LanguageButtonBar"; | ||
import { Layout } from "@/components/Layout"; | ||
import { FullAccessComponent } from "@/components/ProtectedComponent"; | ||
import { appColors, appStyles, sizes } from "@/utils"; | ||
import { apiCalls } from "@/utils/api"; | ||
import { useAuth } from "@/utils/auth"; | ||
import { ChevronLeft } from "@mui/icons-material"; | ||
import { Button, CircularProgress, TextField, Typography } from "@mui/material"; | ||
import Alert from "@mui/material/Alert"; | ||
|
@@ -29,12 +31,13 @@ const AddEditContentPage = () => { | |
const [content, setContent] = React.useState<Content | null>(null); | ||
const [isLoading, setIsLoading] = React.useState<boolean>(true); | ||
|
||
const { token } = useAuth(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This entire page shouldn't run any code at all (including the .getContent fetch, since it's never shown) if we're readonly and instead display a "Not Authorised" message on the page. How do we also stop the useEffects from running on page load based on auth state? |
||
React.useEffect(() => { | ||
if (!content_id) { | ||
setIsLoading(false); | ||
return; | ||
} else { | ||
apiCalls.getContent(content_id).then((data) => { | ||
apiCalls.getContent(content_id, token!).then((data) => { | ||
setContent(data); | ||
setIsLoading(false); | ||
}); | ||
|
@@ -58,17 +61,19 @@ const AddEditContentPage = () => { | |
); | ||
} | ||
return ( | ||
<Layout.FlexBox flexDirection={"column"} sx={{ p: sizes.doubleBaseGap }}> | ||
<Header content_id={content_id} /> | ||
<Layout.FlexBox | ||
flexDirection={"column"} | ||
sx={{ px: sizes.doubleBaseGap, mx: sizes.smallGap }} | ||
> | ||
<Layout.Spacer multiplier={2} /> | ||
<ContentBox content={content} setContent={setContent} /> | ||
<Layout.Spacer multiplier={1} /> | ||
<FullAccessComponent> | ||
sidravi1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<Layout.FlexBox flexDirection={"column"} sx={{ p: sizes.doubleBaseGap }}> | ||
<Header content_id={content_id} /> | ||
<Layout.FlexBox | ||
flexDirection={"column"} | ||
sx={{ px: sizes.doubleBaseGap, mx: sizes.smallGap }} | ||
> | ||
<Layout.Spacer multiplier={2} /> | ||
<ContentBox content={content} setContent={setContent} /> | ||
<Layout.Spacer multiplier={1} /> | ||
</Layout.FlexBox> | ||
</Layout.FlexBox> | ||
</Layout.FlexBox> | ||
</FullAccessComponent> | ||
); | ||
}; | ||
|
||
|
@@ -84,6 +89,8 @@ const ContentBox = ({ | |
const [isTitleEmpty, setIsTitleEmpty] = React.useState(false); | ||
const [isContentEmpty, setIsContentEmpty] = React.useState(false); | ||
|
||
const { token } = useAuth(); | ||
|
||
const router = useRouter(); | ||
const saveContent = async (content: Content) => { | ||
const body: EditContentBody = { | ||
|
@@ -95,8 +102,8 @@ const ContentBox = ({ | |
|
||
const promise = | ||
content.content_id === null | ||
? apiCalls.addContent(body) | ||
: apiCalls.editContent(content.content_id, body); | ||
? apiCalls.addContent(body, token!) | ||
: apiCalls.editContent(content.content_id, body, token!); | ||
|
||
const result = promise | ||
.then((data) => { | ||
|
@@ -162,7 +169,7 @@ const ContentBox = ({ | |
"& .MuiFormHelperText-root": { | ||
backgroundColor: appColors.lightGrey, | ||
mx: 0, | ||
my: 0, // Set your desired background color here | ||
my: 0, | ||
}, | ||
}} | ||
value={content ? content.content_title : ""} | ||
|
@@ -180,7 +187,7 @@ const ContentBox = ({ | |
"& .MuiFormHelperText-root": { | ||
backgroundColor: appColors.lightGrey, | ||
mx: 0, | ||
my: 0, // Set your desired background color here | ||
my: 0, | ||
}, | ||
}} | ||
label="Content" | ||
|
@@ -234,11 +241,13 @@ const ContentBox = ({ | |
}; | ||
|
||
const Header = ({ content_id }: { content_id: number | null }) => { | ||
const router = useRouter(); | ||
|
||
return ( | ||
<Layout.FlexBox flexDirection="row" {...appStyles.alignItemsCenter}> | ||
<ChevronLeft | ||
style={{ cursor: "pointer" }} | ||
onClick={() => (window.location.href = "/content/")} | ||
amiraliemami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
onClick={() => (content_id ? router.back() : router.push("/content"))} | ||
/> | ||
<Layout.Spacer multiplier={1} horizontal /> | ||
{content_id ? ( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import NavBar from "@/components/NavBar"; | ||
import { ProtectedComponent } from "@/components/ProtectedComponent"; | ||
import React from "react"; | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode; | ||
}>) { | ||
return ( | ||
<ProtectedComponent> | ||
amiraliemami marked this conversation as resolved.
Show resolved
Hide resolved
|
||
<NavBar /> | ||
{children} | ||
</ProtectedComponent> | ||
); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.