sveltedfire 0.1.28 → 0.1.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { handleForm } from "../utilities/handleForm.js";
|
|
3
3
|
|
|
4
|
-
let { collectionName, docId = null, docIdField = null, onSubmit, children, beforeSubmit = null, formRef = $bindable(), forceNew = false, ...rest } = $props()
|
|
4
|
+
let { collectionName, docId = null, docIdField = null, onSubmit, children, beforeSubmit = null, formRef = $bindable(), forceNew = false, useDottedNotation = false, ...rest } = $props()
|
|
5
5
|
|
|
6
|
-
const submitTheForm = handleForm({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew })
|
|
6
|
+
const submitTheForm = handleForm({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew, useDottedNotation })
|
|
7
7
|
</script>
|
|
8
8
|
|
|
9
9
|
<form onsubmit={submitTheForm} bind:this={formRef} {...rest}>
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
export declare const handleForm: ({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew }: {
|
|
1
|
+
export declare const handleForm: ({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew, useDottedNotation }: {
|
|
2
2
|
collectionName: string;
|
|
3
3
|
docId?: null | string;
|
|
4
4
|
docIdField?: null | string;
|
|
5
5
|
onSubmit: any;
|
|
6
6
|
beforeSubmit?: any;
|
|
7
7
|
forceNew?: boolean;
|
|
8
|
+
useDottedNotation?: boolean;
|
|
8
9
|
}) => (ev: SubmitEvent) => Promise<void>;
|
|
@@ -1,5 +1,47 @@
|
|
|
1
1
|
import { getFirestore, collection, doc, getDoc, updateDoc, setDoc, addDoc, deleteField } from "firebase/firestore";
|
|
2
|
-
|
|
2
|
+
const isLiteralObject = (a) => (!!a) && (a.constructor === Object);
|
|
3
|
+
const cleanArrays = (obj) => {
|
|
4
|
+
if (Array.isArray(obj)) {
|
|
5
|
+
return obj.filter(k => JSON.stringify(k) !== JSON.stringify(deleteField())); // Raw comparison does not work
|
|
6
|
+
}
|
|
7
|
+
else {
|
|
8
|
+
if (isLiteralObject(obj)) {
|
|
9
|
+
Object.keys(obj).forEach(k => {
|
|
10
|
+
obj[k] = cleanArrays(obj[k]);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return obj;
|
|
15
|
+
};
|
|
16
|
+
const convertToDotted = (data) => {
|
|
17
|
+
let assembler = {};
|
|
18
|
+
Object.keys(data).forEach(k => {
|
|
19
|
+
if (isLiteralObject(data[k])) {
|
|
20
|
+
let subdata = convertToDotted(data[k]);
|
|
21
|
+
Object.keys(subdata).forEach(j => {
|
|
22
|
+
assembler[`${k}.${j}`] = subdata[j];
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
else if (Array.isArray(data[k])) {
|
|
26
|
+
data[k].forEach((val, idx) => {
|
|
27
|
+
if (isLiteralObject(val)) {
|
|
28
|
+
let subdata = convertToDotted(val);
|
|
29
|
+
Object.keys(subdata).forEach(j => {
|
|
30
|
+
assembler[`${k}.${idx}.${j}`] = subdata[j];
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
assembler[`${k}.${idx}`] = val;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
assembler[k] = data[k];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return assembler;
|
|
43
|
+
};
|
|
44
|
+
export const handleForm = ({ collectionName, docId = null, docIdField = null, onSubmit, beforeSubmit = null, forceNew = false, useDottedNotation = false }) => async (ev) => {
|
|
3
45
|
const db = getFirestore();
|
|
4
46
|
console.log('Attempting to save');
|
|
5
47
|
ev.preventDefault();
|
|
@@ -59,21 +101,10 @@ export const handleForm = ({ collectionName, docId = null, docIdField = null, on
|
|
|
59
101
|
delete objData[k];
|
|
60
102
|
}
|
|
61
103
|
});
|
|
62
|
-
const isLiteralObject = (a) => (!!a) && (a.constructor === Object);
|
|
63
|
-
const cleanArrays = (obj) => {
|
|
64
|
-
if (Array.isArray(obj)) {
|
|
65
|
-
return obj.filter(k => JSON.stringify(k) !== JSON.stringify(deleteField())); // Raw comparison does not work
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
if (isLiteralObject(obj)) {
|
|
69
|
-
Object.keys(obj).forEach(k => {
|
|
70
|
-
obj[k] = cleanArrays(obj[k]);
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return obj;
|
|
75
|
-
};
|
|
76
104
|
objData = cleanArrays(objData);
|
|
105
|
+
if (useDottedNotation) {
|
|
106
|
+
objData = convertToDotted(objData);
|
|
107
|
+
}
|
|
77
108
|
console.log('Object data is', objData);
|
|
78
109
|
let docRefId;
|
|
79
110
|
if (docId || docIdField) {
|