feat(back-end & front-end): checkout, update form structure, add new DTOs, refactor order logic
This commit is contained in:
151
frontend/src/app/features/checkout/checkout.component.html
Normal file
151
frontend/src/app/features/checkout/checkout.component.html
Normal file
@@ -0,0 +1,151 @@
|
||||
<div class="checkout-page">
|
||||
<h2 class="section-title">Checkout</h2>
|
||||
|
||||
<div class="checkout-layout">
|
||||
|
||||
<!-- LEFT COLUMN: Form -->
|
||||
<div class="checkout-form-section">
|
||||
<!-- Error Message -->
|
||||
<div *ngIf="error" class="error-message">
|
||||
{{ error }}
|
||||
</div>
|
||||
|
||||
<form [formGroup]="checkoutForm" (ngSubmit)="onSubmit()" *ngIf="!error">
|
||||
|
||||
<!-- Contact Info Card -->
|
||||
<div class="form-card">
|
||||
<div class="card-header">
|
||||
<h3>Contact Information</h3>
|
||||
</div>
|
||||
<div class="card-content">
|
||||
|
||||
<!-- User Type Selector -->
|
||||
<div class="user-type-selector">
|
||||
<div class="type-option" [class.selected]="!isCompany" (click)="setCustomerType(false)">
|
||||
Private
|
||||
</div>
|
||||
<div class="type-option" [class.selected]="isCompany" (click)="setCustomerType(true)">
|
||||
Company
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="form-row">
|
||||
<app-input formControlName="email" type="email" label="Email" [required]="true" [error]="checkoutForm.get('email')?.hasError('email') ? 'Invalid email' : null"></app-input>
|
||||
<app-input formControlName="phone" type="tel" label="Phone" [required]="true"></app-input>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Billing Address Card -->
|
||||
<div class="form-card">
|
||||
<div class="card-header">
|
||||
<h3>Billing Address</h3>
|
||||
</div>
|
||||
<div class="card-content" formGroupName="billingAddress">
|
||||
<div class="form-row">
|
||||
<app-input formControlName="firstName" label="First Name" [required]="true"></app-input>
|
||||
<app-input formControlName="lastName" label="Last Name" [required]="true"></app-input>
|
||||
</div>
|
||||
|
||||
<!-- Company Name (Conditional) -->
|
||||
<app-input *ngIf="isCompany" formControlName="companyName" label="Company Name" [required]="true"></app-input>
|
||||
|
||||
<app-input formControlName="addressLine1" label="Address Line 1" [required]="true"></app-input>
|
||||
<app-input formControlName="addressLine2" label="Address Line 2 (Optional)"></app-input>
|
||||
|
||||
<div class="form-row three-cols">
|
||||
<app-input formControlName="zip" label="ZIP Code" [required]="true"></app-input>
|
||||
<app-input formControlName="city" label="City" class="city-field" [required]="true"></app-input>
|
||||
<app-input formControlName="countryCode" label="Country" [disabled]="true" [required]="true"></app-input>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Shipping Option -->
|
||||
<div class="shipping-option">
|
||||
<label class="checkbox-container">
|
||||
<input type="checkbox" formControlName="shippingSameAsBilling">
|
||||
<span class="checkmark"></span>
|
||||
Shipping address same as billing
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<!-- Shipping Address Card (Conditional) -->
|
||||
<div class="form-card" *ngIf="!checkoutForm.get('shippingSameAsBilling')?.value">
|
||||
<div class="card-header">
|
||||
<h3>Shipping Address</h3>
|
||||
</div>
|
||||
<div class="card-content" formGroupName="shippingAddress">
|
||||
<div class="form-row">
|
||||
<app-input formControlName="firstName" label="First Name"></app-input>
|
||||
<app-input formControlName="lastName" label="Last Name"></app-input>
|
||||
</div>
|
||||
|
||||
<app-input formControlName="companyName" label="Company (Optional)"></app-input>
|
||||
<app-input formControlName="addressLine1" label="Address Line 1"></app-input>
|
||||
<app-input formControlName="zip" label="ZIP Code"></app-input>
|
||||
|
||||
<div class="form-row three-cols">
|
||||
<app-input formControlName="zip" label="ZIP Code"></app-input>
|
||||
<app-input formControlName="city" label="City" class="city-field"></app-input>
|
||||
<app-input formControlName="countryCode" label="Country" [disabled]="true"></app-input>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="actions">
|
||||
<app-button type="submit" [disabled]="checkoutForm.invalid || isSubmitting()" [fullWidth]="true">
|
||||
{{ isSubmitting() ? 'Processing...' : 'Place Order' }}
|
||||
</app-button>
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<!-- RIGHT COLUMN: Order Summary -->
|
||||
<div class="checkout-summary-section">
|
||||
<div class="form-card sticky-card">
|
||||
<div class="card-header">
|
||||
<h3>Order Summary</h3>
|
||||
</div>
|
||||
|
||||
<div class="card-content">
|
||||
<div class="summary-items" *ngIf="quoteSession() as session">
|
||||
<div class="summary-item" *ngFor="let item of session.items">
|
||||
<div class="item-details">
|
||||
<span class="item-name">{{ item.originalFilename }}</span>
|
||||
<div class="item-specs">
|
||||
<span>Qty: {{ item.quantity }}</span>
|
||||
<span *ngIf="item.colorCode" class="color-dot" [style.background-color]="item.colorCode"></span>
|
||||
</div>
|
||||
<div class="item-specs-sub">
|
||||
{{ (item.printTimeSeconds / 3600) | number:'1.1-1' }}h | {{ item.materialGrams | number:'1.0-0' }}g
|
||||
</div>
|
||||
</div>
|
||||
<div class="item-price">
|
||||
{{ (item.unitPriceChf * item.quantity) | currency:'CHF' }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="summary-totals" *ngIf="quoteSession() as session">
|
||||
<div class="total-row">
|
||||
<span>Subtotal</span>
|
||||
<span>{{ (session.totalPrice - session.setupCostChf) | currency:'CHF' }}</span>
|
||||
</div>
|
||||
<div class="total-row">
|
||||
<span>Setup Fee</span>
|
||||
<span>{{ session.setupCostChf | currency:'CHF' }}</span>
|
||||
</div>
|
||||
<div class="divider"></div>
|
||||
<div class="total-row grand-total">
|
||||
<span>Total</span>
|
||||
<span>{{ session.totalPrice | currency:'CHF' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
292
frontend/src/app/features/checkout/checkout.component.scss
Normal file
292
frontend/src/app/features/checkout/checkout.component.scss
Normal file
@@ -0,0 +1,292 @@
|
||||
.checkout-page {
|
||||
padding: 3rem 1rem;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.checkout-layout {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 380px;
|
||||
gap: var(--space-8);
|
||||
align-items: start;
|
||||
|
||||
@media (max-width: 900px) {
|
||||
grid-template-columns: 1fr;
|
||||
gap: var(--space-6);
|
||||
}
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 2rem;
|
||||
font-weight: 700;
|
||||
margin-bottom: var(--space-6);
|
||||
color: var(--color-heading);
|
||||
}
|
||||
|
||||
.form-card {
|
||||
margin-bottom: var(--space-6);
|
||||
background: var(--color-bg-surface);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-lg);
|
||||
overflow: hidden;
|
||||
|
||||
.card-header {
|
||||
padding: var(--space-4) var(--space-6);
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
background: var(--color-bg-subtle);
|
||||
|
||||
h3 {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 600;
|
||||
color: var(--color-heading);
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.card-content {
|
||||
padding: var(--space-6);
|
||||
}
|
||||
}
|
||||
|
||||
.form-row {
|
||||
display: flex;
|
||||
gap: var(--space-4);
|
||||
margin-bottom: var(--space-4);
|
||||
|
||||
&.three-cols {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 2fr 1fr;
|
||||
gap: var(--space-4);
|
||||
}
|
||||
|
||||
app-input {
|
||||
flex: 1;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
flex-direction: column;
|
||||
&.three-cols {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* User Type Selector Styles */
|
||||
.user-type-selector {
|
||||
display: flex;
|
||||
background-color: var(--color-bg-subtle);
|
||||
border-radius: var(--radius-md);
|
||||
padding: 4px;
|
||||
margin-bottom: var(--space-4);
|
||||
gap: 4px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.type-option {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
padding: 8px 16px;
|
||||
border-radius: var(--radius-sm);
|
||||
cursor: pointer;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
color: var(--color-text-muted);
|
||||
transition: all 0.2s ease;
|
||||
user-select: none;
|
||||
|
||||
&:hover { color: var(--color-text); }
|
||||
|
||||
&.selected {
|
||||
background-color: var(--color-brand);
|
||||
color: #000;
|
||||
font-weight: 600;
|
||||
box-shadow: 0 1px 2px rgba(0,0,0,0.05);
|
||||
}
|
||||
}
|
||||
|
||||
.shipping-option {
|
||||
margin: var(--space-6) 0;
|
||||
}
|
||||
|
||||
/* Custom Checkbox */
|
||||
.checkbox-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
position: relative;
|
||||
padding-left: 30px;
|
||||
cursor: pointer;
|
||||
font-size: 1rem;
|
||||
user-select: none;
|
||||
color: var(--color-text);
|
||||
|
||||
input {
|
||||
position: absolute;
|
||||
opacity: 0;
|
||||
cursor: pointer;
|
||||
height: 0;
|
||||
width: 0;
|
||||
|
||||
&:checked ~ .checkmark {
|
||||
background-color: var(--color-brand);
|
||||
border-color: var(--color-brand);
|
||||
|
||||
&:after {
|
||||
display: block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.checkmark {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
left: 0;
|
||||
transform: translateY(-50%);
|
||||
height: 20px;
|
||||
width: 20px;
|
||||
background-color: var(--color-bg-surface);
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: var(--radius-sm);
|
||||
transition: all 0.2s;
|
||||
|
||||
&:after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
display: none;
|
||||
left: 6px;
|
||||
top: 2px;
|
||||
width: 6px;
|
||||
height: 12px;
|
||||
border: solid white;
|
||||
border-width: 0 2px 2px 0;
|
||||
transform: rotate(45deg);
|
||||
}
|
||||
}
|
||||
|
||||
&:hover input ~ .checkmark {
|
||||
border-color: var(--color-brand);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
.checkout-summary-section {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.sticky-card {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
/* Inherits styles from .form-card */
|
||||
}
|
||||
|
||||
.summary-items {
|
||||
margin-bottom: var(--space-6);
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.summary-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: flex-start;
|
||||
padding: var(--space-3) 0;
|
||||
border-bottom: 1px solid var(--color-border);
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.item-details {
|
||||
flex: 1;
|
||||
|
||||
.item-name {
|
||||
display: block;
|
||||
font-weight: 500;
|
||||
margin-bottom: var(--space-1);
|
||||
word-break: break-all;
|
||||
color: var(--color-text);
|
||||
}
|
||||
|
||||
.item-specs {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-2);
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-text-muted);
|
||||
|
||||
.color-dot {
|
||||
width: 12px;
|
||||
height: 12px;
|
||||
border-radius: 50%;
|
||||
display: inline-block;
|
||||
border: 1px solid var(--color-border);
|
||||
}
|
||||
}
|
||||
|
||||
.item-specs-sub {
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-text-muted);
|
||||
margin-top: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.item-price {
|
||||
font-weight: 600;
|
||||
margin-left: var(--space-3);
|
||||
white-space: nowrap;
|
||||
color: var(--color-heading);
|
||||
}
|
||||
}
|
||||
|
||||
.summary-totals {
|
||||
background: var(--color-bg-subtle);
|
||||
padding: var(--space-4);
|
||||
border-radius: var(--radius-md);
|
||||
margin-top: var(--space-4);
|
||||
|
||||
.total-row {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
margin-bottom: var(--space-2);
|
||||
color: var(--color-text);
|
||||
|
||||
&.grand-total {
|
||||
color: var(--color-heading);
|
||||
font-weight: 700;
|
||||
font-size: 1.25rem;
|
||||
margin-top: var(--space-3);
|
||||
padding-top: var(--space-3);
|
||||
border-top: 1px solid var(--color-border);
|
||||
margin-bottom: 0;
|
||||
}
|
||||
}
|
||||
|
||||
.divider {
|
||||
display: none; // Handled by border-top in grand-total
|
||||
}
|
||||
}
|
||||
|
||||
.actions {
|
||||
margin-top: var(--space-6);
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
|
||||
app-button {
|
||||
width: 100%;
|
||||
|
||||
@media (min-width: 900px) {
|
||||
width: auto;
|
||||
min-width: 200px;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.error-message {
|
||||
color: var(--color-danger);
|
||||
background: var(--color-danger-subtle);
|
||||
padding: var(--space-4);
|
||||
border-radius: var(--radius-md);
|
||||
margin-bottom: var(--space-6);
|
||||
border: 1px solid var(--color-danger);
|
||||
}
|
||||
|
||||
187
frontend/src/app/features/checkout/checkout.component.ts
Normal file
187
frontend/src/app/features/checkout/checkout.component.ts
Normal file
@@ -0,0 +1,187 @@
|
||||
import { Component, inject, OnInit, signal } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { Router, ActivatedRoute } from '@angular/router';
|
||||
import { QuoteEstimatorService } from '../calculator/services/quote-estimator.service';
|
||||
import { AppInputComponent } from '../../shared/components/app-input/app-input.component';
|
||||
import { AppButtonComponent } from '../../shared/components/app-button/app-button.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-checkout',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
ReactiveFormsModule,
|
||||
AppInputComponent,
|
||||
AppButtonComponent
|
||||
],
|
||||
templateUrl: './checkout.component.html',
|
||||
styleUrls: ['./checkout.component.scss']
|
||||
})
|
||||
export class CheckoutComponent implements OnInit {
|
||||
private fb = inject(FormBuilder);
|
||||
private quoteService = inject(QuoteEstimatorService);
|
||||
private router = inject(Router);
|
||||
private route = inject(ActivatedRoute);
|
||||
|
||||
checkoutForm: FormGroup;
|
||||
sessionId: string | null = null;
|
||||
loading = false;
|
||||
error: string | null = null;
|
||||
isSubmitting = signal(false); // Add signal for submit state
|
||||
quoteSession = signal<any>(null); // Add signal for session details
|
||||
|
||||
constructor() {
|
||||
this.checkoutForm = this.fb.group({
|
||||
email: ['', [Validators.required, Validators.email]],
|
||||
phone: ['', Validators.required],
|
||||
customerType: ['PRIVATE', Validators.required], // Default to PRIVATE
|
||||
|
||||
shippingSameAsBilling: [true],
|
||||
|
||||
billingAddress: this.fb.group({
|
||||
firstName: ['', Validators.required],
|
||||
lastName: ['', Validators.required],
|
||||
companyName: [''],
|
||||
addressLine1: ['', Validators.required],
|
||||
addressLine2: [''],
|
||||
zip: ['', Validators.required],
|
||||
city: ['', Validators.required],
|
||||
countryCode: ['CH', Validators.required]
|
||||
}),
|
||||
|
||||
shippingAddress: this.fb.group({
|
||||
firstName: [''],
|
||||
lastName: [''],
|
||||
companyName: [''],
|
||||
addressLine1: [''],
|
||||
addressLine2: [''],
|
||||
zip: [''],
|
||||
city: [''],
|
||||
countryCode: ['CH']
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
get isCompany(): boolean {
|
||||
return this.checkoutForm.get('customerType')?.value === 'BUSINESS';
|
||||
}
|
||||
|
||||
setCustomerType(isCompany: boolean) {
|
||||
const type = isCompany ? 'BUSINESS' : 'PRIVATE';
|
||||
this.checkoutForm.patchValue({ customerType: type });
|
||||
|
||||
// Update validators based on type
|
||||
const billingGroup = this.checkoutForm.get('billingAddress') as FormGroup;
|
||||
const companyControl = billingGroup.get('companyName');
|
||||
|
||||
if (isCompany) {
|
||||
companyControl?.setValidators([Validators.required]);
|
||||
} else {
|
||||
companyControl?.clearValidators();
|
||||
}
|
||||
companyControl?.updateValueAndValidity();
|
||||
}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.route.queryParams.subscribe(params => {
|
||||
this.sessionId = params['session'];
|
||||
if (!this.sessionId) {
|
||||
this.error = 'No active session found. Please start a new quote.';
|
||||
this.router.navigate(['/']); // Redirect if no session
|
||||
return;
|
||||
}
|
||||
|
||||
this.loadSessionDetails();
|
||||
});
|
||||
|
||||
// Toggle shipping validation based on checkbox
|
||||
this.checkoutForm.get('shippingSameAsBilling')?.valueChanges.subscribe(isSame => {
|
||||
const shippingGroup = this.checkoutForm.get('shippingAddress') as FormGroup;
|
||||
if (isSame) {
|
||||
shippingGroup.disable();
|
||||
} else {
|
||||
shippingGroup.enable();
|
||||
}
|
||||
});
|
||||
|
||||
// Initial state
|
||||
this.checkoutForm.get('shippingAddress')?.disable();
|
||||
}
|
||||
|
||||
loadSessionDetails() {
|
||||
if (!this.sessionId) return; // Ensure sessionId is present before fetching
|
||||
this.quoteService.getQuoteSession(this.sessionId).subscribe({
|
||||
next: (session) => {
|
||||
this.quoteSession.set(session);
|
||||
console.log('Loaded session:', session);
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Failed to load session', err);
|
||||
this.error = 'Failed to load session details. Please try again.';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
onSubmit() {
|
||||
if (this.checkoutForm.invalid) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isSubmitting.set(true);
|
||||
this.error = null; // Clear previous errors
|
||||
const formVal = this.checkoutForm.getRawValue(); // Use getRawValue to include disabled fields
|
||||
|
||||
// Construct request object matching backend DTO based on original form structure
|
||||
const orderRequest = {
|
||||
customer: {
|
||||
email: formVal.email,
|
||||
phone: formVal.phone,
|
||||
customerType: formVal.customerType,
|
||||
// Assuming firstName, lastName, companyName for customer come from billingAddress if not explicitly in contact group
|
||||
firstName: formVal.billingAddress.firstName,
|
||||
lastName: formVal.billingAddress.lastName,
|
||||
companyName: formVal.billingAddress.companyName
|
||||
},
|
||||
billingAddress: {
|
||||
firstName: formVal.billingAddress.firstName,
|
||||
lastName: formVal.billingAddress.lastName,
|
||||
companyName: formVal.billingAddress.companyName,
|
||||
addressLine1: formVal.billingAddress.addressLine1,
|
||||
addressLine2: formVal.billingAddress.addressLine2,
|
||||
zip: formVal.billingAddress.zip,
|
||||
city: formVal.billingAddress.city,
|
||||
countryCode: formVal.billingAddress.countryCode
|
||||
},
|
||||
shippingAddress: formVal.shippingSameAsBilling ? null : {
|
||||
firstName: formVal.shippingAddress.firstName,
|
||||
lastName: formVal.shippingAddress.lastName,
|
||||
companyName: formVal.shippingAddress.companyName,
|
||||
addressLine1: formVal.shippingAddress.addressLine1,
|
||||
addressLine2: formVal.shippingAddress.addressLine2,
|
||||
zip: formVal.shippingAddress.zip,
|
||||
city: formVal.shippingAddress.city,
|
||||
countryCode: formVal.shippingAddress.countryCode
|
||||
},
|
||||
shippingSameAsBilling: formVal.shippingSameAsBilling
|
||||
};
|
||||
|
||||
if (!this.sessionId) {
|
||||
this.error = 'No active session found. Cannot create order.';
|
||||
this.isSubmitting.set(false);
|
||||
return;
|
||||
}
|
||||
|
||||
this.quoteService.createOrder(this.sessionId, orderRequest).subscribe({
|
||||
next: (order) => {
|
||||
console.log('Order created', order);
|
||||
this.router.navigate(['/payment', order.id]);
|
||||
},
|
||||
error: (err) => {
|
||||
console.error('Order creation failed', err);
|
||||
this.isSubmitting.set(false);
|
||||
this.error = 'Failed to create order. Please try again.';
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user