Skip to content

Commit 1b766b7

Browse files
committed
for edited recipes, send json version of recipe to server rather than upload to firebase
1 parent baf0dd0 commit 1b766b7

File tree

3 files changed

+20
-59
lines changed

3 files changed

+20
-59
lines changed

src/App.tsx

Lines changed: 11 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { useState } from "react";
2-
import { v4 as uuidv4 } from "uuid";
32
import { Layout, Typography } from "antd";
4-
import { getJobStatus, addRecipe } from "./utils/firebase";
3+
import { getJobStatus } from "./utils/firebase";
54
import { getFirebaseRecipe, jsonToString } from "./utils/recipeLoader";
65
import { getSubmitPackingUrl, JOB_STATUS } from "./constants/aws";
7-
import { FIRESTORE_FIELDS } from "./constants/firebase";
86
import { SIMULARIUM_EMBED_URL } from "./constants/urls";
97
import PackingInput from "./components/PackingInput";
108
import Viewer from "./components/Viewer";
@@ -44,54 +42,18 @@ function App() {
4442
return !(jsonToString(originalRecipe) == recipeString);
4543
};
4644

47-
const recipeToFirebase = (
48-
recipe: string,
49-
path: string,
50-
id: string
51-
): object => {
52-
const recipeJson = JSON.parse(recipe);
53-
if (recipeJson.bounding_box) {
54-
const flattened_array = Object.assign({}, recipeJson.bounding_box);
55-
recipeJson.bounding_box = flattened_array;
56-
}
57-
recipeJson[FIRESTORE_FIELDS.RECIPE_PATH] = path;
58-
recipeJson[FIRESTORE_FIELDS.NAME] = id;
59-
recipeJson[FIRESTORE_FIELDS.TIMESTAMP] = Date.now();
60-
return recipeJson;
61-
};
45+
const buildSubmitRecipeRequest = async (recipeId: string, configId: string, recipeString: string): Promise<Request> => {
46+
const recipeChanged: boolean = await recipeHasChanged(recipeId, recipeString);
47+
const firebaseRecipe = recipeChanged ? undefined : "firebase:recipes/" + recipeId;
48+
const firebaseConfig = configId ? "firebase:configs/" + configId : undefined;
49+
const url = getSubmitPackingUrl(firebaseRecipe, firebaseConfig);
50+
const requestBody = recipeChanged ? recipeString : undefined;
51+
return new Request(url, { method: "POST", body: requestBody });
52+
}
6253

63-
const submitRecipe = async (
64-
recipeId: string,
65-
configId: string,
66-
recipeString: string
67-
) => {
54+
const submitRecipe = async (recipeId: string, configId: string, recipeString: string) => {
6855
resetState();
69-
let firebaseRecipe = "firebase:recipes/" + recipeId;
70-
const firebaseConfig = configId
71-
? "firebase:configs/" + configId
72-
: undefined;
73-
const recipeChanged: boolean = await recipeHasChanged(
74-
recipeId,
75-
recipeString
76-
);
77-
if (recipeChanged) {
78-
const recipeId = uuidv4();
79-
firebaseRecipe = "firebase:recipes_edited/" + recipeId;
80-
const recipeJson = recipeToFirebase(
81-
recipeString,
82-
firebaseRecipe,
83-
recipeId
84-
);
85-
try {
86-
await addRecipe(recipeId, recipeJson);
87-
} catch (e) {
88-
setJobStatus(JOB_STATUS.FAILED);
89-
setJobLogs(String(e));
90-
return;
91-
}
92-
}
93-
const url = getSubmitPackingUrl(firebaseRecipe, firebaseConfig);
94-
const request: RequestInfo = new Request(url, { method: "POST" });
56+
const request = await buildSubmitRecipeRequest(recipeId, configId, recipeString);
9557
start = Date.now();
9658
const response = await fetch(request);
9759
setJobStatus(JOB_STATUS.SUBMITTED);

src/constants/aws.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ const SUBMIT_PACKING_ECS = "https://bda21vau5c.execute-api.us-west-2.amazonaws.c
55
const S3_BASE_URL = "https://s3.us-west-2.amazonaws.com";
66

77
export const getSubmitPackingUrl = (
8-
recipe: string,
8+
recipe?: string,
99
config?: string,
1010
) => {
11-
let url = `${SUBMIT_PACKING_ECS}?recipe=${recipe}`;
12-
if (config) {
13-
url += `&config=${config}`;
11+
let url = SUBMIT_PACKING_ECS;
12+
if (recipe && config) {
13+
url += `?recipe=${recipe}&config=${config}`;
14+
} else if (recipe) {
15+
url += `?recipe=${recipe}`;
16+
} else if (config) {
17+
url += `?config=${config}`;
1418
}
1519
return url;
1620
};

src/utils/firebase.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
documentId,
99
QuerySnapshot,
1010
DocumentData,
11-
setDoc,
1211
doc,
1312
Timestamp,
1413
deleteDoc,
@@ -172,10 +171,6 @@ const getDocsByIds = async (coll: string, ids: string[]) => {
172171
return docs;
173172
}
174173

175-
const addRecipe = async (id: string, data: object) => {
176-
await setDoc(doc(db, FIRESTORE_COLLECTIONS.EDITED_RECIPES, id), data);
177-
}
178-
179174
const docCleanup = async () => {
180175
const now = Date.now();
181176
const collectionsToClean = [
@@ -202,4 +197,4 @@ const docCleanup = async () => {
202197
console.log(`Cleaned up ${deletePromises.length} documents from ${collectionConfig.name}`);
203198
}
204199
}
205-
export { db, queryDocumentById, getDocsByIds, getJobStatus, addRecipe, docCleanup, getPackingInputsDict, getOutputsDirectory };
200+
export { db, queryDocumentById, getDocsByIds, getJobStatus, docCleanup, getPackingInputsDict, getOutputsDirectory };

0 commit comments

Comments
 (0)