| @@ -14,6 +14,7 @@ import { OfferService } from './services/offer.service'; | |||||
| import { OutletService } from './services/outlet.service'; | import { OutletService } from './services/outlet.service'; | ||||
| import { CartItemService } from './services/cart-item.service'; | import { CartItemService } from './services/cart-item.service'; | ||||
| import { UserDataService } from './services/user-data.service'; | import { UserDataService } from './services/user-data.service'; | ||||
| import { OrderService } from './services/order.service'; | |||||
| import { AppComponent } from './app.component'; | import { AppComponent } from './app.component'; | ||||
| import { AppRoutingModule } from './app-routing.module'; | import { AppRoutingModule } from './app-routing.module'; | ||||
| @@ -38,6 +39,7 @@ import { environment } from '../environments/environment'; | |||||
| OutletService, | OutletService, | ||||
| CartItemService, | CartItemService, | ||||
| UserDataService, | UserDataService, | ||||
| OrderService, | |||||
| { provide: RouteReuseStrategy, useClass: IonicRouteStrategy } | { provide: RouteReuseStrategy, useClass: IonicRouteStrategy } | ||||
| ], | ], | ||||
| bootstrap: [AppComponent] | bootstrap: [AppComponent] | ||||
| @@ -78,7 +78,8 @@ | |||||
| <ion-button fill="clear"> ₹ {{ total_savings }} </ion-button> | <ion-button fill="clear"> ₹ {{ total_savings }} </ion-button> | ||||
| </div> | </div> | ||||
| <ion-button shape="round" expand="block" class="pay-button"> Pay Now </ion-button> | |||||
| <ion-button shape="round" expand="block" class="pay-button" (click)="placeOrder()" | |||||
| [disabled]="cart_items.length === 0"> Pay Now </ion-button> | |||||
| <div class="common-semi-modal filter-holder with-border" [ngClass]="{'active' : show_promocodes }"> | <div class="common-semi-modal filter-holder with-border" [ngClass]="{'active' : show_promocodes }"> | ||||
| <header> | <header> | ||||
| @@ -144,7 +145,6 @@ | |||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| </div> | </div> | ||||
| </section> | </section> | ||||
| </div> | </div> | ||||
| @@ -7,6 +7,8 @@ import { IMall } from '../models/mall'; | |||||
| import * as moment from 'moment'; | import * as moment from 'moment'; | ||||
| import { Router } from '@angular/router'; | import { Router } from '@angular/router'; | ||||
| import { IOutlet } from '../models/outlet'; | import { IOutlet } from '../models/outlet'; | ||||
| import { OrderService } from '../services/order.service'; | |||||
| import { ToastController } from '@ionic/angular'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-cart', | selector: 'app-cart', | ||||
| @@ -31,7 +33,9 @@ export class CartPage implements OnInit { | |||||
| private location: Location, | private location: Location, | ||||
| private cartService: CartItemService, | private cartService: CartItemService, | ||||
| private mallService: MallService, | private mallService: MallService, | ||||
| private router: Router | |||||
| private router: Router, | |||||
| private orderService: OrderService, | |||||
| public toastController: ToastController | |||||
| ) { } | ) { } | ||||
| ngOnInit() {} | ngOnInit() {} | ||||
| @@ -222,4 +226,29 @@ export class CartPage implements OnInit { | |||||
| this.cartService.updateCartItems(this.cart_items); | this.cartService.updateCartItems(this.cart_items); | ||||
| } | } | ||||
| placeOrder() { | |||||
| this.orderService.createOrder(this.cart_items).then(() => { | |||||
| this.presentToast('Order has been created! :-)', 'dark'); | |||||
| this.cartService.clearCartItems(); | |||||
| this.router.navigate(['/profile']); | |||||
| }, () => { | |||||
| this.presentToast('Failed to create the order, We\'re sorry for the inconvenience', 'danger'); | |||||
| }); | |||||
| } | |||||
| async presentToast(text: string, color: string) { | |||||
| const toast = await this.toastController.create({ | |||||
| message: text, | |||||
| position: 'bottom', | |||||
| duration: 4000, | |||||
| color: color, | |||||
| buttons: [ | |||||
| { | |||||
| text: 'Done', | |||||
| } | |||||
| ] | |||||
| }); | |||||
| toast.present(); | |||||
| } | |||||
| } | } | ||||
| @@ -2,8 +2,7 @@ import { Component, OnInit } from '@angular/core'; | |||||
| import { IMall } from '../models/mall'; | import { IMall } from '../models/mall'; | ||||
| import { MallService } from '../services/mall.service'; | import { MallService } from '../services/mall.service'; | ||||
| import { CartItemService } from '../services/cart-item.service'; | import { CartItemService } from '../services/cart-item.service'; | ||||
| import CartItem from '../models/cart-item'; | |||||
| import * as moment from 'moment'; | |||||
| import { OrderService } from '../services/order.service'; | |||||
| @Component({ | @Component({ | ||||
| selector: 'app-orders', | selector: 'app-orders', | ||||
| @@ -16,27 +15,11 @@ export class OrdersComponent implements OnInit { | |||||
| constructor( | constructor( | ||||
| private mallService: MallService, | private mallService: MallService, | ||||
| private cartService: CartItemService, | |||||
| private orderService: OrderService | |||||
| ) { } | ) { } | ||||
| ngOnInit() { | ngOnInit() { | ||||
| this.mallService.getAllMalls().then((malls: Array<IMall>) => { | |||||
| this.all_malls = malls; | |||||
| }); | |||||
| this.cartService.getAllCartItems().then((cart_items: Array<CartItem>) => { | |||||
| this.cart_items = cart_items; | |||||
| let i: number; | |||||
| for (i = 0; i < this.cart_items.length; i += 1) { | |||||
| let mall = this.all_malls.find(mall => mall.id === this.cart_items[i].mall_id); | |||||
| let outlet = mall.outlets.find(outlet => outlet.id === this.cart_items[i].outlet_id); | |||||
| this.cart_items[i].menu_details = outlet.menu_items.find(item => item.id === this.cart_items[i].menu_item); | |||||
| } | |||||
| console.log(this.cart_items); | |||||
| }); | |||||
| } | } | ||||
| } | } | ||||
| @@ -50,7 +50,6 @@ export class OutletDetailsPage implements OnInit { | |||||
| this.mall_details = data; | this.mall_details = data; | ||||
| this.outlet_details = this.mall_details.outlets.find((outlet) => outlet.id === outlet_id); | this.outlet_details = this.mall_details.outlets.find((outlet) => outlet.id === outlet_id); | ||||
| this.temp_outlet_details = JSON.parse(JSON.stringify(this.outlet_details)); | this.temp_outlet_details = JSON.parse(JSON.stringify(this.outlet_details)); | ||||
| console.log(this.temp_outlet_details); | |||||
| }); | }); | ||||
| } | } | ||||
| @@ -28,4 +28,8 @@ export class CartItemService { | |||||
| }); | }); | ||||
| } | } | ||||
| async clearCartItems() { | |||||
| return await this.storage.set('cart_items', '[]'); | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,12 @@ | |||||
| import { TestBed } from '@angular/core/testing'; | |||||
| import { OrderService } from './order.service'; | |||||
| describe('OrderService', () => { | |||||
| beforeEach(() => TestBed.configureTestingModule({})); | |||||
| it('should be created', () => { | |||||
| const service: OrderService = TestBed.get(OrderService); | |||||
| expect(service).toBeTruthy(); | |||||
| }); | |||||
| }); | |||||
| @@ -0,0 +1,45 @@ | |||||
| import { Injectable } from '@angular/core'; | |||||
| import { Storage } from '@ionic/storage'; | |||||
| import * as uuid from 'uuid'; | |||||
| import CartItem from '../models/cart-item'; | |||||
| @Injectable({ | |||||
| providedIn: 'root' | |||||
| }) | |||||
| export class OrderService { | |||||
| orders: any = []; | |||||
| constructor( | |||||
| private storage: Storage | |||||
| ) { } | |||||
| async createOrder(cart_items: Array<CartItem>) { | |||||
| return this.storage.get('orders').then((data: string) => { | |||||
| if (data === null) { | |||||
| this.orders.push({ | |||||
| id: uuid(), | |||||
| order_items: cart_items | |||||
| }); | |||||
| this.storage.set('orders', JSON.stringify(this.orders)).then((data) => { | |||||
| console.log(data); | |||||
| }); | |||||
| } else { | |||||
| this.orders = JSON.parse(data); | |||||
| this.orders.push({ | |||||
| id: uuid(), | |||||
| order_items: cart_items | |||||
| }); | |||||
| this.storage.set('orders', JSON.stringify(this.orders)); | |||||
| } | |||||
| }, () => { | |||||
| console.log("failed"); | |||||
| }); | |||||
| } | |||||
| async getCreatedOrders() { | |||||
| return await this.storage.get('orders').then((data: string) => { | |||||
| return JSON.parse(data); | |||||
| }); | |||||
| } | |||||
| } | |||||
| @@ -1,30 +1,10 @@ | |||||
| import { Injectable } from '@angular/core'; | import { Injectable } from '@angular/core'; | ||||
| import CartItem from '../models/cart-item'; | |||||
| import { Storage } from '@ionic/storage'; | |||||
| @Injectable({ | @Injectable({ | ||||
| providedIn: 'root' | providedIn: 'root' | ||||
| }) | }) | ||||
| export class UserDataService { | export class UserDataService { | ||||
| orders: Array<CartItem> = []; | |||||
| constructor( | constructor( | ||||
| private storage: Storage | |||||
| ) { } | |||||
| async updateUserOrders(orders: Array<CartItem>) { | |||||
| this.orders = orders; | |||||
| this.storage.set('user_orders', JSON.stringify(this.orders)); | |||||
| return await this.orders; | |||||
| } | |||||
| async getAllUserOrders() { | |||||
| return await this.storage.get('user_orders').then((data: string) => { | |||||
| if (data) { | |||||
| return JSON.parse(data); | |||||
| } else { | |||||
| return []; | |||||
| } | |||||
| }); | |||||
| } | |||||
| ) { } | |||||
| } | } | ||||