delete product

This commit is contained in:
Martin Quarda 2024-10-14 08:02:49 +02:00
parent 9a65214c01
commit f35a317e20
2 changed files with 28 additions and 0 deletions

View File

@ -372,6 +372,16 @@ def cart_add(request):
return HttpResponse('{"status":"success", "reason":"Úspěšně přidáno do košíku."}', status=200) return HttpResponse('{"status":"success", "reason":"Úspěšně přidáno do košíku."}', status=200)
def cart_delete(request):
_id = request.GET['id']
user = request.user
cart = user.cart
product = Product.objects.get(id=_id)
cart_product = CartProduct.objects.get(cart=cart, product=product)
cart_product.delete()
return HttpResponse('{"status":"success", "reason":"Úspěšně odstraněno z košíku."')
def cart(request): def cart(request):
user = request.user user = request.user
if not user.is_authenticated: if not user.is_authenticated:

View File

@ -174,6 +174,22 @@ class Main extends Component {
<svg xmlns="http://www.w3.org/2000/svg" width="92" height="92" id="cross"><path fill="white" d="M70.7 64.3c1.8 1.8 1.8 4.6 0 6.4-.9.9-2 1.3-3.2 1.3-1.2 0-2.3-.4-3.2-1.3L46 52.4 27.7 70.7c-.9.9-2 1.3-3.2 1.3s-2.3-.4-3.2-1.3c-1.8-1.8-1.8-4.6 0-6.4L39.6 46 21.3 27.7c-1.8-1.8-1.8-4.6 0-6.4 1.8-1.8 4.6-1.8 6.4 0L46 39.6l18.3-18.3c1.8-1.8 4.6-1.8 6.4 0 1.8 1.8 1.8 4.6 0 6.4L52.4 46l18.3 18.3z"></path></svg> <svg xmlns="http://www.w3.org/2000/svg" width="92" height="92" id="cross"><path fill="white" d="M70.7 64.3c1.8 1.8 1.8 4.6 0 6.4-.9.9-2 1.3-3.2 1.3-1.2 0-2.3-.4-3.2-1.3L46 52.4 27.7 70.7c-.9.9-2 1.3-3.2 1.3s-2.3-.4-3.2-1.3c-1.8-1.8-1.8-4.6 0-6.4L39.6 46 21.3 27.7c-1.8-1.8-1.8-4.6 0-6.4 1.8-1.8 4.6-1.8 6.4 0L46 39.6l18.3-18.3c1.8-1.8 4.6-1.8 6.4 0 1.8 1.8 1.8 4.6 0 6.4L52.4 46l18.3 18.3z"></path></svg>
</button> </button>
} }
deleteProduct(product){
fetch(addr_prefix + "/api/cart/delete?id=" + product.id).then(resp => resp.json()).then(json=>{
if(json.status == "failed"){
this.setState({
status_text: json.reason,
status: "failed",
})
}else{
this.setState({
status_text: json.reason,
status: "success",
cart: this.state.cart.filter(p => product.id != p.id),
})
}
})
}
render(){ render(){
let photos_gallery = []; let photos_gallery = [];
let photos_carousel = []; let photos_carousel = [];
@ -275,6 +291,7 @@ class Main extends Component {
<th scope="col">Název</th> <th scope="col">Název</th>
<th scope="col">Množství</th> <th scope="col">Množství</th>
<th scope="col">Cena</th> <th scope="col">Cena</th>
<th scope="col">Smazat</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -283,6 +300,7 @@ class Main extends Component {
<td>{product.name}</td> <td>{product.name}</td>
<td>{product.quantity}</td> <td>{product.quantity}</td>
<td>{product.price} </td> <td>{product.price} </td>
<td><button type="button" class="btn btn-light" onClick={(e) => {this.deleteProduct(product)}}>Smazat</button></td>
</tr>)} </tr>)}
</tbody> </tbody>
</table> </table>