diff --git a/MidrandBookshop/Components/Pages/Checkout.razor b/MidrandBookshop/Components/Pages/Checkout.razor index 1b307ee..01d8dc6 100644 --- a/MidrandBookshop/Components/Pages/Checkout.razor +++ b/MidrandBookshop/Components/Pages/Checkout.razor @@ -1,19 +1,18 @@ @page "/checkout" @inject NavigationManager Navigation +@rendermode InteractiveServer +@attribute [Authorize]

Checkout

- -
-
Your Items
- @foreach (var item in CartItems) + @foreach (var item in ShoppingCart.Items) {
-
@item.Title
@item.Author
+
@item.Product!.Name
@($"{item.Author!.Name} {item.Author.LastName}")
@@ -26,7 +25,6 @@ }
-
Shipping Method
@@ -41,70 +39,28 @@
-
Shipping Address
-
-
Order Summary
-
SubtotalR @Subtotal.ToString("F2")
-
VAT (15%)R @VatAmount.ToString("F2")
-
ShippingR @ShippingCost.ToString("F2")
+
SubtotalR @ShoppingCart.TotalAmount.ToString("F2")
+
VAT (15%)R @ShoppingCart.TotalVat.ToString("F2")
+
ShippingR @($"{ShippingCost:F2}")

Total Due -

R @((Subtotal + VatAmount + ShippingCost).ToString("F2"))

+

R @($"{ShoppingCart.TotalAmount + ShoppingCart.TotalVat + ShippingCost:F2}")

-
- -@code { - private decimal ShippingCost = 0; - private bool IsSameAddress = true; - - // Calculations - private decimal Subtotal => CartItems.Sum(i => (decimal)i.Price * i.Quantity); - private decimal VatAmount => Subtotal * 0.15m; - - // Assuming your CartItems list is managed via a Service or cascading parameter - // Here it is locally mocked for this example - private List CartItems = new() - { - new CartItem { Id = 1, Title = "Letters from M/M (Paris)", Author = "M/M Paris", Price = 720, Quantity = 1 }, - new CartItem { Id = 2, Title = "Daan Paans: Floating Signifiers", Author = "Daan Paans", Price = 540, Quantity = 1 } - }; - - private void ChangeQuantity(CartItem item, int delta) - { - item.Quantity += delta; - if (item.Quantity <= 0) CartItems.Remove(item); - } - - private void RemoveFromCart(CartItem item) => CartItems.Remove(item); - private int GetCartTotal() => CartItems.Sum(i => i.Price * i.Quantity); - - public class CartItem - { - public int Id { get; set; } - public string Title { get; set; } = ""; - public string Author { get; set; } = ""; - public int Price { get; set; } - public int Quantity { get; set; } - } - private void CompletePurchase(MouseEventArgs args) - { - Navigation.NavigateTo("/payment-confirmation"); - } -} \ No newline at end of file +
\ No newline at end of file diff --git a/MidrandBookshop/Components/Pages/Checkout.razor.cs b/MidrandBookshop/Components/Pages/Checkout.razor.cs new file mode 100644 index 0000000..d980d9c --- /dev/null +++ b/MidrandBookshop/Components/Pages/Checkout.razor.cs @@ -0,0 +1,54 @@ +using MidrandBookshop.Services.ShoppingCart; +using MidrandBookshop.Services.ShoppingCart.Models; + +namespace MidrandBookshop.Components.Pages; + +public partial class Checkout(CartService cartService) +{ + [Inject] + private AuthenticationStateProvider AuthStateProvider { get; set; } = default!; + + private Services.ShoppingCart.Models.Cart ShoppingCart => cartService.ShoppingCart; + + private AuthenticationState? AuthState { get; set; } + private System.Security.Claims.ClaimsPrincipal? User { get; set; } + private bool IsAuthenticated => User?.Identity?.IsAuthenticated ?? false; + + private decimal ShippingCost = 0; + private bool IsSameAddress = true; + + private decimal OrderTotalAmount => ShoppingCart.TotalAmount + ShoppingCart.TotalVat + ShippingCost; + + protected override async Task OnInitializedAsync() + { + AuthState = await AuthStateProvider.GetAuthenticationStateAsync(); + User = AuthState!.User; + + Navigation.LocationChanged += OnLocationChanged; + cartService.OnCartChanged += CartService_OnCartChanged; + } + + private async void CartService_OnCartChanged() => await InvokeAsync(StateHasChanged); + + private void OnLocationChanged(object? sender, LocationChangedEventArgs e) => StateHasChanged(); + + private async void ChangeQuantity(CartItem item, int delta) + { + var peekQuantity = item.Quantity + delta; + + if (peekQuantity < 1) return; + + cartService.UpdateQuantity(item.Price!.Id, delta); + + await cartService.SaveCartToStorageAsync(); + } + + private async void RemoveFromCart(CartItem item) + { + cartService.RemoveOneItem(item.Price!.Id); + + await cartService.SaveCartToStorageAsync(); + } + + private void CompletePurchase(MouseEventArgs args) => Navigation.NavigateTo("/payment-confirmation"); +} diff --git a/MidrandBookshop/Components/Pages/PaymentConfirmation.razor b/MidrandBookshop/Components/Pages/PaymentConfirmation.razor index 555af5f..0b06630 100644 --- a/MidrandBookshop/Components/Pages/PaymentConfirmation.razor +++ b/MidrandBookshop/Components/Pages/PaymentConfirmation.razor @@ -3,8 +3,6 @@
- -
@@ -20,12 +18,11 @@
-
Continue Shopping
Track Order @@ -33,7 +30,6 @@
-

You will receive a confirmation email shortly at user@email.com.