|
1 | 1 | import { useState } from "react"; |
2 | | -import { v4 as uuidv4 } from 'uuid'; |
3 | 2 | import { Layout, Typography } from "antd"; |
4 | | -import { getJobStatus, addRecipe } from "./utils/firebase"; |
| 3 | +import { getJobStatus } from "./utils/firebase"; |
5 | 4 | import { getFirebaseRecipe, jsonToString } from "./utils/recipeLoader"; |
6 | 5 | import { getSubmitPackingUrl, JOB_STATUS } from "./constants/aws"; |
7 | | -import { FIRESTORE_FIELDS } from "./constants/firebase"; |
8 | 6 | import { SIMULARIUM_EMBED_URL } from "./constants/urls"; |
9 | 7 | import PackingInput from "./components/PackingInput"; |
10 | 8 | import Viewer from "./components/Viewer"; |
@@ -34,39 +32,19 @@ function App() { |
34 | 32 | return !(jsonToString(originalRecipe) == recipeString); |
35 | 33 | } |
36 | 34 |
|
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 }); |
47 | 42 | } |
48 | 43 |
|
49 | 44 | const submitRecipe = async (recipeId: string, configId: string, recipeString: string) => { |
50 | 45 | setResultUrl(""); |
51 | 46 | 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); |
70 | 48 | start = Date.now(); |
71 | 49 | const response = await fetch(request); |
72 | 50 | setJobStatus(JOB_STATUS.SUBMITTED); |
|
0 commit comments