Skip to content

Commit d47ed9d

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

File tree

3 files changed

+18
-41
lines changed

3 files changed

+18
-41
lines changed

src/App.tsx

Lines changed: 9 additions & 31 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";
@@ -34,39 +32,19 @@ function App() {
3432
return !(jsonToString(originalRecipe) == recipeString);
3533
}
3634

37-
const recipeToFirebase = (recipe: string, path: string, id: string): object => {
38-
const recipeJson = JSON.parse(recipe);
39-
if (recipeJson.bounding_box) {
40-
const flattened_array = Object.assign({}, recipeJson.bounding_box);
41-
recipeJson.bounding_box = flattened_array;
42-
}
43-
recipeJson[FIRESTORE_FIELDS.RECIPE_PATH] = path;
44-
recipeJson[FIRESTORE_FIELDS.NAME] = id;
45-
recipeJson[FIRESTORE_FIELDS.TIMESTAMP] = Date.now();
46-
return recipeJson;
35+
const buildSubmitRecipeRequest = async (recipeId: string, configId: string, recipeString: string): Promise<Request> => {
36+
const recipeChanged: boolean = await recipeHasChanged(recipeId, recipeString);
37+
const firebaseRecipe = recipeChanged ? undefined : "firebase:recipes/" + recipeId;
38+
const firebaseConfig = configId ? "firebase:configs/" + configId : undefined;
39+
const url = getSubmitPackingUrl(firebaseRecipe, firebaseConfig);
40+
const requestBody = recipeChanged ? recipeString : undefined;
41+
return new Request(url, { method: "POST", body: requestBody });
4742
}
4843

4944
const submitRecipe = async (recipeId: string, configId: string, recipeString: string) => {
5045
setResultUrl("");
5146
setRunTime(0);
52-
let firebaseRecipe = "firebase:recipes/" + recipeId;
53-
const firebaseConfig = configId ? "firebase:configs/" + configId : undefined;
54-
const recipeChanged: boolean = await recipeHasChanged(recipeId, recipeString);
55-
if (recipeChanged) {
56-
const recipeId = uuidv4();
57-
firebaseRecipe = "firebase:recipes_edited/" + recipeId;
58-
const recipeJson = recipeToFirebase(recipeString, firebaseRecipe, recipeId);
59-
try {
60-
await addRecipe(recipeId, recipeJson);
61-
} catch (e) {
62-
setJobStatus(JOB_STATUS.FAILED);
63-
setJobLogs(String(e));
64-
return;
65-
}
66-
67-
}
68-
const url = getSubmitPackingUrl(firebaseRecipe, firebaseConfig);
69-
const request: RequestInfo = new Request(url, { method: "POST" });
47+
const request = await buildSubmitRecipeRequest(recipeId, configId, recipeString);
7048
start = Date.now();
7149
const response = await fetch(request);
7250
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)