fix: fix history not writing when formdata is present

This commit is contained in:
Andrew Bastin
2021-11-11 18:36:26 +05:30
parent 521a96bffb
commit 25878b9bb1
3 changed files with 61 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ import {
query,
updateDoc,
} from "firebase/firestore"
import { FormDataKeyValue } from "../types/HoppRESTRequest"
import { currentUser$ } from "./auth"
import { settingsStore } from "~/newstore/settings"
import {
@@ -47,12 +48,36 @@ let loadedRESTHistory = false
*/
let loadedGraphqlHistory = false
async function writeHistory(entry: any, col: HistoryFBCollections) {
const purgeFormDataFromRequest = (req: RESTHistoryEntry): RESTHistoryEntry => {
if (req.request.body.contentType !== "multipart/form-data") return req
req.request.body.body = req.request.body.body.map<FormDataKeyValue>(
(formData) => {
if (!formData.isFile) return formData
return {
active: formData.active,
isFile: false, // Something we can do to keep the status ?
key: formData.key,
value: "",
}
}
)
return req
}
async function writeHistory(
entry: RESTHistoryEntry,
col: HistoryFBCollections
) {
const processedEntry = purgeFormDataFromRequest(entry)
if (currentUser$.value == null)
throw new Error("User not logged in to sync history")
const hs = {
...entry,
...processedEntry,
updatedOn: new Date(),
}
@@ -67,7 +92,10 @@ async function writeHistory(entry: any, col: HistoryFBCollections) {
}
}
async function deleteHistory(entry: any, col: HistoryFBCollections) {
async function deleteHistory(
entry: RESTHistoryEntry & { id: string },
col: HistoryFBCollections
) {
if (currentUser$.value == null)
throw new Error("User not logged in to delete history")
@@ -89,7 +117,7 @@ async function clearHistory(col: HistoryFBCollections) {
collection(getFirestore(), "users", currentUser$.value.uid, col)
)
await Promise.all(docs.map((e) => deleteHistory(e, col)))
await Promise.all(docs.map((e) => deleteHistory(e as any, col)))
}
async function toggleStar(entry: any, col: HistoryFBCollections) {