-
Notifications
You must be signed in to change notification settings - Fork 458
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
Tarik Huber
authored and
Tarik Huber
committed
Jul 25, 2023
1 parent
0c03e2e
commit 503a76e
Showing
3 changed files
with
60 additions
and
50 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,78 +1,88 @@ | ||
import Delete from '@mui/icons-material/Delete' | ||
import IconButton from '@mui/material/IconButton' | ||
import Page from 'material-ui-shell/lib/containers/Page' | ||
import React, { useState } from 'react' | ||
import Save from '@mui/icons-material/Save' | ||
import { useNavigate } from 'react-router-dom' | ||
import { usePaths } from '../../providers/Firebase/Paths' | ||
import { useQuestions } from 'material-ui-shell/lib/providers/Dialogs/Question' | ||
import { useAuth } from 'base-shell/lib/providers/Auth' | ||
import FirebaseForm from '../../containers/FirebaseForm' | ||
import { getDatabase, ref, set } from 'firebase/database' | ||
import Delete from "@mui/icons-material/Delete"; | ||
import IconButton from "@mui/material/IconButton"; | ||
import Page from "material-ui-shell/lib/containers/Page"; | ||
import React, { useState } from "react"; | ||
import Save from "@mui/icons-material/Save"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { usePaths } from "../../providers/Firebase/Paths"; | ||
import { useQuestions } from "material-ui-shell/lib/providers/Dialogs/Question"; | ||
import { useAuth } from "base-shell/lib/providers/Auth"; | ||
import FirebaseForm from "../../containers/FirebaseForm"; | ||
import { getDatabase, ref, set } from "firebase/database"; | ||
|
||
export default function FormPage(props) { | ||
const { | ||
uid, | ||
path = 'none', | ||
path = "none", | ||
getPageProps = () => {}, | ||
handleDelete = () => {}, | ||
deleteDialogProps = {}, | ||
grants = {}, | ||
initialValues = {}, | ||
} = props | ||
const navigate = useNavigate() | ||
const { openDialog } = useQuestions() | ||
const { getPath } = usePaths() | ||
const { auth } = useAuth() | ||
const [submit, setSubmit] = useState(false) | ||
const db = getDatabase() | ||
const { isGranted = () => false } = auth || {} | ||
useSave = true, | ||
useDelete = true, | ||
alwaysAllowSave = false, | ||
alwaysAllowDelete = false, | ||
} = props; | ||
const navigate = useNavigate(); | ||
const { openDialog } = useQuestions(); | ||
const { getPath } = usePaths(); | ||
const { auth } = useAuth(); | ||
const [submit, setSubmit] = useState(false); | ||
const db = getDatabase(); | ||
const { isGranted = () => false } = auth || {}; | ||
|
||
const databasePath = `${path}/${uid}` | ||
const data = getPath(databasePath, {}) || initialValues | ||
const databasePath = `${path}/${uid}`; | ||
const data = getPath(databasePath, {}) || initialValues; | ||
|
||
const openDeleteDialog = () => { | ||
openDialog({ | ||
handleAction: async (handleClose) => { | ||
await set(ref(db, `${path}/${uid}`), null) | ||
handleClose() | ||
handleDelete() | ||
await set(ref(db, `${path}/${uid}`), null); | ||
handleClose(); | ||
handleDelete(); | ||
}, | ||
...deleteDialogProps, | ||
}) | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<Page | ||
onBackClick={() => { | ||
navigate(-1) | ||
navigate(-1); | ||
}} | ||
appBarContent={ | ||
<div> | ||
<IconButton | ||
disabled={!isGranted(auth, grants.create)} | ||
color="inherit" | ||
onClick={(e) => { | ||
submit(e) | ||
}} | ||
> | ||
<Save /> | ||
</IconButton> | ||
{useSave && ( | ||
<IconButton | ||
disabled={!isGranted(auth, grants.create) && !alwaysAllowSave} | ||
color="inherit" | ||
onClick={(e) => { | ||
submit(e); | ||
}} | ||
> | ||
<Save /> | ||
</IconButton> | ||
)} | ||
|
||
<IconButton | ||
disabled={!uid || !isGranted(auth, grants.delete)} | ||
color="inherit" | ||
onClick={() => { | ||
openDeleteDialog() | ||
}} | ||
> | ||
<Delete /> | ||
</IconButton> | ||
{useDelete && ( | ||
<IconButton | ||
disabled={ | ||
!uid || (!isGranted(auth, grants.delete) && !alwaysAllowDelete) | ||
} | ||
color="inherit" | ||
onClick={() => { | ||
openDeleteDialog(); | ||
}} | ||
> | ||
<Delete /> | ||
</IconButton> | ||
)} | ||
</div> | ||
} | ||
{...getPageProps({ values: data, submit })} | ||
> | ||
<FirebaseForm setSubmit={setSubmit} submit={submit} {...props} /> | ||
</Page> | ||
) | ||
); | ||
} |