Skip to content

Commit 779edcd

Browse files
committed
Melhorias no processo de checkout
1 parent 38e471f commit 779edcd

File tree

6 files changed

+58
-40
lines changed

6 files changed

+58
-40
lines changed

src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ import { OrderService } from './services/order.service';
7373
{ path: 'login', component: LoginComponent },
7474

7575
{ path: 'check-out', component: CheckOutComponent, canActivate: [AuthGuard] },
76-
{ path: 'sucesso', component: OrderSuccessComponent, canActivate: [AuthGuard] },
76+
{ path: 'sucesso/:id', component: OrderSuccessComponent, canActivate: [AuthGuard] },
7777
{ path: 'meus-pedidos', component: MyOrdersComponent, canActivate: [AuthGuard] },
7878

7979
{ path: 'admin/produtos/novo', component: ProductFormComponent, canActivate: [AuthGuard, AdminAuthGuard] },

src/app/check-out/check-out.component.html

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@ <h2>Envio</h2>
44
<form #f="ngForm" (ngSubmit)="save(f.value)">
55
<div class="form-group">
66
<label for="name">Nome</label>
7-
<input name="name" id="name" [(ngModel)]="order.name" type="text" class="form-control">
7+
<input #name="ngModel" name="name" id="name" required [(ngModel)]="shipping.name" type="text" class="form-control">
8+
<div *ngIf="name.touched && name.invalid" class="alert alert-danger">O nome é obrigatório</div>
89
</div>
910
<div class="form-group">
1011
<label for="address">Endereço</label>
11-
<input name="address" [(ngModel)]="order.address" id="address" type="text" class="form-control" placeholder="Linha 1"><br>
12-
<input name="address1" id="address1" type="text" class="form-control" placeholder="Linha 2">
12+
<input #address="ngModel" name="address" required [(ngModel)]="shipping.address" id="address" type="text" class="form-control" placeholder="Linha 1">
13+
<div *ngIf="address.touched && address.invalid" class="alert alert-danger">O endereço é obrigatório</div><br>
14+
<input name="address1" id="address1" [(ngModel)]="shipping.address1" type="text" class="form-control" placeholder="Linha 2">
1315
</div>
1416
<div class="form-group">
1517
<label for="city">Cidade</label>
16-
<input #city="ngModel" [(ngModel)]="order.city" name="city" id="city" type="text" class="form-control">
18+
<input #city="ngModel" required [(ngModel)]="shipping.city" name="city" id="city" type="text" class="form-control">
19+
<div *ngIf="city.touched && city.invalid" class="alert alert-danger">A cidade é obrigatória</div>
1720
</div>
18-
<button class="btn btn-primary">Realizar pedido</button>
21+
<button [disabled]="f.invalid" class="btn btn-primary">Realizar pedido</button>
1922
</form>
2023
</div>
2124
<div class="col-md-6">
22-
<shopping-cart-card [shopping-cart]="cart$ | async"></shopping-cart-card>
25+
<shopping-cart-card [shopping-cart]="cart"></shopping-cart-card>
2326
</div>
2427
</div>
Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { ShoppingCart } from './../models/shopping-cart';
2+
import { Component, OnInit, OnDestroy } from '@angular/core';
23
import { Order } from '../models/order';
34
import { ShoppingCartService } from '../services/shopping-cart.service';
45
import { OrderService } from '../services/order.service';
56
import { Router } from '@angular/router';
6-
import { UserService } from '../services/user.service';
7+
import { Subscription } from 'rxjs/Subscription';
8+
import { AuthService } from '../services/auth.service';
79

810
@Component({
911
selector: 'app-check-out',
1012
templateUrl: './check-out.component.html',
1113
styleUrls: ['./check-out.component.css']
1214
})
13-
export class CheckOutComponent implements OnInit {
15+
export class CheckOutComponent implements OnInit, OnDestroy {
1416

15-
order: Order = {
16-
key: '',
17-
name: '',
18-
address: '',
19-
city: '',
20-
cart: null
21-
};
22-
23-
cart$;
17+
shipping = {};
18+
cart: ShoppingCart;
19+
cartSubscription: Subscription;
20+
userId: string;
21+
userSubscription: Subscription;
2422

2523
constructor(
2624
private cartService: ShoppingCartService,
2725
private orderService: OrderService,
28-
private userService: UserService,
26+
private authService: AuthService,
2927
private router: Router) { }
3028

3129
async ngOnInit() {
32-
this.cart$ = await this.cartService.getCart();
30+
const cart$ = await this.cartService.getCart();
31+
this.cartSubscription = cart$.subscribe(cart => this.cart = cart);
32+
this.userSubscription = this.authService.user$.subscribe(user => this.userId = user.uid);
33+
}
34+
35+
ngOnDestroy() {
36+
this.cartSubscription.unsubscribe();
37+
this.userSubscription.unsubscribe();
3338
}
3439

35-
save(order) {
36-
this.orderService.create(order);
37-
this.router.navigate(['/order-success']);
40+
async save() {
41+
const order = new Order(this.userId, this.shipping, this.cart);
42+
const result = await this.orderService.placeOrder(order);
43+
this.router.navigate(['/sucesso', result.key]);
3844
}
3945
}

src/app/models/order.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@ import { ShoppingCart } from './shopping-cart';
22
import { AppUser } from './app-user';
33

44
export class Order {
5+
datePlaced: number;
6+
items: any[];
57

6-
key: string;
7-
name: string;
8-
address: string;
9-
city: string;
10-
cart: ShoppingCart;
8+
constructor(public uderId: string, public shipping: any, shoppingCart: ShoppingCart) {
9+
this.datePlaced = new Date().getTime();
10+
11+
this.items = shoppingCart.items.map(i => {
12+
return {
13+
product: {
14+
title: i.title,
15+
imageUrl: i.imageUrl,
16+
price: i.price
17+
},
18+
quantity: i.quantity,
19+
totalPrice: i.totalPrice
20+
};
21+
});
22+
}
1123
}

src/app/services/order.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import { Injectable } from '@angular/core';
22
import { Order } from '../models/order';
33
import { AngularFireDatabase } from 'angularfire2/database';
4+
import { ShoppingCartService } from './shopping-cart.service';
45

56
@Injectable()
67
export class OrderService {
78

8-
constructor(private db: AngularFireDatabase) { }
9+
constructor(private db: AngularFireDatabase, private service: ShoppingCartService) { }
910

10-
create(order: Order) {
11-
return this.db.list('/orders').push(order);
11+
async placeOrder(order: Order) {
12+
const result = await this.db.list('/orders').push(order);
13+
this.service.clearCart();
14+
return result;
1215
}
1316

1417
getAll() {

src/app/shopping-cart/shopping-cart.component.html

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ <h1>Carrinho</h1>
22
<div class="row col-10" *ngIf="cart$ | async as cart">
33
<p>
44
Você tem {{ cart.totalItemsCount }} itens no seu carrinho.
5+
<button *ngIf="cart.items.length" class="btn btn-light btn-sm" (click)="clearCart()">Limpar Carrinho</button>
56
</p>
67
<table class="table">
78
<thead>
@@ -31,12 +32,5 @@ <h1>Carrinho</h1>
3132
</tr>
3233
</tfoot>
3334
</table>
34-
</div>
35-
<div class="row">
36-
<div class="col-md-2">
37-
<button routerLink="/check-out" class="btn btn-primary">Check-out</button>
38-
</div>
39-
<div class="col-md-2">
40-
<button class="btn btn-danger" (click)="clearCart()">Limpar Carrinho</button>
41-
</div>
35+
<button *ngIf="cart.items.length" routerLink="/check-out" class="btn btn-primary">Check-out</button>
4236
</div>

0 commit comments

Comments
 (0)