forked from rowyio/rowy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseSettings.ts
120 lines (110 loc) · 3.6 KB
/
useSettings.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { useEffect } from "react";
import _findIndex from "lodash/findIndex";
import _camelCase from "lodash/camelCase";
import useDoc from "./useDoc";
import { db } from "../firebase";
import {
SETTINGS,
TABLE_GROUP_SCHEMAS,
TABLE_SCHEMAS,
} from "@src/config/dbPaths";
import { FieldType } from "@src/constants/fields";
import { getFieldProp } from "@src/components/fields";
export default function useSettings() {
const [settingsState, documentDispatch] = useDoc({ path: SETTINGS });
useEffect(() => {
//updates tables data on document change
const { doc, tables } = settingsState;
if (doc && tables !== doc.tables) {
// const sections = _groupBy(
// tables.filter(
// table =>
// !table.roles || table.roles.some(role => userRoles.includes(role))
// ),
// "section"
// );
documentDispatch({ tables: doc.tables, roles: doc.roles });
}
}, [settingsState]);
const createTable = async (data: {
id: string;
name: string;
collection: string;
description: string;
tableType: string;
roles: string[];
schemaSource: any;
_initialColumns: Record<FieldType, boolean>;
}) => {
const { tables } = settingsState;
const { schemaSource, ...tableSettings } = data;
const tableSchemaPath = `${
tableSettings.tableType !== "collectionGroup"
? TABLE_SCHEMAS
: TABLE_GROUP_SCHEMAS
}/${tableSettings.id}`;
const tableSchemaDocRef = db.doc(tableSchemaPath);
// Get columns from schemaSource if provided
let columns: Record<string, any> = {};
if (schemaSource) {
const schemaSourcePath = `${
tableSettings.tableType !== "collectionGroup"
? TABLE_SCHEMAS
: TABLE_GROUP_SCHEMAS
}/${schemaSource.id}`;
const sourceDoc = await db.doc(schemaSourcePath).get();
columns = sourceDoc.get("columns");
}
// Add columns from `_initialColumns`
for (const [type, checked] of Object.entries(data._initialColumns)) {
if (
checked &&
!Object.values(columns).some((column) => column.type === type)
)
columns["_" + _camelCase(type)] = {
type,
name: getFieldProp("name", type as FieldType),
key: "_" + _camelCase(type),
fieldName: "_" + _camelCase(type),
config: {},
index: Object.values(columns).length,
};
}
// Appends table to settings doc
await db.doc(SETTINGS).set(
{
tables: Array.isArray(tables)
? [...tables, tableSettings]
: [tableSettings],
},
{ merge: true }
);
// Creates schema doc with columns
await tableSchemaDocRef.set({ columns }, { merge: true });
};
const updateTable = async (data: {
id: string;
name?: string;
collection?: string;
section?: string;
description?: string;
roles?: string[];
[key: string]: any;
}) => {
const { tables } = settingsState;
const newTables = Array.isArray(tables) ? [...tables] : [];
const foundIndex = _findIndex(newTables, { id: data.id });
const tableIndex = foundIndex > -1 ? foundIndex : tables.length;
newTables[tableIndex] = { ...newTables[tableIndex], ...data };
await db.doc(SETTINGS).set({ tables: newTables }, { merge: true });
};
const deleteTable = (id: string) => {
const { tables } = settingsState;
db.doc(SETTINGS).update({
tables: tables.filter((table) => table.id !== id),
});
db.collection(TABLE_SCHEMAS).doc(id).delete();
};
const settingsActions = { createTable, updateTable, deleteTable };
return [settingsState, settingsActions];
}