sveltedfire 0.1.25 → 0.1.27
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(), ...rest } = $props()
|
|
4
|
+
let { collectionName, docId = null, docIdField = null, onSubmit, children, beforeSubmit = null, formRef = $bindable(), forceNew = false, ...rest } = $props()
|
|
5
5
|
|
|
6
|
-
const submitTheForm = handleForm({ collectionName, docId, docIdField, onSubmit, beforeSubmit })
|
|
6
|
+
const submitTheForm = handleForm({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew })
|
|
7
7
|
</script>
|
|
8
8
|
|
|
9
9
|
<form onsubmit={submitTheForm} bind:this={formRef} {...rest}>
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export declare const handleForm: ({ collectionName, docId, docIdField, onSubmit, beforeSubmit }: {
|
|
1
|
+
export declare const handleForm: ({ collectionName, docId, docIdField, onSubmit, beforeSubmit, forceNew }: {
|
|
2
2
|
collectionName: string;
|
|
3
3
|
docId?: null | string;
|
|
4
4
|
docIdField?: null | string;
|
|
5
5
|
onSubmit: any;
|
|
6
6
|
beforeSubmit?: any;
|
|
7
|
+
forceNew?: boolean;
|
|
7
8
|
}) => (ev: SubmitEvent) => Promise<void>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getFirestore, collection, doc, getDoc, updateDoc, setDoc, addDoc, deleteField } from "firebase/firestore";
|
|
2
|
-
export const handleForm = ({ collectionName, docId = null, docIdField = null, onSubmit, beforeSubmit = null }) => async (ev) => {
|
|
2
|
+
export const handleForm = ({ collectionName, docId = null, docIdField = null, onSubmit, beforeSubmit = null, forceNew = false }) => async (ev) => {
|
|
3
3
|
const db = getFirestore();
|
|
4
4
|
console.log('Attempting to save');
|
|
5
5
|
ev.preventDefault();
|
|
@@ -70,11 +70,21 @@ export const handleForm = ({ collectionName, docId = null, docIdField = null, on
|
|
|
70
70
|
console.log('Object data is', objData);
|
|
71
71
|
let docRefId;
|
|
72
72
|
if (docId) {
|
|
73
|
-
|
|
73
|
+
if (forceNew) {
|
|
74
|
+
await setDoc(doc(col, docId), objData);
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
await updateDoc(doc(col, docId), objData);
|
|
78
|
+
}
|
|
74
79
|
docRefId = docId;
|
|
75
80
|
}
|
|
76
81
|
else if (docIdField) {
|
|
77
|
-
|
|
82
|
+
if (forceNew) {
|
|
83
|
+
await setDoc(doc(col, formData.get(docIdField)), objData);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
await updateDoc(doc(col, formData.get(docIdField)), objData);
|
|
87
|
+
}
|
|
78
88
|
docRefId = formData.get(docIdField);
|
|
79
89
|
}
|
|
80
90
|
else {
|