This commit is contained in:
shamsmosowi
2021-11-03 19:13:41 +11:00
parent b128c9b17c
commit f10eb6e65a
4 changed files with 53 additions and 43 deletions

View File

@@ -28,11 +28,11 @@ export default function FinalColumn({ row }: FormatterProps<any, any>) {
useStyles();
const { requestConfirmation } = useConfirmation();
const { tableActions, addRow } = useProjectContext();
const { deleteRow, addRow } = useProjectContext();
const altPress = useKeyPress("Alt");
const handleDelete = () => tableActions!.row.delete(row.id);
const handleDelete = () => {
if (deleteRow) deleteRow(row.id);
};
return (
<Stack direction="row" spacing={0.5}>
<Tooltip title="Duplicate row">
@@ -48,7 +48,7 @@ export default function FinalColumn({ row }: FormatterProps<any, any>) {
Object.keys(clonedRow).forEach((key) => {
if (clonedRow[key] === undefined) delete clonedRow[key];
});
if (tableActions) addRow!(clonedRow);
if (addRow) addRow!(clonedRow);
}}
aria-label="Duplicate row"
className="row-hover-iconButton"
@@ -61,7 +61,6 @@ export default function FinalColumn({ row }: FormatterProps<any, any>) {
<IconButton
size="small"
color="inherit"
disabled={!tableActions}
onClick={
altPress
? handleDelete

View File

@@ -44,6 +44,7 @@ interface IProjectContext {
tableState: TableState;
tableActions: TableActions;
addRow: (data?: Record<string, any>, ignoreRequiredFields?: boolean) => void;
deleteRow: (rowId) => void;
updateCell: (
ref: firebase.firestore.DocumentReference,
fieldName: string,
@@ -137,7 +138,28 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
: [],
[tables]
);
const auditChange = (
type: "ADD_ROW" | "UPDATE_CELL" | "DELETE_ROW",
rowId,
data
) => {
if (table?.audit !== false) {
_rowyRun({
route: runRoutes.auditChange,
body: {
rowyUser: rowyUser(currentUser!),
type,
ref: {
rowPath: tableState.tablePath,
rowId,
tableId: table?.id,
collectionPath: tableState.tablePath,
},
data,
},
});
}
};
const addRow: IProjectContext["addRow"] = (data, ignoreRequiredFields) => {
const valuesFromFilter = tableState.filters.reduce((acc, curr) => {
if (curr.operator === "==") {
@@ -170,19 +192,12 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
initialData[table?.auditFieldUpdatedBy || "_updatedBy"] = rowyUser(
currentUser!
);
// _rowyRun({route:runRoutes.auditChange,body:{
// rowyUser,
// eventType:"ADD_ROW",
// eventData:{
// rowPath:ref.path,
// tableId:table?.id,
// }
// }})
}
tableActions.row.add(
{ ...valuesFromFilter, ...initialData, ...data },
ignoreRequiredFields ? [] : requiredFields
ignoreRequiredFields ? [] : requiredFields,
(rowId: string) => auditChange("ADD_ROW", rowId, {})
);
};
@@ -197,26 +212,16 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
const update = { [fieldName]: value };
if (table?.audit !== false) {
const _rowyUser = rowyUser(currentUser!, { updatedField: fieldName });
update[table?.auditFieldUpdatedBy || "_updatedBy"] = _rowyUser;
_rowyRun({
route: runRoutes.auditChange,
body: {
rowyUser: _rowyUser,
eventType: "UPDATE_CELL",
eventData: {
rowPath: ref.path,
tableId: table?.id,
updatedField: fieldName,
},
},
});
update[table?.auditFieldUpdatedBy || "_updatedBy"] = rowyUser(
currentUser!,
{ updatedField: fieldName }
);
}
tableActions.row.update(
ref,
update,
() => {
auditChange("UPDATE_CELL", ref.id, { updatedField: fieldName });
if (onSuccess) onSuccess(ref, fieldName, value);
},
(error) => {
@@ -232,6 +237,10 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
}
);
};
const deleteRow = (rowId) => {
tableActions.row.delete(rowId, () => auditChange("DELETE_ROW", rowId, {}));
};
// rowyRun access
const _rowyRun: IProjectContext["rowyRun"] = async (args) => {
const authToken = await getAuthToken();
@@ -272,6 +281,7 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
tableActions,
addRow,
updateCell,
deleteRow,
settingsActions,
settings: settings.doc,
roles,

View File

@@ -7,14 +7,6 @@ import githubLogo from "@src/assets/logos/github.svg";
import appleLogo from "@src/assets/logos/apple.svg";
import yahooLogo from "@src/assets/logos/yahoo.svg";
import { mdiGoogle } from "@mdi/js";
console.log(
`data:image/svg+xml;utf8,` +
encodeURIComponent(
`<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="${mdiGoogle}" /></svg>`
)
);
export const authOptions = {
google: {
provider: firebase.auth.GoogleAuthProvider.PROVIDER_ID,

View File

@@ -231,12 +231,12 @@ const useTableData = () => {
* @param rowIndex local position
* @param documentId firestore document id
*/
const deleteRow = (rowId: string) => {
const deleteRow = async (rowId: string, onSuccess: () => void) => {
// Remove row locally
rowsDispatch({ type: "delete", rowId });
// Delete document
try {
db.collection(tableState.path).doc(rowId).delete();
await db.collection(tableState.path).doc(rowId).delete().then(onSuccess);
} catch (error: any) {
console.log(error);
if (error.code === "permission-denied") {
@@ -264,7 +264,11 @@ const useTableData = () => {
/** creating new document/row
* @param data(optional: default will create empty row)
*/
const addRow = async (data: any, requiredFields: string[]) => {
const addRow = (
data: any,
requiredFields: string[],
onSuccess: (rowId: string) => void
) => {
const missingRequiredFields = requiredFields
? requiredFields.reduce(missingFieldsReducer(data), [])
: [];
@@ -274,7 +278,12 @@ const useTableData = () => {
if (missingRequiredFields.length === 0) {
try {
await db.collection(path).doc(newId).set(data, { merge: true });
db.collection(path)
.doc(newId)
.set(data, { merge: true })
.then(() => {
onSuccess(newId);
});
} catch (error: any) {
if (error.code === "permission-denied") {
enqueueSnackbar("You do not have the permissions to add new rows.", {