Files
MistoxCom-Blazor-Webassembly/src/MistoxWebsite.Client/Pages/Store/Cart.razor
T
2025-06-19 16:49:34 -07:00

82 lines
2.4 KiB
Plaintext
Executable File

@page "/store/cart"
@attribute [Authorize]
<h3 class="cart-title">Cart</h3>
<div class="Spacer">
@foreach( MistoxWebsite.Shared.Database.Cart obj in Statics.Carts ) {
<div class="cart-item">
<h1>@getItem(obj.ProductID)?.Name</h1>
@foreach( string cur in getItem( obj.ProductID )?.Images ) {
<img src="@cur" alt="Not found">
}
<h2>$@((Convert.ToSingle( getItem( obj.ProductID )?.Cost ) / 100f).ToString( "0.00" )) USD</h2>
<button @onclick="() => removeFromCart( obj.ProductID )"><span>Remove From Cart</span></button>
</div>
}
</div>
<div class="cart-total">
@if( Statics.Carts.Count > 0 ) {
<h3>Total : $@total.ToString( "0.00" )</h3>
<button @onclick="() => Chekout()">Checkout</button>
} else {
<h3>The cart is empty</h3>
}
</div>
@code {
[Parameter] public Func<bool>? ShouldReRender { get; set; }
[Parameter] public Catalog? parent { get; set; } = null;
float total = 0;
Product? getItem(int productID ) {
foreach(Product cur in Statics.Products ) {
if (cur.ID == productID ) {
return cur;
}
}
return null;
}
protected override void OnInitialized() {
total = 0;
foreach( MistoxWebsite.Shared.Database.Cart obj in Statics.Carts ) {
foreach( Product item in Statics.Products ) {
if( obj.ProductID == item.ID ) {
total = total + (item.Cost / 100f);
break;
}
}
}
base.StateHasChanged();
}
public void RecalcTotal() {
OnInitialized();
}
public async void removeFromCart(int ID) {
for(int i=0; i<=Statics.Carts.Count; i++ ) {
if(ID == Statics.Carts[i].ProductID ) {
await Http.PostAsJsonAsync( "api/cart/remove", Statics.Carts [i] );
Statics.Carts.RemoveAt(i);
total = 0;
OnInitialized();
break;
}
}
if (parent != null ) {
parent?.Refresh();
} else {
base.StateHasChanged();
}
}
public void Chekout() {
Nav.NavigateTo("/store/payment/checkout");
}
}