Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. import { Routes, RouterModule } from '@angular/router';
  2. import { RegisterComponent } from './pages/user/register/register.component';
  3. import { LoginComponent } from './pages/user/login/login.component';
  4. import { PageNotFoundComponent } from './shared/components/page-not-found/page-not-found.component';
  5.  
  6. const routes: Routes = [
  7. {
  8. path: '',
  9. redirectTo: 'login',
  10. pathMatch: 'full'
  11. },
  12. {
  13. path: 'login',
  14. component: LoginComponent
  15. },
  16. {
  17. path: 'products',
  18. loadChildren: './pages/products/products.module#ProductsModule'
  19. },
  20. {
  21. path: 'register',
  22. component: RegisterComponent
  23. },
  24. { path: '**', component: PageNotFoundComponent }
  25. ];
  26. @NgModule({
  27. imports: [RouterModule.forRoot(routes)],
  28. exports: [RouterModule]
  29. })
  30. export class AppRoutingModule {}
  31.  
  32.  
  33.  
  34. Product actions:
  35.  
  36. import { Action } from '@ngrx/store';
  37. import { Product } from '../../../shared/models/products.model';
  38.  
  39. export const GET_PRODUCT = '[PRODUCT] Get Product';
  40. export const GET_PRODUCTS = '[PRODUCT] Get Products';
  41. export const ADD_CART = '[CART] Add Cart';
  42. export const REMOVE_CART = '[CART] Add Cart';
  43. export const ADD_FAV = '[FAV] Add Fav';
  44. export const REMOVE_FAV = '[FAV] Remove Fav';
  45.  
  46. // export const getProducts = createAction ('[PRODUCT] Get Products', props<{products: Product[]}>());
  47. // export const getProduct = createAction ('[PRODUCT] Get Product', props<{products: Product}>());
  48.  
  49. export class GetProducts implements Action {
  50. public readonly type = GET_PRODUCTS;
  51. constructor(public payload: Product[]) {}
  52. }
  53.  
  54. export class GetProduct implements Action {
  55. public readonly type = GET_PRODUCT;
  56. constructor(public payload: Product) {}
  57. }
  58.  
  59. export class AddCart implements Action {
  60. public readonly type = ADD_CART;
  61. constructor(public payload: Product) {}
  62. }
  63.  
  64. export class RemoveCart implements Action {
  65. public readonly type = REMOVE_FAV;
  66. // Since we need to remove a particular product we need to pass the index
  67. constructor(public payload: number) {}
  68. }
  69.  
  70. export type ProductActions = GetProducts | AddCart | RemoveCart | GetProduct;
  71.  
  72.  
  73. product reducer
  74.  
  75. // Takes the incoming action and decide what to do with it
  76.  
  77. import * as AllProductActions from '../actios/product-actions';
  78. import { ProductState, InitialProductState } from '../state/product.state';
  79.  
  80. export function productReducers(
  81. state = InitialProductState,
  82. action: AllProductActions.ProductActions
  83. ) {
  84. switch (action.type) {
  85. case AllProductActions.GET_PRODUCT:
  86. return { ...state, products: action.payload };
  87. }
  88. }
  89.  
  90.  
  91. App.state
  92.  
  93. import { Product } from './../../models/products.model';
  94.  
  95.  
  96.  
  97. export interface AppState {
  98. readonly product: Product[];
  99. }
  100.  
  101. // export const InitialAppState: AppState = {
  102. // products: InitialProductState
  103. // };
  104.  
  105. // export function getInitialState(): AppState {
  106. // return InitialAppState;
  107. // }
  108.  
  109.  
  110. Product State:
  111.  
  112. import { Product } from '../../../shared/models/products.model';
  113.  
  114. export interface ProductState {
  115. products: Product[];
  116. selectedProduct: Product;
  117. }
  118.  
  119. export const InitialProductState: ProductState = {
  120. products: null,
  121. selectedProduct: null
  122. };
  123.  
  124. Product Service
  125.  
  126. import { Injectable, Inject } from '@angular/core';
  127. import { HttpClient } from '@angular/common/http';
  128. import { Observable } from 'rxjs';
  129. import {shareReplay} from 'rxjs/operators';
  130. import { tap } from 'rxjs/operators';
  131.  
  132. import { Product } from './../models/products.model';
  133. import { environment } from '../../../environments/environment';
  134. import { ProductActions } from './../store/actios/product-actions';
  135.  
  136.  
  137. @Injectable()
  138. export class ProductsService {
  139.  
  140.  
  141. constructor( private httpClient: HttpClient) {
  142.  
  143. }
  144. getProducts(): Observable<any> {
  145. return this.httpClient.get(`${environment.services["get-products"]}`).pipe(
  146. shareReplay(1)
  147. )
  148.  
  149. }
  150.  
  151. }
  152. Products component:
  153.  
  154. import { Product } from './../../../shared/models/products.model';
  155. import { Observable } from 'rxjs';
  156. import { GetProducts, GetProduct } from './../../../shared/store/actios/product-actions';
  157. import { AppState } from './../../../shared/store/state/app.state';
  158. import { Component, OnInit } from '@angular/core';
  159. import { ProductsService } from './../../../shared/services/products.service';
  160. import { Store, select } from '@ngrx/store';
  161. import { selectProductList } from 'src/app/shared/store/selectors/product.selector';
  162.  
  163. @Component({
  164. selector: 'hero-products',
  165. templateUrl: './products.component.html',
  166. styleUrls: ['./products.component.scss']
  167. })
  168. export class ProductsComponent implements OnInit {
  169.  
  170. products: Observable<Product[]>;
  171.  
  172. constructor(private svc: ProductsService, private store: Store<AppState>) { }
  173.  
  174. ngOnInit() {
  175. this.svc.getProducts().subscribe(data => {
  176. this.products = data;
  177. });
  178. }
  179.  
  180. viewProduct(product: Product) {
  181. this.store.dispatch(new GetProduct(product));
  182. }
  183.  
  184. }
  185.  
  186.  
  187. View product:
  188.  
  189. import { Product } from './../../../shared/models/products.model';
  190. import { GetProduct } from './../../../shared/store/actios/product-actions';
  191. import { Component, OnInit } from '@angular/core';
  192. import { Store } from '@ngrx/store';
  193.  
  194. import { AppState } from './../../../shared/store/state/app.state';
  195. import { Observable } from 'rxjs';
  196.  
  197. @Component({
  198. selector: 'hero-view-product',
  199. templateUrl: './view-product.component.html',
  200. styleUrls: ['./view-product.component.scss']
  201. })
  202. export class ViewProductComponent implements OnInit {
  203.  
  204. private subscription: any;
  205. selectedProduct: Observable<Product[]>;
  206.  
  207. constructor(private _store: Store<AppState>) { }
  208.  
  209. ngOnInit() {
  210. this._store.select('product').subscribe(data=> {
  211. console.log(data) ;
  212. });
  213. console.log(this.selectedProduct, 'kk')
  214. // this.subscription = this._store
  215. // .select()
  216. // .subscribe(people => {
  217. // console.log(people, 'ppppppp');
  218. // this.selectedProduct = people;
  219. // });
  220. }
  221.  
  222.  
  223.  
  224. }````
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement