From c85fd0522e3a28152631cbc2259f6383e3193701 Mon Sep 17 00:00:00 2001 From: Marwan Ehab Date: Mon, 2 Jan 2023 19:42:17 +0200 Subject: [PATCH] added environment variables --- .env.all | 3 +++ package-lock.json | 24 +++++++++++++++++++ package.json | 5 ++-- server/index.php | 6 +++-- src/components/CreateProduct/CreateProduct.js | 2 +- src/components/ProductsList/ProductsList.js | 5 ++-- 6 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 .env.all diff --git a/.env.all b/.env.all new file mode 100644 index 0000000..cfd63dc --- /dev/null +++ b/.env.all @@ -0,0 +1,3 @@ +REACT_APP_BASE_URL = "https://scandiweb-task-marwan-elsheikh.000webhostapp.com/" +REACT_APP_ADD_URL = "https://scandiweb-task-marwan-elsheikh.000webhostapp.com/addproduct/" +REACT_APP_DELETE_URL = "https://scandiweb-task-marwan-elsheikh.000webhostapp.com/deleteproduct/" \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 9240531..fa1f321 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@testing-library/user-event": "^13.5.0", "axios": "^1.2.1", "bootstrap": "^5.3.0-alpha1", + "env-cmd": "^10.1.0", "react": "^18.2.0", "react-bootstrap": "^2.7.0", "react-dom": "^18.2.0", @@ -6973,6 +6974,29 @@ "url": "https://github.com/fb55/entities?sponsor=1" } }, + "node_modules/env-cmd": { + "version": "10.1.0", + "resolved": "https://registry.npmjs.org/env-cmd/-/env-cmd-10.1.0.tgz", + "integrity": "sha512-mMdWTT9XKN7yNth/6N6g2GuKuJTsKMDHlQFUDacb/heQRRWOTIZ42t1rMHnQu4jYxU1ajdTeJM+9eEETlqToMA==", + "dependencies": { + "commander": "^4.0.0", + "cross-spawn": "^7.0.0" + }, + "bin": { + "env-cmd": "bin/env-cmd.js" + }, + "engines": { + "node": ">=8.0.0" + } + }, + "node_modules/env-cmd/node_modules/commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "engines": { + "node": ">= 6" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", diff --git a/package.json b/package.json index b69726c..c447dae 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "@testing-library/user-event": "^13.5.0", "axios": "^1.2.1", "bootstrap": "^5.3.0-alpha1", + "env-cmd": "^10.1.0", "react": "^18.2.0", "react-bootstrap": "^2.7.0", "react-dom": "^18.2.0", @@ -16,8 +17,8 @@ "web-vitals": "^2.1.4" }, "scripts": { - "start": "react-scripts start", - "build": "react-scripts build", + "start": "env-cmd -f .env.all react-scripts start", + "build": "env-cmd -f .env.all react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "heroku-postbuild": "npm run build" diff --git a/server/index.php b/server/index.php index efb30cc..42a518f 100644 --- a/server/index.php +++ b/server/index.php @@ -27,9 +27,11 @@ $class = $type; $product_object = new $class($product, true); $product_object->addProduct($conn, $type); - } else { + } else if ($path[1] == "deleteproduct") { + $body = json_decode(file_get_contents('php://input')); + $sku = $body->SKU; $products_list = new ProductList(); - $products_list->deleteProduct($conn, $path[2]); + $products_list->deleteProduct($conn, $sku); } break; } \ No newline at end of file diff --git a/src/components/CreateProduct/CreateProduct.js b/src/components/CreateProduct/CreateProduct.js index 9b55364..969f6bf 100644 --- a/src/components/CreateProduct/CreateProduct.js +++ b/src/components/CreateProduct/CreateProduct.js @@ -62,7 +62,7 @@ function CreateProduct() { if (valid) { try { // console.log(product) - fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/addproduct', { + fetch(process.env.REACT_APP_ADD_URL, { method: 'post', body: JSON.stringify(product) }).then(function (response) { diff --git a/src/components/ProductsList/ProductsList.js b/src/components/ProductsList/ProductsList.js index 2828cf5..a59af7c 100644 --- a/src/components/ProductsList/ProductsList.js +++ b/src/components/ProductsList/ProductsList.js @@ -22,7 +22,7 @@ export default function ProductsList() { function getProducts() { try { - fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/', { + fetch(process.env.REACT_APP_BASE_URL, { method: 'get', }).then(function (response) { return response.json(); @@ -46,8 +46,9 @@ export default function ProductsList() { let tempSelected = []; Object.assign(tempSelected, selected); try { - fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/deleteproduct/' + SKU, { + fetch(process.env.REACT_APP_DELETE_URL, { method: 'post', + body: JSON.stringify({"SKU": SKU}) }).then(function (response) { return response.json(); }).then(function (data) {