Skip to content

Commit

Permalink
used fetch instead of axios to fix compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
marwanehab98 committed Jan 2, 2023
1 parent a3131c0 commit 83ae882
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 74 deletions.
Binary file added favicon.ico
Binary file not shown.
Binary file added logo192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added logo512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion server/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
SetEnvIfNoCase Request_URI ^ / index.php no-gzip -vary
SetEnvIfNoCase Request_URI ^ / index.php no-gzip -vary
# HTID:21006778: DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES BELOW
allow from 0.0.0.0
# DO NOT REMOVE OR MODIFY THIS LINE AND THE LINES ABOVE HTID:21006778:
20 changes: 13 additions & 7 deletions server/DbConnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@
*/
class DbConnect
{
// private $server = 'https://databases.000webhost.com';
// private $dbname = 'id20087235_scandiweb_task';
// private $user = 'id20087235_root';
// private $pass = 'WpG)fM7WHlk%oEOX';
private $server = 'localhost';
private $dbname = 'scandiweb_task';
private $user = 'root';
private $pass = '';
private $dbname = 'id20087235_scandiweb_task';
private $user = 'id20087235_root';
private $pass = 'WpG)fM7WHlk%oEOX';

// private $server = 'sql7.freemysqlhosting.net';
// private $dbname = 'sql7587618';
// private $user = 'sql7587618';
// private $pass = '2BbPJcDg3X';

// private $server = 'localhost';
// private $dbname = 'scandiweb_task';
// private $user = 'root';
// private $pass = '';



Expand Down
27 changes: 17 additions & 10 deletions server/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ini_set('display_errors', 1);
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
// header('Access-Control-Max-Age: 86400');
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");

Expand All @@ -21,14 +21,21 @@
$products_list->getProducts($conn);
break;
case "POST":
$product = json_decode(file_get_contents('php://input'));
$type = $product->type;
$class = $type;
$product_object = new $class($product, true);
$product_object->addProduct($conn, $type);
break;
case "DELETE":
$products_list = new ProductList();
$products_list->deleteProduct($conn);
$path = explode('/', $_SERVER['REQUEST_URI']);
if ($path[1] == "addproduct") {
$product = json_decode(file_get_contents('php://input'));
$type = $product->type;
$class = $type;
// echo json_encode($path);
$product_object = new $class($product, true);
$product_object->addProduct($conn, $type);
} else {
$products_list = new ProductList();
$products_list->deleteProduct($conn, $path[2]);
}
break;
// case "DELETE":
// $products_list = new ProductList();
// $products_list->deleteProduct($conn);
// break;
}
9 changes: 4 additions & 5 deletions server/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function addProduct($conn, $type)
if ($valid) {
try {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type);" .
"INSERT INTO dvds(SKU, size) VALUES(:SKU, :size)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
Expand Down Expand Up @@ -188,7 +188,7 @@ public function addProduct($conn, $type)
if ($valid) {
try {
$sql =
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type)" .
"INSERT INTO products(SKU, name, price, type) VALUES(:SKU, :name, :price, :type);" .
"INSERT INTO furniture(SKU, length, width, height) VALUES(:SKU, :length, :width, :height)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':SKU', $this->SKU);
Expand Down Expand Up @@ -241,13 +241,12 @@ public function getProducts($conn)
}
}

public function deleteProduct($conn)
public function deleteProduct($conn, $SKU)
{
try {
$sql = "DELETE FROM products WHERE SKU = :id";
$path = explode('/', $_SERVER['REQUEST_URI']);
$stmt = $conn->prepare($sql);
$stmt->bindParam(':id', $path[4]);
$stmt->bindParam(':id', $SKU);
$stmt->execute();
$response = ['status' => '200', 'message' => 'Record deleted successfully.'];
} catch (\Throwable $error) {
Expand Down
11 changes: 0 additions & 11 deletions server/test.php

This file was deleted.

40 changes: 25 additions & 15 deletions src/components/CreateProduct/CreateProduct.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import './CreateProduct.css';
import axios from 'axios';
import { Navbar, Container, Nav, Form, Button, Modal } from 'react-bootstrap';
import BookComponent from './TypeComponents/BookComponent';
import DVDComponent from './TypeComponents/DVDComponent'
Expand Down Expand Up @@ -61,16 +60,27 @@ function CreateProduct() {
return true
});
if (valid) {
axios.post('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/', product).then(function (response) {
console.log(response.data);
if (response.data.status === '200') {
navigate('/');
}
else {
setErrorMessage(response.data.message);
handleShow()
}
});
try {
// console.log(product)
fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/addproduct', {
method: 'post',
body: JSON.stringify(product)
}).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data);
if (data.status === '200') {
navigate('/');
}
else {
setErrorMessage(data.message);
handleShow()
}
});
} catch (error) {
alert(error)
}

}
else {
handleShow();
Expand Down Expand Up @@ -113,7 +123,7 @@ function CreateProduct() {
<Form.Group className="mb-3" controlId="#sku">
<Form.Label>SKU</Form.Label>
<Form.Control
id='#sku'
// id='#sku'
required
className="productDataEntry"
placeholder="Enter SKU"
Expand All @@ -128,7 +138,7 @@ function CreateProduct() {
<Form.Group className="mb-3" controlId="#name">
<Form.Label>Name</Form.Label>
<Form.Control
id='#name'
// id='#name'
required
className="productDataEntry"
placeholder="Enter Product Name"
Expand All @@ -143,7 +153,7 @@ function CreateProduct() {
<Form.Group className="mb-3" controlId="#price">
<Form.Label>Price ($)</Form.Label>
<Form.Control
id='#price'
// id='#price'
required
// type='number'
// step={0.01}
Expand All @@ -159,7 +169,7 @@ function CreateProduct() {
</Form.Group>
<Form.Group className="mb-3" controlId='#productType'>
<Form.Label>Type Swithcer</Form.Label>
<Form.Select id='#productType' onChange={(e) => handleTypeChange(e)}>
<Form.Select onChange={(e) => handleTypeChange(e)}>
<option>Book</option>
<option>DVD</option>
<option>Furniture</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function BookComponent(props) {
<Form.Group className="mb-3" controlId="#weight">
<Form.Label>Weight (KG)</Form.Label>
<Form.Control
id='#weight'
// id='#weight'
required
// type='number'
// step={0.01}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function DVDComponent(props) {
<Form.Group className="mb-3" controlId="#size">
<Form.Label>Size (MB)</Form.Label>
<Form.Control
id='#size'
// id='#size'
required
// type='number'
// step={0.01}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function FurnitureComponent(props) {
<Form.Group className="mb-3" controlId="#length">
<Form.Label>Length (CM)</Form.Label>
<Form.Control
id='#length'
// id='#length'
required
// type='number'
// step={0.01}
Expand All @@ -21,7 +21,7 @@ function FurnitureComponent(props) {
<Form.Group className="mb-3" controlId="#width">
<Form.Label>Width (CM)</Form.Label>
<Form.Control
id='#width'
// id='#width'
required
// type='number'
// step={0.01}
Expand All @@ -35,7 +35,7 @@ function FurnitureComponent(props) {
<Form.Group className="mb-3" controlId="#height">
<Form.Label>Height (CM)</Form.Label>
<Form.Control
id='#height'
// id='#height'
required
// type='number'
// step={0.01}
Expand Down
3 changes: 2 additions & 1 deletion src/components/Product/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,14 @@ function Product(props) {
<>
<Card
bg={"light"}
key={"Light"}
key={"Card" + props.product.SKU}
text={"dark"}
style={{ width: '18rem' }}
className="mb-2"
>
<Card.Header>
<input
id={'checkbox' + props.product.SKU}
type="checkbox"
className='.delete-checkbox'
onChange={(e) => handleCheckbox(e)}></input>
Expand Down
49 changes: 30 additions & 19 deletions src/components/ProductsList/ProductsList.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import axios from 'axios';
import { useEffect, useState } from 'react';
import './ProductsList.css';
import Product from '../Product/Product';
// import Col from 'react-bootstrap/Col';
// import Row from 'react-bootstrap/Row';
import { Navbar, Container, Nav, Col, Row, Button } from 'react-bootstrap';


Expand All @@ -24,31 +21,49 @@ export default function ProductsList() {


function getProducts() {
axios.get('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/').then(function (response) {
setProducts(response.data);
});
try {
fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/', {
method: 'get',
}).then(function (response) {
return response.json();
}).then(function (data) {
// console.log(data);
setProducts(data);
});
} catch (error) {
alert(error);
}
}

const handleDelete = (event) => {
event.preventDefault();
selected.forEach(SKU => {
deleteHelper(SKU);
});
// handleSelection({});
}

const deleteHelper = (SKU) => {
let tempSelected = [];
Object.assign(tempSelected, selected);
axios.delete('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/' + SKU + '/delete').then(function (response) {
console.log(response.data);
tempSelected = tempSelected.filter(product => {
// eslint-disable-next-line eqeqeq
return product != SKU;
try {
fetch('https://scandiweb-task-marwan-elsheikh.000webhostapp.com/deleteproduct/' + SKU, {
method: 'post',
}).then(function (response) {
return response.json();
}).then(function (data) {
console.log(data);
if (data.status === '200') {
tempSelected = tempSelected.filter(product => {
// eslint-disable-next-line eqeqeq
return product != SKU;
})
setSelected(tempSelected);
getProducts();
}
});
setSelected(tempSelected);
getProducts();
});
} catch (error) {
alert(error);
}
}


Expand All @@ -63,10 +78,6 @@ export default function ProductsList() {
setSelected(tempSelected);
};

// if (products.length === 0) {
// return ([]);
// }

return (
<>
<Navbar bg="dark" variant="dark">
Expand Down

0 comments on commit 83ae882

Please sign in to comment.