소스 검색

Fix several issues: Fix server path. Improve ingredients parsing

Tatiana Inama 7 년 전
부모
커밋
4d57c91a25
5개의 변경된 파일57개의 추가작업 그리고 38개의 파일을 삭제
  1. 27 14
      ktchn/src/recipes/scrape/index.ts
  2. 6 1
      ktchn/src/server.ts
  3. 1 1
      recipes/.env
  4. 20 21
      recipes/package.json
  5. 3 1
      recipes/src/components/RecipeForm/index.tsx

+ 27 - 14
ktchn/src/recipes/scrape/index.ts

@@ -20,13 +20,22 @@ function selectSourceAsync(url: string): Promise<ScrapingSource> {
 }
 
 export const parseIngredients = (rawIngredient: string): IIngredient => {
-  let parsed = parseIngredient(rawIngredient);
-  return {
-    name: parsed.ingredient,
-    quantity: Number(parsed.quantity) || 0,
-    unit: units(parsed.unit),
-    _original: rawIngredient,
-  };
+  try {
+    let parsed = parseIngredient(rawIngredient);
+    return {
+      name: parsed.ingredient,
+      quantity: Number(parsed.quantity) || 0,
+      unit: units(parsed.unit),
+      _original: rawIngredient,
+    };
+  } catch {
+    return {
+      name: '',
+      quantity: 0,
+      unit: '',
+      _original: rawIngredient
+    }
+  }
 };
 
 async function scrape(url:string) {
@@ -40,13 +49,17 @@ async function scrape(url:string) {
     $ => selectSourceAsync(url).then(
       source => {
         const recipe = source.scrapeRecipe($);
-        const cleanIngredients = recipe.ingredients.filter(subRecipe => {
-          return subRecipe.name !== '' && subRecipe.ingredients.length !== 0;
-        })
-        return {
-          ...recipe,
-          ingredients: cleanIngredients,
-        };
+        if (recipe.ingredients.length > 1 ) {
+          const cleanIngredients = recipe.ingredients.filter(subRecipe => {
+            return subRecipe.name !== '' && subRecipe.ingredients.length !== 0;
+          })
+          return {
+            ...recipe,
+            ingredients: cleanIngredients,
+          };
+        } else {
+          return recipe;
+        }
       }
     )
   )

+ 6 - 1
ktchn/src/server.ts

@@ -59,8 +59,13 @@ class App {
     this.app.use(express.static(path.join(__dirname, "public")));
     this.app.use((req, res, next)=> {
       res.header("Access-Control-Allow-Origin", "*");
+      res.header('Access-Control-Allow-Methods', 'DELETE, PUT');
       res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
-      next();
+      if ('OPTIONS' == req.method) {
+        res.sendStatus(200);
+      } else {
+        next();
+      }
     })
   }
 

+ 1 - 1
recipes/.env

@@ -1,3 +1,3 @@
 SASS_PATH=node_modules:src
 NODE_PATH=src/
-REACT_APP_API_RECIPES='http://localhost:3000/recipes'
+REACT_APP_API_RECIPES='http://192.168.2.178:3000/recipes'

+ 20 - 21
recipes/package.json

@@ -17,31 +17,31 @@
     "@material/react-tab-indicator": "^0.13.0",
     "@material/react-text-field": "^0.11.0",
     "@material/ripple": "^2.3.0",
-    "@types/jest": "^24.0.11",
-    "@types/node": "^11.13.0",
-    "@types/react": "^16.8.12",
+    "@types/jest": "^24.0.15",
+    "@types/node": "^11.13.17",
+    "@types/react": "^16.8.23",
     "@types/react-dom": "^16.8.3",
-    "@types/react-router-dom": "^4.3.1",
-    "@types/storybook__addon-actions": "^3.4.2",
-    "@types/storybook__addon-links": "^3.3.4",
-    "@types/storybook__react": "^4.0.1",
-    "axios": "^0.18.0",
+    "@types/react-router-dom": "^4.3.4",
+    "@types/storybook__addon-actions": "^3.4.3",
+    "@types/storybook__addon-links": "^3.3.5",
+    "@types/storybook__react": "^4.0.2",
+    "axios": "^0.19.0",
     "classnames": "^2.2.6",
     "convert-units": "git+https://github.com/ben-ng/convert-units.git",
-    "formik": "^1.5.7",
+    "formik": "1.5.7",
     "moment": "^2.24.0",
     "node-sass": "^4.11.0",
     "ramda": "^0.26.1",
     "react": "^16.8.4",
     "react-dom": "^16.8.4",
-    "react-redux": "^7.0.1",
-    "react-router-dom": "^5.0.0",
+    "react-redux": "^7.1.0",
+    "react-router-dom": "^5.0.1",
     "react-scripts": "2.1.8",
-    "react-svg": "^9.0.2",
-    "redux": "^4.0.1",
+    "react-svg": "^9.0.4",
+    "redux": "^4.0.4",
     "redux-devtools-extension": "^2.13.8",
     "redux-thunk": "^2.3.0",
-    "typescript": "^3.4.1"
+    "typescript": "^3.5.3"
   },
   "scripts": {
     "start": "set PORT=3006 && react-scripts start",
@@ -61,12 +61,11 @@
     "not op_mini all"
   ],
   "devDependencies": {
-    "@babel/core": "^7.4.3",
-    "@storybook/addon-actions": "^5.0.6",
-    "@storybook/addon-links": "^5.0.6",
-    "@storybook/addons": "^5.0.6",
-    "@storybook/react": "^5.0.6",
-    "@types/react-redux": "^7.0.6",
-    "babel-loader": "^8.0.5"
+    "@babel/core": "^7.5.4",
+    "@storybook/addon-actions": "^5.1.9",
+    "@storybook/addon-links": "^5.1.9",
+    "@storybook/addons": "^5.1.9",
+    "@storybook/react": "^5.1.9",
+    "@types/react-redux": "^7.1.1"
   }
 }

+ 3 - 1
recipes/src/components/RecipeForm/index.tsx

@@ -1,5 +1,5 @@
 import React, { useState, Component } from 'react';
-import { Formik, Field, FieldArray,  FormikProps, ArrayHelpers } from 'formik';
+
 import Recipe, { Ingredient, Suggestion, _ingredient } from 'types/recipes';
 import { Grid, Row, Cell } from '@material/react-layout-grid';
 import './styles.scss';
@@ -7,6 +7,8 @@ import sample_image from 'sample.png';
 import { MeasuresTypes } from 'services/measurements';
 import { LightInput as Input } from 'components/Input';
 import Button from 'components/Button';
+//@ts-ignore
+import { Field, FieldArray, FormikProps, ArrayHelpers, Formik } from 'formik';
 
 type RecipeFormProps = {
   initialValues: Recipe,