├── .browserslistrc ├── .editorconfig ├── .gitignore ├── README.md ├── angular.json ├── karma.conf.js ├── package-lock.json ├── package.json ├── src ├── app │ ├── app-routing.module.ts │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── component │ │ ├── cart │ │ │ ├── cart.component.html │ │ │ ├── cart.component.scss │ │ │ ├── cart.component.spec.ts │ │ │ └── cart.component.ts │ │ ├── header │ │ │ ├── header.component.html │ │ │ ├── header.component.scss │ │ │ ├── header.component.spec.ts │ │ │ └── header.component.ts │ │ └── products │ │ │ ├── products.component.html │ │ │ ├── products.component.scss │ │ │ ├── products.component.spec.ts │ │ │ └── products.component.ts │ ├── service │ │ ├── api.service.ts │ │ └── cart.service.ts │ └── shared │ │ ├── filter.pipe.spec.ts │ │ └── filter.pipe.ts ├── assets │ └── .gitkeep ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── main.ts ├── polyfills.ts ├── styles.scss └── test.ts ├── tsconfig.app.json ├── tsconfig.json └── tsconfig.spec.json /.browserslistrc: -------------------------------------------------------------------------------- 1 | # This file is used by the build system to adjust CSS and JS output to support the specified browsers below. 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | 5 | # For the full list of supported browsers by the Angular framework, please see: 6 | # https://angular.io/guide/browser-support 7 | 8 | # You can see what browsers were selected by your queries by running: 9 | # npx browserslist 10 | 11 | last 1 Chrome version 12 | last 1 Firefox version 13 | last 2 Edge major versions 14 | last 2 Safari major versions 15 | last 2 iOS major versions 16 | Firefox ESR 17 | not IE 11 # Angular supports IE 11 only as an opt-in. To opt-in, remove the 'not' prefix on this line. 18 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.ts] 12 | quote_type = single 13 | 14 | [*.md] 15 | max_line_length = off 16 | trim_trailing_whitespace = false 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events*.json 15 | 16 | # IDEs and editors 17 | /.idea 18 | .project 19 | .classpath 20 | .c9/ 21 | *.launch 22 | .settings/ 23 | *.sublime-workspace 24 | 25 | # IDE - VSCode 26 | .vscode/* 27 | !.vscode/settings.json 28 | !.vscode/tasks.json 29 | !.vscode/launch.json 30 | !.vscode/extensions.json 31 | .history/* 32 | 33 | # misc 34 | /.sass-cache 35 | /connect.lock 36 | /coverage 37 | /libpeerconnection.log 38 | npm-debug.log 39 | yarn-error.log 40 | testem.log 41 | /typings 42 | 43 | # System Files 44 | .DS_Store 45 | Thumbs.db 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AddToCart 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 12.0.4. 4 | 5 | ## Development server 6 | 7 | Run `ng serve` for a dev server. Navigate to `http://localhost:4200/`. The app will automatically reload if you change any of the source files. 8 | 9 | ## Code scaffolding 10 | 11 | Run `ng generate component component-name` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module`. 12 | 13 | ## Build 14 | 15 | Run `ng build` to build the project. The build artifacts will be stored in the `dist/` directory. 16 | 17 | ## Running unit tests 18 | 19 | Run `ng test` to execute the unit tests via [Karma](https://karma-runner.github.io). 20 | 21 | ## Running end-to-end tests 22 | 23 | Run `ng e2e` to execute the end-to-end tests via a platform of your choice. To use this command, you need to first add a package that implements end-to-end testing capabilities. 24 | 25 | ## Further help 26 | 27 | To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page. 28 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "add-to-cart": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss" 11 | }, 12 | "@schematics/angular:application": { 13 | "strict": true 14 | } 15 | }, 16 | "root": "", 17 | "sourceRoot": "src", 18 | "prefix": "app", 19 | "architect": { 20 | "build": { 21 | "builder": "@angular-devkit/build-angular:browser", 22 | "options": { 23 | "outputPath": "dist/add-to-cart", 24 | "index": "src/index.html", 25 | "main": "src/main.ts", 26 | "polyfills": "src/polyfills.ts", 27 | "tsConfig": "tsconfig.app.json", 28 | "inlineStyleLanguage": "scss", 29 | "assets": [ 30 | "src/favicon.ico", 31 | "src/assets" 32 | ], 33 | "styles": [ 34 | "src/styles.scss" 35 | ], 36 | "scripts": [] 37 | }, 38 | "configurations": { 39 | "production": { 40 | "budgets": [ 41 | { 42 | "type": "initial", 43 | "maximumWarning": "500kb", 44 | "maximumError": "1mb" 45 | }, 46 | { 47 | "type": "anyComponentStyle", 48 | "maximumWarning": "2kb", 49 | "maximumError": "4kb" 50 | } 51 | ], 52 | "fileReplacements": [ 53 | { 54 | "replace": "src/environments/environment.ts", 55 | "with": "src/environments/environment.prod.ts" 56 | } 57 | ], 58 | "outputHashing": "all" 59 | }, 60 | "development": { 61 | "buildOptimizer": false, 62 | "optimization": false, 63 | "vendorChunk": true, 64 | "extractLicenses": false, 65 | "sourceMap": true, 66 | "namedChunks": true 67 | } 68 | }, 69 | "defaultConfiguration": "production" 70 | }, 71 | "serve": { 72 | "builder": "@angular-devkit/build-angular:dev-server", 73 | "configurations": { 74 | "production": { 75 | "browserTarget": "add-to-cart:build:production" 76 | }, 77 | "development": { 78 | "browserTarget": "add-to-cart:build:development" 79 | } 80 | }, 81 | "defaultConfiguration": "development" 82 | }, 83 | "extract-i18n": { 84 | "builder": "@angular-devkit/build-angular:extract-i18n", 85 | "options": { 86 | "browserTarget": "add-to-cart:build" 87 | } 88 | }, 89 | "test": { 90 | "builder": "@angular-devkit/build-angular:karma", 91 | "options": { 92 | "main": "src/test.ts", 93 | "polyfills": "src/polyfills.ts", 94 | "tsConfig": "tsconfig.spec.json", 95 | "karmaConfig": "karma.conf.js", 96 | "inlineStyleLanguage": "scss", 97 | "assets": [ 98 | "src/favicon.ico", 99 | "src/assets" 100 | ], 101 | "styles": [ 102 | "src/styles.scss" 103 | ], 104 | "scripts": [] 105 | } 106 | } 107 | } 108 | } 109 | }, 110 | "defaultProject": "add-to-cart" 111 | } 112 | -------------------------------------------------------------------------------- /karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | jasmine: { 17 | // you can add configuration options for Jasmine here 18 | // the possible options are listed at https://jasmine.github.io/api/edge/Configuration.html 19 | // for example, you can disable the random execution with `random: false` 20 | // or set a specific seed with `seed: 4321` 21 | }, 22 | clearContext: false // leave Jasmine Spec Runner output visible in browser 23 | }, 24 | jasmineHtmlReporter: { 25 | suppressAll: true // removes the duplicated traces 26 | }, 27 | coverageReporter: { 28 | dir: require('path').join(__dirname, './coverage/add-to-cart'), 29 | subdir: '.', 30 | reporters: [ 31 | { type: 'html' }, 32 | { type: 'text-summary' } 33 | ] 34 | }, 35 | reporters: ['progress', 'kjhtml'], 36 | port: 9876, 37 | colors: true, 38 | logLevel: config.LOG_INFO, 39 | autoWatch: true, 40 | browsers: ['Chrome'], 41 | singleRun: false, 42 | restartOnFileChange: true 43 | }); 44 | }; 45 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "add-to-cart", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "watch": "ng build --watch --configuration development", 9 | "test": "ng test" 10 | }, 11 | "private": true, 12 | "dependencies": { 13 | "@angular/animations": "~12.0.4", 14 | "@angular/common": "~12.0.4", 15 | "@angular/compiler": "~12.0.4", 16 | "@angular/core": "~12.0.4", 17 | "@angular/forms": "~12.0.4", 18 | "@angular/platform-browser": "~12.0.4", 19 | "@angular/platform-browser-dynamic": "~12.0.4", 20 | "@angular/router": "~12.0.4", 21 | "rxjs": "~6.6.0", 22 | "tslib": "^2.1.0", 23 | "zone.js": "~0.11.4" 24 | }, 25 | "devDependencies": { 26 | "@angular-devkit/build-angular": "~12.0.4", 27 | "@angular/cli": "~12.0.4", 28 | "@angular/compiler-cli": "~12.0.4", 29 | "@types/jasmine": "~3.6.0", 30 | "@types/node": "^12.11.1", 31 | "jasmine-core": "~3.7.0", 32 | "karma": "~6.3.0", 33 | "karma-chrome-launcher": "~3.1.0", 34 | "karma-coverage": "~2.0.3", 35 | "karma-jasmine": "~4.0.0", 36 | "karma-jasmine-html-reporter": "^1.5.0", 37 | "typescript": "~4.2.3" 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { RouterModule, Routes } from '@angular/router'; 3 | import { CartComponent } from './component/cart/cart.component'; 4 | import { ProductsComponent } from './component/products/products.component'; 5 | 6 | const routes: Routes = [ 7 | {path:'', redirectTo:'products',pathMatch:'full'}, 8 | {path:'products', component: ProductsComponent}, 9 | {path:'cart', component: CartComponent} 10 | ]; 11 | 12 | @NgModule({ 13 | imports: [RouterModule.forRoot(routes)], 14 | exports: [RouterModule] 15 | }) 16 | export class AppRoutingModule { } 17 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yshashi/add-to-cart/df01349f84d4ebe73f6266bd10fb7bf7cdab0b66/src/app/app.component.scss -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(async () => { 7 | await TestBed.configureTestingModule({ 8 | imports: [ 9 | RouterTestingModule 10 | ], 11 | declarations: [ 12 | AppComponent 13 | ], 14 | }).compileComponents(); 15 | }); 16 | 17 | it('should create the app', () => { 18 | const fixture = TestBed.createComponent(AppComponent); 19 | const app = fixture.componentInstance; 20 | expect(app).toBeTruthy(); 21 | }); 22 | 23 | it(`should have as title 'add-to-cart'`, () => { 24 | const fixture = TestBed.createComponent(AppComponent); 25 | const app = fixture.componentInstance; 26 | expect(app.title).toEqual('add-to-cart'); 27 | }); 28 | 29 | it('should render title', () => { 30 | const fixture = TestBed.createComponent(AppComponent); 31 | fixture.detectChanges(); 32 | const compiled = fixture.nativeElement; 33 | expect(compiled.querySelector('.content span').textContent).toContain('add-to-cart app is running!'); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'app-root', 5 | templateUrl: './app.component.html', 6 | styleUrls: ['./app.component.scss'] 7 | }) 8 | export class AppComponent { 9 | title = 'add-to-cart'; 10 | } 11 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { BrowserModule } from '@angular/platform-browser'; 3 | 4 | import { AppRoutingModule } from './app-routing.module'; 5 | import { AppComponent } from './app.component'; 6 | import { HeaderComponent } from './component/header/header.component'; 7 | import { CartComponent } from './component/cart/cart.component'; 8 | import { ProductsComponent } from './component/products/products.component'; 9 | import { HttpClientModule } from '@angular/common/http'; 10 | import { FilterPipe } from './shared/filter.pipe'; 11 | import { FormsModule, ReactiveFormsModule } from '@angular/forms'; 12 | 13 | @NgModule({ 14 | declarations: [ 15 | AppComponent, 16 | HeaderComponent, 17 | CartComponent, 18 | ProductsComponent, 19 | FilterPipe 20 | ], 21 | imports: [ 22 | BrowserModule, 23 | AppRoutingModule, 24 | HttpClientModule, 25 | FormsModule, 26 | ReactiveFormsModule 27 | ], 28 | providers: [], 29 | bootstrap: [AppComponent] 30 | }) 31 | export class AppModule { } 32 | -------------------------------------------------------------------------------- /src/app/component/cart/cart.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 |
5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 |
Sr.NoProduct NameProduct ImageDescriptionPriceQuantityTotalAction
{{i+1}}{{item.title}}{{item.description}}{{item.price}}{{item.quantity}}{{item.total}} 28 | 29 |
Grand Total : ${{grandTotal}}
40 |
41 |
42 |
43 |
44 | 45 | 46 | 47 |
48 |
49 |
My Cart
50 |
51 |
52 | 53 |

Your cart is empty!

54 |
Add item to it now
55 | 56 |
57 |
58 |
-------------------------------------------------------------------------------- /src/app/component/cart/cart.component.scss: -------------------------------------------------------------------------------- 1 | .card { 2 | height: 60vh; 3 | margin: 25px; 4 | padding: 25px; 5 | } 6 | 7 | .center img { 8 | text-decoration: none; 9 | color: inherit; 10 | border: none; 11 | outline: none; 12 | height: 162px; 13 | width: 250px; 14 | margin: 20px 0px; 15 | } 16 | 17 | h4, 18 | h6 { 19 | font-weight: 400; 20 | } 21 | 22 | .center { 23 | position: absolute; 24 | top: 50%; 25 | left: 50%; 26 | transform: translate(-50%, -50%); 27 | text-align: center; 28 | } 29 | 30 | .card-table { 31 | position: relative; 32 | display: flex; 33 | flex-direction: column; 34 | min-width: none; 35 | word-wrap: break-word; 36 | background-color: #fff; 37 | background-clip: border-box; 38 | border: 1px solid rgba(0, 0, 0, 0.2); 39 | border-radius: .25rem; 40 | } 41 | 42 | .center .btn { 43 | font-size: 14px !important; 44 | margin-top: 20px !important; 45 | font-weight: 400; 46 | padding: 12px 72px; 47 | border-radius: 3px !important; 48 | } -------------------------------------------------------------------------------- /src/app/component/cart/cart.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { CartComponent } from './cart.component'; 4 | 5 | describe('CartComponent', () => { 6 | let component: CartComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ CartComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(CartComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/component/cart/cart.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { CartService } from 'src/app/service/cart.service'; 3 | 4 | @Component({ 5 | selector: 'app-cart', 6 | templateUrl: './cart.component.html', 7 | styleUrls: ['./cart.component.scss'] 8 | }) 9 | export class CartComponent implements OnInit { 10 | 11 | public products : any = []; 12 | public grandTotal !: number; 13 | constructor(private cartService : CartService) { } 14 | 15 | ngOnInit(): void { 16 | this.cartService.getProducts() 17 | .subscribe(res=>{ 18 | this.products = res; 19 | this.grandTotal = this.cartService.getTotalPrice(); 20 | }) 21 | } 22 | removeItem(item: any){ 23 | this.cartService.removeCartItem(item); 24 | } 25 | emptycart(){ 26 | this.cartService.removeAllCart(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/app/component/header/header.component.html: -------------------------------------------------------------------------------- 1 |
2 | 20 |
-------------------------------------------------------------------------------- /src/app/component/header/header.component.scss: -------------------------------------------------------------------------------- 1 | .form-control { 2 | border-radius: 3px; 3 | width: 600px; 4 | margin-left: 20px; 5 | } 6 | 7 | .search-icon { 8 | position: absolute; 9 | z-index: 10; 10 | right: 59%; 11 | top: 20px; 12 | color: blue; 13 | } -------------------------------------------------------------------------------- /src/app/component/header/header.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HeaderComponent } from './header.component'; 4 | 5 | describe('HeaderComponent', () => { 6 | let component: HeaderComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ HeaderComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HeaderComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/component/header/header.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { CartService } from 'src/app/service/cart.service'; 3 | 4 | @Component({ 5 | selector: 'app-header', 6 | templateUrl: './header.component.html', 7 | styleUrls: ['./header.component.scss'] 8 | }) 9 | export class HeaderComponent implements OnInit { 10 | 11 | public totalItem : number = 0; 12 | public searchTerm !: string; 13 | constructor(private cartService : CartService) { } 14 | 15 | ngOnInit(): void { 16 | this.cartService.getProducts() 17 | .subscribe(res=>{ 18 | this.totalItem = res.length; 19 | }) 20 | } 21 | search(event:any){ 22 | this.searchTerm = (event.target as HTMLInputElement).value; 23 | console.log(this.searchTerm); 24 | this.cartService.search.next(this.searchTerm); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/app/component/products/products.component.html: -------------------------------------------------------------------------------- 1 | 29 | 30 |
31 |
32 | 33 |
34 | 35 |
{{item.title}}
36 |

{{item.description}}

37 |

Price: ${{item.price}}

38 | 39 |
40 |
41 | 42 |
43 |
-------------------------------------------------------------------------------- /src/app/component/products/products.component.scss: -------------------------------------------------------------------------------- 1 | .card-top { 2 | position: relative; 3 | display: flex; 4 | flex-direction: column; 5 | min-width: 0; 6 | word-wrap: break-word; 7 | background: #fff; 8 | background-clip: border-box; 9 | border: 1px solid rgba(0, 0, 0, 0.2); 10 | border-radius: .25rem; 11 | } 12 | 13 | .item img { 14 | width: 64px; 15 | } 16 | 17 | .item { 18 | margin: 0px 15px; 19 | text-align: center; 20 | } 21 | 22 | .item a:hover { 23 | color: blue; 24 | } 25 | 26 | .card { 27 | padding: 20px; 28 | margin: 50px; 29 | } 30 | 31 | .card img { 32 | width: 200px; 33 | height: 200px; 34 | margin-bottom: 15px; 35 | transition: 0.3s ease-in-out; 36 | } 37 | 38 | .card img:hover { 39 | transition: 0.3s ease-in-out; 40 | transform: scale(1.1); 41 | } -------------------------------------------------------------------------------- /src/app/component/products/products.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ProductsComponent } from './products.component'; 4 | 5 | describe('ProductsComponent', () => { 6 | let component: ProductsComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async () => { 10 | await TestBed.configureTestingModule({ 11 | declarations: [ ProductsComponent ] 12 | }) 13 | .compileComponents(); 14 | }); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ProductsComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/component/products/products.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { ApiService } from 'src/app/service/api.service'; 3 | import { CartService } from 'src/app/service/cart.service'; 4 | 5 | @Component({ 6 | selector: 'app-products', 7 | templateUrl: './products.component.html', 8 | styleUrls: ['./products.component.scss'] 9 | }) 10 | export class ProductsComponent implements OnInit { 11 | 12 | public productList : any ; 13 | public filterCategory : any 14 | searchKey:string =""; 15 | constructor(private api : ApiService, private cartService : CartService) { } 16 | 17 | ngOnInit(): void { 18 | this.api.getProduct() 19 | .subscribe(res=>{ 20 | this.productList = res; 21 | this.filterCategory = res; 22 | this.productList.forEach((a:any) => { 23 | if(a.category ==="women's clothing" || a.category ==="men's clothing"){ 24 | a.category ="fashion" 25 | } 26 | Object.assign(a,{quantity:1,total:a.price}); 27 | }); 28 | console.log(this.productList) 29 | }); 30 | 31 | this.cartService.search.subscribe((val:any)=>{ 32 | this.searchKey = val; 33 | }) 34 | } 35 | addtocart(item: any){ 36 | this.cartService.addtoCart(item); 37 | } 38 | filter(category:string){ 39 | this.filterCategory = this.productList 40 | .filter((a:any)=>{ 41 | if(a.category == category || category==''){ 42 | return a; 43 | } 44 | }) 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/app/service/api.service.ts: -------------------------------------------------------------------------------- 1 | import { HttpClient } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import {map} from 'rxjs/operators'; 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class ApiService { 8 | 9 | constructor(private http : HttpClient) { } 10 | 11 | getProduct(){ 12 | return this.http.get("https://fakestoreapi.com/products") 13 | .pipe(map((res:any)=>{ 14 | return res; 15 | })) 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/app/service/cart.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { BehaviorSubject } from 'rxjs'; 3 | 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class CartService { 8 | 9 | public cartItemList : any =[] 10 | public productList = new BehaviorSubject([]); 11 | public search = new BehaviorSubject(""); 12 | 13 | constructor() { } 14 | getProducts(){ 15 | return this.productList.asObservable(); 16 | } 17 | 18 | setProduct(product : any){ 19 | this.cartItemList.push(...product); 20 | this.productList.next(product); 21 | } 22 | addtoCart(product : any){ 23 | this.cartItemList.push(product); 24 | this.productList.next(this.cartItemList); 25 | this.getTotalPrice(); 26 | console.log(this.cartItemList) 27 | } 28 | getTotalPrice() : number{ 29 | let grandTotal = 0; 30 | this.cartItemList.map((a:any)=>{ 31 | grandTotal += a.total; 32 | }) 33 | return grandTotal; 34 | } 35 | removeCartItem(product: any){ 36 | this.cartItemList.map((a:any, index:any)=>{ 37 | if(product.id=== a.id){ 38 | this.cartItemList.splice(index,1); 39 | } 40 | }) 41 | this.productList.next(this.cartItemList); 42 | } 43 | removeAllCart(){ 44 | this.cartItemList = [] 45 | this.productList.next(this.cartItemList); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/app/shared/filter.pipe.spec.ts: -------------------------------------------------------------------------------- 1 | import { FilterPipe } from './filter.pipe'; 2 | 3 | describe('FilterPipe', () => { 4 | it('create an instance', () => { 5 | const pipe = new FilterPipe(); 6 | expect(pipe).toBeTruthy(); 7 | }); 8 | }); 9 | -------------------------------------------------------------------------------- /src/app/shared/filter.pipe.ts: -------------------------------------------------------------------------------- 1 | import { Pipe, PipeTransform } from '@angular/core'; 2 | 3 | @Pipe({ 4 | name: 'filter' 5 | }) 6 | export class FilterPipe implements PipeTransform { 7 | 8 | transform(value : any[], filterString: string, propName:string): any[] { 9 | const result:any =[]; 10 | if(!value || filterString==='' || propName ===''){ 11 | return value; 12 | } 13 | value.forEach((a:any)=>{ 14 | if(a[propName].trim().toLowerCase().includes(filterString.toLowerCase())){ 15 | result.push(a); 16 | } 17 | }); 18 | return result; 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yshashi/add-to-cart/df01349f84d4ebe73f6266bd10fb7bf7cdab0b66/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/plugins/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yshashi/add-to-cart/df01349f84d4ebe73f6266bd10fb7bf7cdab0b66/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | AddToCart 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** 22 | * IE11 requires the following for NgClass support on SVG elements 23 | */ 24 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 25 | 26 | /** 27 | * Web Animations `@angular/platform-browser/animations` 28 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 29 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 30 | */ 31 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 32 | 33 | /** 34 | * By default, zone.js will patch all possible macroTask and DomEvents 35 | * user can disable parts of macroTask/DomEvents patch by setting following flags 36 | * because those flags need to be set before `zone.js` being loaded, and webpack 37 | * will put import in the top of bundle, so user need to create a separate file 38 | * in this directory (for example: zone-flags.ts), and put the following flags 39 | * into that file, and then add the following code before importing zone.js. 40 | * import './zone-flags'; 41 | * 42 | * The flags allowed in zone-flags.ts are listed here. 43 | * 44 | * The following flags will work for all browsers. 45 | * 46 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 47 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 48 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 49 | * 50 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 51 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 52 | * 53 | * (window as any).__Zone_enable_cross_context_check = true; 54 | * 55 | */ 56 | 57 | /*************************************************************************************************** 58 | * Zone JS is required by default for Angular itself. 59 | */ 60 | import 'zone.js'; // Included with Angular CLI. 61 | 62 | 63 | /*************************************************************************************************** 64 | * APPLICATION IMPORTS 65 | */ 66 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: { 11 | context(path: string, deep?: boolean, filter?: RegExp): { 12 | keys(): string[]; 13 | (id: string): T; 14 | }; 15 | }; 16 | 17 | // First, initialize the Angular testing environment. 18 | getTestBed().initTestEnvironment( 19 | BrowserDynamicTestingModule, 20 | platformBrowserDynamicTesting() 21 | ); 22 | // Then we find all the tests. 23 | const context = require.context('./', true, /\.spec\.ts$/); 24 | // And load the modules. 25 | context.keys().map(context); 26 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/app", 6 | "types": [] 7 | }, 8 | "files": [ 9 | "src/main.ts", 10 | "src/polyfills.ts" 11 | ], 12 | "include": [ 13 | "src/**/*.d.ts" 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "compileOnSave": false, 4 | "compilerOptions": { 5 | "baseUrl": "./", 6 | "outDir": "./dist/out-tsc", 7 | "forceConsistentCasingInFileNames": true, 8 | "strict": true, 9 | "noImplicitReturns": true, 10 | "noFallthroughCasesInSwitch": true, 11 | "sourceMap": true, 12 | "declaration": false, 13 | "downlevelIteration": true, 14 | "experimentalDecorators": true, 15 | "moduleResolution": "node", 16 | "importHelpers": true, 17 | "target": "es2017", 18 | "module": "es2020", 19 | "lib": [ 20 | "es2018", 21 | "dom" 22 | ] 23 | }, 24 | "angularCompilerOptions": { 25 | "enableI18nLegacyMessageIdFormat": false, 26 | "strictInjectionParameters": true, 27 | "strictInputAccessModifiers": true, 28 | "strictTemplates": true 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | /* To learn more about this file see: https://angular.io/config/tsconfig. */ 2 | { 3 | "extends": "./tsconfig.json", 4 | "compilerOptions": { 5 | "outDir": "./out-tsc/spec", 6 | "types": [ 7 | "jasmine" 8 | ] 9 | }, 10 | "files": [ 11 | "src/test.ts", 12 | "src/polyfills.ts" 13 | ], 14 | "include": [ 15 | "src/**/*.spec.ts", 16 | "src/**/*.d.ts" 17 | ] 18 | } 19 | --------------------------------------------------------------------------------