Merge pull request #283 from AntlerVC/develop

check for basic permissions on the home page
This commit is contained in:
Shams
2020-12-22 15:33:49 +08:00
committed by GitHub
2 changed files with 43 additions and 21 deletions

View File

@@ -12,31 +12,35 @@ const documentInitialState = {
doc: null,
ref: null,
loading: true,
};
const documentReducer = (prevState: any, newProps: any) => {
switch (newProps.action) {
case DocActions.clear:
return documentInitialState;
case DocActions.update:
// takes data object form the dispatcher and updates doc
prevState.ref.set({ ...newProps.data }, { merge: true });
return { ...prevState, doc: { ...prevState.doc, ...newProps.data } };
case DocActions.delete:
prevState.ref.delete();
return null;
default:
return { ...prevState, ...newProps };
}
error:null
};
const useDoc = (initialOverrides: any) => {
const documentReducer = (prevState: any, newProps: any) => {
switch (newProps.action) {
case DocActions.clear:
return documentInitialState;
case DocActions.update:
// takes data object form the dispatcher and updates doc
(prevState.ref?prevState.ref:db.doc(prevState.path)).set({ ...newProps.data }, { merge: true }).then((result: any) => {
}).catch((error)=>{
console.log(error);
documentDispatch({error})
});
return { ...prevState, doc: { ...prevState.doc, ...newProps.data } };
case DocActions.delete:
prevState.ref.delete();
return null;
default:
return { ...prevState, ...newProps };
}
};
const [documentState, documentDispatch] = useReducer(documentReducer, {
...documentInitialState,
...initialOverrides,
});
const setDocumentListner = () => {
documentDispatch({ prevPath: documentState.path });
const unsubscribe = db.doc(documentState.path).onSnapshot((snapshot) => {
@@ -55,6 +59,11 @@ const useDoc = (initialOverrides: any) => {
loading: false,
});
}
},(error)=>{
documentDispatch({
loading: false,
error
});
});
documentDispatch({ unsubscribe });
};

View File

@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState ,useEffect} from "react";
import _groupBy from "lodash/groupBy";
import _find from "lodash/find";
@@ -26,11 +26,11 @@ import StyledCard from "components/StyledCard";
import routes from "constants/routes";
import { useFiretableContext } from "contexts/FiretableContext";
import { useAppContext } from "contexts/AppContext";
import { DocActions } from "hooks/useDoc";
import useDoc,{ DocActions } from "hooks/useDoc";
import TableSettingsDialog, {
TableSettingsDialogModes,
} from "components/TableSettings";
import EmptyState from 'components/EmptyState';
const useStyles = makeStyles((theme) =>
createStyles({
"@global": {
@@ -123,6 +123,19 @@ export default function HomePage() {
const [open, setOpen] = useState(false);
const [settingsDocState,settingsDocDispatch] = useDoc({path:'_FIRETABLE_/settings'})
useEffect(() => {
if(!settingsDocState.loading && !settingsDocState.doc){
settingsDocDispatch({action:DocActions.update,data:{createdAt:new Date()}});
}
},[settingsDocState])
if (settingsDocState.error?.code === "permission-denied") {
return <EmptyState fullScreen message="Access Denied" description={<><Typography variant="overline">You don't current have access to firetable, please contact this project's owner</Typography>
<Typography variant='body2'>If you are the project owner please follow the instructions <a href="https://github.com/AntlerVC/firetable/wiki/Role-Based-Security-Rules" target="_blank">here</a> to setup the project rules.</Typography>
</>}/>
}
const TableCard = ({ table }) => {
const checked = Boolean(_find(favs, table));
return (