|
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"; |
@@ -44,54 +42,18 @@ function App() { |
44 | 42 | return !(jsonToString(originalRecipe) == recipeString); |
45 | 43 | }; |
46 | 44 |
|
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 | + } |
62 | 53 |
|
63 | | - const submitRecipe = async ( |
64 | | - recipeId: string, |
65 | | - configId: string, |
66 | | - recipeString: string |
67 | | - ) => { |
| 54 | + const submitRecipe = async (recipeId: string, configId: string, recipeString: string) => { |
68 | 55 | 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); |
95 | 57 | start = Date.now(); |
96 | 58 | const response = await fetch(request); |
97 | 59 | setJobStatus(JOB_STATUS.SUBMITTED); |
|
0 commit comments