├── src ├── assets │ ├── .gitkeep │ └── images │ │ └── flame.png ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── app │ ├── auth │ │ ├── login │ │ │ ├── login.component.scss │ │ │ ├── login.component.html │ │ │ └── login.component.ts │ │ └── signup │ │ │ ├── signup.component.scss │ │ │ ├── signup.component.html │ │ │ └── signup.component.ts │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.ts │ ├── models │ │ └── Sauce.model.ts │ ├── sauce-form │ │ ├── sauce-form.component.scss │ │ ├── sauce-form.component.html │ │ └── sauce-form.component.ts │ ├── interceptors │ │ └── auth-interceptor.ts │ ├── sauce-list │ │ ├── sauce-list.component.scss │ │ ├── sauce-list.component.html │ │ └── sauce-list.component.ts │ ├── header │ │ ├── header.component.ts │ │ ├── header.component.html │ │ └── header.component.scss │ ├── services │ │ ├── auth-guard.service.ts │ │ ├── auth.service.ts │ │ └── sauces.service.ts │ ├── single-sauce │ │ ├── single-sauce.component.scss │ │ ├── single-sauce.component.html │ │ └── single-sauce.component.ts │ ├── app-routing.module.ts │ └── app.module.ts ├── favicon.ico ├── colors.scss ├── styles.scss ├── main.ts ├── test.ts ├── index.html └── polyfills.ts ├── .vscode ├── extensions.json ├── launch.json └── tasks.json ├── .editorconfig ├── tsconfig.app.json ├── tsconfig.spec.json ├── .browserslistrc ├── .gitignore ├── tsconfig.json ├── README.md ├── package.json ├── karma.conf.js └── angular.json /src/assets/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/app/auth/login/login.component.scss: -------------------------------------------------------------------------------- 1 | form { 2 | margin: 2em auto; 3 | max-width: 750px; 4 | } 5 | -------------------------------------------------------------------------------- /src/app/auth/signup/signup.component.scss: -------------------------------------------------------------------------------- 1 | form { 2 | margin: 2em auto; 3 | max-width: 750px; 4 | } 5 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenClassrooms-Student-Center/Web-Developer-P6/HEAD/src/favicon.ico -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 | -------------------------------------------------------------------------------- /src/assets/images/flame.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/OpenClassrooms-Student-Center/Web-Developer-P6/HEAD/src/assets/images/flame.png -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- 1 | .container { 2 | position: relative; 3 | width: 100%; 4 | max-width: 1000px; 5 | margin: auto; 6 | } 7 | -------------------------------------------------------------------------------- /src/colors.scss: -------------------------------------------------------------------------------- 1 | $colors: ( 2 | light-bg: #E9E3B4, 3 | medium-bg: #AABD8C, 4 | dark-type: #393D3F, 5 | accent: #FF8442, 6 | highlight: #E84E4E 7 | ); 8 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=827846 3 | "recommendations": ["angular.ng-template"] 4 | } 5 | -------------------------------------------------------------------------------- /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 | } 10 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | 3 | html, body { height: 100%; } 4 | body { 5 | font-family: "Source Sans Pro", sans-serif ; 6 | letter-spacing: 0.1em; 7 | margin: 0; 8 | white-space: pre-line; 9 | } 10 | 11 | mat-spinner { 12 | margin: auto; 13 | } 14 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/app/models/Sauce.model.ts: -------------------------------------------------------------------------------- 1 | export class Sauce { 2 | _id!: string; 3 | name!: string; 4 | manufacturer!: string; 5 | description!: string; 6 | heat!: number; 7 | likes!: number; 8 | dislikes!: number; 9 | imageUrl!: string; 10 | mainPepper!: string; 11 | usersLiked!: string[]; 12 | usersDisliked!: string[]; 13 | userId!: string; 14 | } 15 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /src/app/sauce-form/sauce-form.component.scss: -------------------------------------------------------------------------------- 1 | form { 2 | margin: 2em auto; 3 | max-width: 750px; 4 | } 5 | 6 | .heat-range { 7 | width: 60%; 8 | display: block; 9 | } 10 | 11 | .heat-container { 12 | display: flex; 13 | } 14 | 15 | .heat-reading { 16 | width: 5em; 17 | margin-left: 1.5em; 18 | background-color: white; 19 | } 20 | 21 | input[type="file"] { 22 | display: none; 23 | } 24 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 3 | "version": "0.2.0", 4 | "configurations": [ 5 | { 6 | "name": "ng serve", 7 | "type": "pwa-chrome", 8 | "request": "launch", 9 | "preLaunchTask": "npm: start", 10 | "url": "http://localhost:4200/" 11 | }, 12 | { 13 | "name": "ng test", 14 | "type": "chrome", 15 | "request": "launch", 16 | "preLaunchTask": "npm: test", 17 | "url": "http://localhost:9876/debug.html" 18 | } 19 | ] 20 | } 21 | -------------------------------------------------------------------------------- /src/app/interceptors/auth-interceptor.ts: -------------------------------------------------------------------------------- 1 | import { HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; 2 | import { Injectable } from '@angular/core'; 3 | import { AuthService } from '../services/auth.service'; 4 | 5 | @Injectable() 6 | export class AuthInterceptor implements HttpInterceptor { 7 | 8 | constructor(private auth: AuthService) {} 9 | 10 | intercept(req: HttpRequest, next: HttpHandler) { 11 | const authToken = this.auth.getToken(); 12 | const newRequest = req.clone({ 13 | headers: req.headers.set('Authorization', 'Bearer ' + authToken) 14 | }); 15 | return next.handle(newRequest); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/app/sauce-list/sauce-list.component.scss: -------------------------------------------------------------------------------- 1 | .sauce-list { 2 | display: flex; 3 | justify-content: space-around; 4 | flex-wrap: wrap; 5 | } 6 | 7 | .sauce-list-item { 8 | text-align: center; 9 | width: 220px; 10 | img { 11 | max-width: 200px; 12 | max-height: 250px; 13 | } 14 | h4 { 15 | margin: 0; 16 | font-weight: 500; 17 | } 18 | p { 19 | margin: 0; 20 | } 21 | transition: all 0.1s ease-in-out; 22 | cursor: pointer; 23 | &:hover { 24 | transform: scale(1.05); 25 | box-shadow: 1px 1px 20px rgba(120, 120, 120, 0.3); 26 | } 27 | } 28 | 29 | .list-title { 30 | text-align: center; 31 | margin: 2em auto; 32 | } 33 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /src/app/auth/login/login.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 | 5 | 6 |
7 |
8 | 9 | 10 |
11 | 12 |

{{ errorMsg }}

13 |
14 | -------------------------------------------------------------------------------- /src/app/sauce-list/sauce-list.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | No sauces to display! 5 |

6 |

THE SAUCES

7 |
8 |
9 | Hot sauce bottle 10 |

{{ sauce.name | uppercase }}

11 |

Heat: {{ sauce.heat }}/10

12 |
13 |
14 |
15 | -------------------------------------------------------------------------------- /src/app/auth/signup/signup.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |
4 | 5 | 6 |
7 |
8 | 9 | 10 |
11 | 12 |

{{ errorMsg }}

13 |
14 | -------------------------------------------------------------------------------- /src/app/header/header.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { AuthService } from '../services/auth.service'; 3 | import { Observable, shareReplay } from 'rxjs'; 4 | 5 | @Component({ 6 | selector: 'app-header', 7 | templateUrl: './header.component.html', 8 | styleUrls: ['./header.component.scss'] 9 | }) 10 | export class HeaderComponent implements OnInit { 11 | 12 | isAuth$!: Observable; 13 | 14 | constructor(private auth: AuthService) { } 15 | 16 | ngOnInit() { 17 | this.isAuth$ = this.auth.isAuth$.pipe( 18 | shareReplay(1) 19 | ); 20 | } 21 | 22 | onLogout() { 23 | this.auth.logout(); 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /.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 | /bazel-out 8 | 9 | # Node 10 | /node_modules 11 | npm-debug.log 12 | yarn-error.log 13 | 14 | # IDEs and editors 15 | .idea/ 16 | .project 17 | .classpath 18 | .c9/ 19 | *.launch 20 | .settings/ 21 | *.sublime-workspace 22 | 23 | # Visual Studio Code 24 | .vscode/* 25 | !.vscode/settings.json 26 | !.vscode/tasks.json 27 | !.vscode/launch.json 28 | !.vscode/extensions.json 29 | .history/* 30 | 31 | # Miscellaneous 32 | /.angular/cache 33 | .sass-cache/ 34 | /connect.lock 35 | /coverage 36 | /libpeerconnection.log 37 | testem.log 38 | /typings 39 | 40 | # System files 41 | .DS_Store 42 | Thumbs.db 43 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` 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/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/app/services/auth-guard.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; 3 | import { Observable, take, tap } from 'rxjs'; 4 | import { AuthService } from './auth.service'; 5 | 6 | @Injectable() 7 | export class AuthGuard implements CanActivate { 8 | 9 | constructor(private auth: AuthService, 10 | private router: Router) {} 11 | 12 | canActivate(route: ActivatedRouteSnapshot, 13 | state: RouterStateSnapshot): Observable { 14 | return this.auth.isAuth$.pipe( 15 | take(1), 16 | tap(auth => { 17 | if (!auth) { 18 | this.router.navigate(['/login']); 19 | } 20 | }) 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /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 | (id: string): T; 13 | keys(): string[]; 14 | }; 15 | }; 16 | 17 | // First, initialize the Angular testing environment. 18 | getTestBed().initTestEnvironment( 19 | BrowserDynamicTestingModule, 20 | platformBrowserDynamicTesting(), 21 | ); 22 | 23 | // Then we find all the tests. 24 | const context = require.context('./', true, /\.spec\.ts$/); 25 | // And load the modules. 26 | context.keys().map(context); 27 | -------------------------------------------------------------------------------- /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 | "noImplicitOverride": true, 10 | "noPropertyAccessFromIndexSignature": true, 11 | "noImplicitReturns": true, 12 | "noFallthroughCasesInSwitch": true, 13 | "sourceMap": true, 14 | "declaration": false, 15 | "downlevelIteration": true, 16 | "experimentalDecorators": true, 17 | "moduleResolution": "node", 18 | "importHelpers": true, 19 | "target": "es2017", 20 | "module": "es2020", 21 | "lib": [ 22 | "es2020", 23 | "dom" 24 | ] 25 | }, 26 | "angularCompilerOptions": { 27 | "enableI18nLegacyMessageIdFormat": false, 28 | "strictInjectionParameters": true, 29 | "strictInputAccessModifiers": true, 30 | "strictTemplates": true 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | HotTakes 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/app/single-sauce/single-sauce.component.scss: -------------------------------------------------------------------------------- 1 | .sauce-container { 2 | display: flex; 3 | img { 4 | max-height: 70vh; 5 | flex: 1; 6 | } 7 | } 8 | 9 | .sauce-info { 10 | padding: 40px 20px; 11 | flex: 1; 12 | } 13 | 14 | .manufacturer { 15 | margin: 0; 16 | } 17 | 18 | .sauce-name { 19 | margin: 0; 20 | } 21 | 22 | .like-buttons { 23 | display: flex; 24 | i { 25 | margin-right: 0.5em; 26 | cursor: pointer; 27 | } 28 | } 29 | 30 | .likes, .dislikes { 31 | margin: 0 0.4em; 32 | } 33 | 34 | .liked { 35 | color: rgba(51, 219, 0, 1); 36 | } 37 | 38 | .disliked { 39 | color: rgba(219, 51, 0, 1); 40 | } 41 | 42 | .like { 43 | &:hover { 44 | color: rgba(51, 219, 0, 1); 45 | } 46 | } 47 | 48 | .dislike { 49 | transform: scaleX(-1) translateY(5px); 50 | &:hover { 51 | color: rgba(219, 51, 0, 1); 52 | } 53 | } 54 | 55 | .disabled { 56 | cursor: none; 57 | &:hover { 58 | color: rgba(190, 190, 190, 1); 59 | } 60 | color: rgba(190, 190, 190, 1); 61 | } 62 | 63 | .control-buttons { 64 | button { 65 | margin: 1em 1em 0 0; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | // For more information, visit: https://go.microsoft.com/fwlink/?LinkId=733558 3 | "version": "2.0.0", 4 | "tasks": [ 5 | { 6 | "type": "npm", 7 | "script": "start", 8 | "isBackground": true, 9 | "problemMatcher": { 10 | "owner": "typescript", 11 | "pattern": "$tsc", 12 | "background": { 13 | "activeOnStart": true, 14 | "beginsPattern": { 15 | "regexp": "(.*?)" 16 | }, 17 | "endsPattern": { 18 | "regexp": "bundle generation complete" 19 | } 20 | } 21 | } 22 | }, 23 | { 24 | "type": "npm", 25 | "script": "test", 26 | "isBackground": true, 27 | "problemMatcher": { 28 | "owner": "typescript", 29 | "pattern": "$tsc", 30 | "background": { 31 | "activeOnStart": true, 32 | "beginsPattern": { 33 | "regexp": "(.*?)" 34 | }, 35 | "endsPattern": { 36 | "regexp": "bundle generation complete" 37 | } 38 | } 39 | } 40 | } 41 | ] 42 | } 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # HotTakes 2 | 3 | This project was generated with [Angular CLI](https://github.com/angular/angular-cli) version 13.2.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 | -------------------------------------------------------------------------------- /src/app/sauce-list/sauce-list.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { SaucesService } from '../services/sauces.service'; 3 | import { catchError, Observable, of, tap } from 'rxjs'; 4 | import { Sauce } from '../models/Sauce.model'; 5 | import { Router } from '@angular/router'; 6 | 7 | @Component({ 8 | selector: 'app-sauce-list', 9 | templateUrl: './sauce-list.component.html', 10 | styleUrls: ['./sauce-list.component.scss'] 11 | }) 12 | export class SauceListComponent implements OnInit { 13 | 14 | sauces$!: Observable; 15 | loading!: boolean; 16 | errorMsg!: string; 17 | 18 | constructor(private sauce: SaucesService, 19 | private router: Router) { } 20 | 21 | ngOnInit() { 22 | this.loading = true; 23 | this.sauces$ = this.sauce.sauces$.pipe( 24 | tap(() => { 25 | this.loading = false; 26 | this.errorMsg = ''; 27 | }), 28 | catchError(error => { 29 | this.errorMsg = JSON.stringify(error); 30 | this.loading = false; 31 | return of([]); 32 | }) 33 | ); 34 | this.sauce.getSauces(); 35 | } 36 | 37 | onClickSauce(id: string) { 38 | this.router.navigate(['sauce', id]); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /src/app/header/header.component.html: -------------------------------------------------------------------------------- 1 | 49 | -------------------------------------------------------------------------------- /src/app/header/header.component.scss: -------------------------------------------------------------------------------- 1 | nav { 2 | box-sizing: border-box; 3 | width: 100%; 4 | max-width: 1400px; 5 | margin: auto; 6 | position: relative; 7 | top: 0; 8 | left: 0; 9 | display: flex; 10 | padding: 20px; 11 | justify-content: space-between; 12 | border-bottom: thin solid black; 13 | } 14 | 15 | h1 { 16 | font-weight: 700; 17 | font-size: 2.4em; 18 | margin: 0; 19 | } 20 | 21 | h5 { 22 | font-size: 1.2em; 23 | font-weight: 400; 24 | margin: 0; 25 | } 26 | 27 | .logo { 28 | display: flex; 29 | align-items: center; 30 | } 31 | 32 | .logo-text { 33 | text-align: center; 34 | } 35 | 36 | .logo-image { 37 | img { 38 | height: 4.5em; 39 | } 40 | margin-right: 1em; 41 | } 42 | 43 | .left-nav, .right-nav { 44 | align-self: center; 45 | width: 30%; 46 | } 47 | 48 | .right-nav { 49 | ul { 50 | justify-content: flex-end; 51 | } 52 | } 53 | 54 | ul { 55 | list-style: none; 56 | padding: 0; 57 | margin: 0; 58 | display: flex; 59 | } 60 | 61 | li { 62 | margin: 0 15px; 63 | } 64 | 65 | a { 66 | color: black; 67 | font-weight: 400; 68 | text-decoration: none; 69 | cursor: pointer; 70 | &:hover { 71 | text-decoration: underline; 72 | } 73 | } 74 | 75 | .active { 76 | font-weight: 700; 77 | text-decoration: underline; 78 | } 79 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule } from '@angular/core'; 2 | import { Routes, RouterModule } from '@angular/router'; 3 | import { SauceListComponent } from './sauce-list/sauce-list.component'; 4 | import { SauceFormComponent } from './sauce-form/sauce-form.component'; 5 | import { SingleSauceComponent } from './single-sauce/single-sauce.component'; 6 | import { SignupComponent } from './auth/signup/signup.component'; 7 | import { LoginComponent } from './auth/login/login.component'; 8 | import { AuthGuard } from './services/auth-guard.service'; 9 | 10 | const routes: Routes = [ 11 | { path: 'signup', component: SignupComponent }, 12 | { path: 'login', component: LoginComponent }, 13 | { path: 'sauces', component: SauceListComponent, canActivate: [AuthGuard] }, 14 | { path: 'sauce/:id', component: SingleSauceComponent, canActivate: [AuthGuard] }, 15 | { path: 'new-sauce', component: SauceFormComponent, canActivate: [AuthGuard] }, 16 | { path: 'modify-sauce/:id', component: SauceFormComponent, canActivate: [AuthGuard] }, 17 | { path: '', pathMatch: 'full', redirectTo: 'sauces'}, 18 | { path: '**', redirectTo: 'sauces' } 19 | ]; 20 | 21 | @NgModule({ 22 | imports: [RouterModule.forRoot(routes)], 23 | exports: [RouterModule], 24 | providers: [AuthGuard] 25 | }) 26 | export class AppRoutingModule { } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "hot-takes", 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": "~13.2.0", 14 | "@angular/cdk": "~13.2.3", 15 | "@angular/common": "~13.2.0", 16 | "@angular/compiler": "~13.2.0", 17 | "@angular/core": "~13.2.0", 18 | "@angular/forms": "~13.2.0", 19 | "@angular/material": "~13.2.3", 20 | "@angular/platform-browser": "~13.2.0", 21 | "@angular/platform-browser-dynamic": "~13.2.0", 22 | "@angular/router": "~13.2.0", 23 | "bootstrap": "^5.1.3", 24 | "rxjs": "~7.5.0", 25 | "tslib": "^2.3.0", 26 | "zone.js": "~0.11.4" 27 | }, 28 | "devDependencies": { 29 | "@angular-devkit/build-angular": "~13.2.4", 30 | "@angular/cli": "~13.2.4", 31 | "@angular/compiler-cli": "~13.2.0", 32 | "@types/jasmine": "~3.10.0", 33 | "@types/node": "^12.11.1", 34 | "jasmine-core": "~4.0.0", 35 | "karma": "~6.3.0", 36 | "karma-chrome-launcher": "~3.1.0", 37 | "karma-coverage": "~2.1.0", 38 | "karma-jasmine": "~4.0.0", 39 | "karma-jasmine-html-reporter": "~1.7.0", 40 | "typescript": "~4.5.2" 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/app/services/auth.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { HttpClient } from '@angular/common/http'; 3 | import { BehaviorSubject, tap } from 'rxjs'; 4 | import { Router } from '@angular/router'; 5 | 6 | @Injectable({ 7 | providedIn: 'root' 8 | }) 9 | export class AuthService { 10 | 11 | isAuth$ = new BehaviorSubject(false); 12 | private authToken = ''; 13 | private userId = ''; 14 | 15 | constructor(private http: HttpClient, 16 | private router: Router) {} 17 | 18 | createUser(email: string, password: string) { 19 | return this.http.post<{ message: string }>('http://localhost:3000/api/auth/signup', {email: email, password: password}); 20 | } 21 | 22 | getToken() { 23 | return this.authToken; 24 | } 25 | 26 | getUserId() { 27 | return this.userId; 28 | } 29 | 30 | loginUser(email: string, password: string) { 31 | return this.http.post<{ userId: string, token: string }>('http://localhost:3000/api/auth/login', {email: email, password: password}).pipe( 32 | tap(({ userId, token }) => { 33 | this.userId = userId; 34 | this.authToken = token; 35 | this.isAuth$.next(true); 36 | }) 37 | ); 38 | } 39 | 40 | logout() { 41 | this.authToken = ''; 42 | this.userId = ''; 43 | this.isAuth$.next(false); 44 | this.router.navigate(['login']); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/app/single-sauce/single-sauce.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 | 4 |
5 |

{{ sauce?.name }}

6 |

by {{ sauce?.manufacturer }}

7 |

Description

8 |

{{ sauce.description }}

9 | 19 | 22 |
23 | 24 | 25 | 26 |
27 |
28 |
29 | -------------------------------------------------------------------------------- /src/app/auth/login/login.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 3 | import { AuthService } from '../../services/auth.service'; 4 | import { Router } from '@angular/router'; 5 | import { catchError, EMPTY, tap } from 'rxjs'; 6 | 7 | @Component({ 8 | selector: 'app-login', 9 | templateUrl: './login.component.html', 10 | styleUrls: ['./login.component.scss'] 11 | }) 12 | export class LoginComponent implements OnInit { 13 | 14 | loginForm!: FormGroup; 15 | loading!: boolean; 16 | errorMsg!: string; 17 | 18 | constructor(private formBuilder: FormBuilder, 19 | private auth: AuthService, 20 | private router: Router) { } 21 | 22 | ngOnInit() { 23 | this.loginForm = this.formBuilder.group({ 24 | email: [null, [Validators.required, Validators.email]], 25 | password: [null, Validators.required] 26 | }); 27 | } 28 | 29 | onLogin() { 30 | this.loading = true; 31 | const email = this.loginForm.get('email')!.value; 32 | const password = this.loginForm.get('password')!.value; 33 | this.auth.loginUser(email, password).pipe( 34 | tap(() => { 35 | this.loading = false; 36 | this.router.navigate(['/sauces']); 37 | }), 38 | catchError(error => { 39 | this.loading = false; 40 | this.errorMsg = error.message; 41 | return EMPTY; 42 | }) 43 | ).subscribe(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/app/auth/signup/signup.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 3 | import { AuthService } from '../../services/auth.service'; 4 | import { Router } from '@angular/router'; 5 | import { catchError, EMPTY, switchMap, tap } from 'rxjs'; 6 | 7 | @Component({ 8 | selector: 'app-signup', 9 | templateUrl: './signup.component.html', 10 | styleUrls: ['./signup.component.scss'] 11 | }) 12 | export class SignupComponent implements OnInit { 13 | 14 | signupForm!: FormGroup; 15 | loading!: boolean; 16 | errorMsg!: string; 17 | 18 | constructor(private formBuilder: FormBuilder, 19 | private auth: AuthService, 20 | private router: Router) { } 21 | 22 | ngOnInit() { 23 | this.signupForm = this.formBuilder.group({ 24 | email: [null, [Validators.required, Validators.email]], 25 | password: [null, Validators.required] 26 | }); 27 | } 28 | 29 | onSignup() { 30 | this.loading = true; 31 | const email = this.signupForm.get('email')!.value; 32 | const password = this.signupForm.get('password')!.value; 33 | this.auth.createUser(email, password).pipe( 34 | switchMap(() => this.auth.loginUser(email, password)), 35 | tap(() => { 36 | this.loading = false; 37 | this.router.navigate(['/sauces']); 38 | }), 39 | catchError(error => { 40 | this.loading = false; 41 | this.errorMsg = error.message; 42 | return EMPTY; 43 | }) 44 | ).subscribe(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /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/hot-takes'), 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 | -------------------------------------------------------------------------------- /src/app/sauce-form/sauce-form.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 | 28 | 29 |
30 |
31 | 32 |
33 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule } from '@angular/core'; 3 | 4 | import { AppRoutingModule } from './app-routing.module'; 5 | import { AppComponent } from './app.component'; 6 | import { SignupComponent } from './auth/signup/signup.component'; 7 | import { LoginComponent } from './auth/login/login.component'; 8 | import { SauceListComponent } from './sauce-list/sauce-list.component'; 9 | import { SingleSauceComponent } from './single-sauce/single-sauce.component'; 10 | import { SauceFormComponent } from './sauce-form/sauce-form.component'; 11 | import { HeaderComponent } from './header/header.component'; 12 | import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; 13 | import { ReactiveFormsModule } from '@angular/forms'; 14 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; 15 | import { AuthInterceptor } from './interceptors/auth-interceptor'; 16 | import { MatButtonModule } from '@angular/material/button'; 17 | import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; 18 | 19 | @NgModule({ 20 | declarations: [ 21 | AppComponent, 22 | SignupComponent, 23 | LoginComponent, 24 | SauceListComponent, 25 | SingleSauceComponent, 26 | SauceFormComponent, 27 | HeaderComponent 28 | ], 29 | imports: [ 30 | BrowserModule, 31 | AppRoutingModule, 32 | HttpClientModule, 33 | ReactiveFormsModule, 34 | BrowserAnimationsModule, 35 | MatProgressSpinnerModule, 36 | MatButtonModule 37 | ], 38 | providers: [{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}], 39 | bootstrap: [AppComponent] 40 | }) 41 | export class AppModule { } 42 | -------------------------------------------------------------------------------- /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 recent versions of Safari, Chrome (including 12 | * Opera), Edge on the desktop, and iOS 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 | * By default, zone.js will patch all possible macroTask and DomEvents 23 | * user can disable parts of macroTask/DomEvents patch by setting following flags 24 | * because those flags need to be set before `zone.js` being loaded, and webpack 25 | * will put import in the top of bundle, so user need to create a separate file 26 | * in this directory (for example: zone-flags.ts), and put the following flags 27 | * into that file, and then add the following code before importing zone.js. 28 | * import './zone-flags'; 29 | * 30 | * The flags allowed in zone-flags.ts are listed here. 31 | * 32 | * The following flags will work for all browsers. 33 | * 34 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 35 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 36 | * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 37 | * 38 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 39 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 40 | * 41 | * (window as any).__Zone_enable_cross_context_check = true; 42 | * 43 | */ 44 | 45 | /*************************************************************************************************** 46 | * Zone JS is required by default for Angular itself. 47 | */ 48 | import 'zone.js'; // Included with Angular CLI. 49 | 50 | 51 | /*************************************************************************************************** 52 | * APPLICATION IMPORTS 53 | */ 54 | -------------------------------------------------------------------------------- /src/app/services/sauces.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { catchError, mapTo, of, Subject, tap, throwError } from 'rxjs'; 3 | import { Sauce } from '../models/Sauce.model'; 4 | import { HttpClient } from '@angular/common/http'; 5 | import { AuthService } from './auth.service'; 6 | 7 | @Injectable({ 8 | providedIn: 'root' 9 | }) 10 | export class SaucesService { 11 | 12 | sauces$ = new Subject(); 13 | 14 | constructor(private http: HttpClient, 15 | private auth: AuthService) {} 16 | 17 | getSauces() { 18 | this.http.get('http://localhost:3000/api/sauces').pipe( 19 | tap(sauces => this.sauces$.next(sauces)), 20 | catchError(error => { 21 | console.error(error.error.message); 22 | return of([]); 23 | }) 24 | ).subscribe(); 25 | } 26 | 27 | getSauceById(id: string) { 28 | return this.http.get('http://localhost:3000/api/sauces/' + id).pipe( 29 | catchError(error => throwError(error.error.message)) 30 | ); 31 | } 32 | 33 | likeSauce(id: string, like: boolean) { 34 | return this.http.post<{ message: string }>( 35 | 'http://localhost:3000/api/sauces/' + id + '/like', 36 | { userId: this.auth.getUserId(), like: like ? 1 : 0 } 37 | ).pipe( 38 | mapTo(like), 39 | catchError(error => throwError(error.error.message)) 40 | ); 41 | } 42 | 43 | dislikeSauce(id: string, dislike: boolean) { 44 | return this.http.post<{ message: string }>( 45 | 'http://localhost:3000/api/sauces/' + id + '/like', 46 | { userId: this.auth.getUserId(), like: dislike ? -1 : 0 } 47 | ).pipe( 48 | mapTo(dislike), 49 | catchError(error => throwError(error.error.message)) 50 | ); 51 | } 52 | 53 | createSauce(sauce: Sauce, image: File) { 54 | const formData = new FormData(); 55 | formData.append('sauce', JSON.stringify(sauce)); 56 | formData.append('image', image); 57 | return this.http.post<{ message: string }>('http://localhost:3000/api/sauces', formData).pipe( 58 | catchError(error => throwError(error.error.message)) 59 | ); 60 | } 61 | 62 | modifySauce(id: string, sauce: Sauce, image: string | File) { 63 | if (typeof image === 'string') { 64 | return this.http.put<{ message: string }>('http://localhost:3000/api/sauces/' + id, sauce).pipe( 65 | catchError(error => throwError(error.error.message)) 66 | ); 67 | } else { 68 | const formData = new FormData(); 69 | formData.append('sauce', JSON.stringify(sauce)); 70 | formData.append('image', image); 71 | return this.http.put<{ message: string }>('http://localhost:3000/api/sauces/' + id, formData).pipe( 72 | catchError(error => throwError(error.error.message)) 73 | ); 74 | } 75 | } 76 | 77 | deleteSauce(id: string) { 78 | return this.http.delete<{ message: string }>('http://localhost:3000/api/sauces/' + id).pipe( 79 | catchError(error => throwError(error.error.message)) 80 | ); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/app/single-sauce/single-sauce.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Sauce } from '../models/Sauce.model'; 3 | import { SaucesService } from '../services/sauces.service'; 4 | import { ActivatedRoute, Router } from '@angular/router'; 5 | import { AuthService } from '../services/auth.service'; 6 | import { catchError, EMPTY, map, Observable, of, switchMap, take, tap } from 'rxjs'; 7 | 8 | @Component({ 9 | selector: 'app-single-sauce', 10 | templateUrl: './single-sauce.component.html', 11 | styleUrls: ['./single-sauce.component.scss'] 12 | }) 13 | export class SingleSauceComponent implements OnInit { 14 | 15 | loading!: boolean; 16 | sauce$!: Observable; 17 | userId!: string; 18 | likePending!: boolean; 19 | liked!: boolean; 20 | disliked!: boolean; 21 | errorMessage!: string; 22 | 23 | constructor(private sauces: SaucesService, 24 | private route: ActivatedRoute, 25 | private auth: AuthService, 26 | private router: Router) { } 27 | 28 | ngOnInit() { 29 | this.userId = this.auth.getUserId(); 30 | this.loading = true; 31 | this.userId = this.auth.getUserId(); 32 | this.sauce$ = this.route.params.pipe( 33 | map(params => params['id']), 34 | switchMap(id => this.sauces.getSauceById(id)), 35 | tap(sauce => { 36 | this.loading = false; 37 | if (sauce.usersLiked.find(user => user === this.userId)) { 38 | this.liked = true; 39 | } else if (sauce.usersDisliked.find(user => user === this.userId)) { 40 | this.disliked = true; 41 | } 42 | }) 43 | ); 44 | } 45 | 46 | onLike() { 47 | if (this.disliked) { 48 | return; 49 | } 50 | this.likePending = true; 51 | this.sauce$.pipe( 52 | take(1), 53 | switchMap((sauce: Sauce) => this.sauces.likeSauce(sauce._id, !this.liked).pipe( 54 | tap(liked => { 55 | this.likePending = false; 56 | this.liked = liked; 57 | }), 58 | map(liked => ({ ...sauce, likes: liked ? sauce.likes + 1 : sauce.likes - 1 })), 59 | tap(sauce => this.sauce$ = of(sauce)) 60 | )), 61 | ).subscribe(); 62 | } 63 | 64 | onDislike() { 65 | if (this.liked) { 66 | return; 67 | } 68 | this.likePending = true; 69 | this.sauce$.pipe( 70 | take(1), 71 | switchMap((sauce: Sauce) => this.sauces.dislikeSauce(sauce._id, !this.disliked).pipe( 72 | tap(disliked => { 73 | this.likePending = false; 74 | this.disliked = disliked; 75 | }), 76 | map(disliked => ({ ...sauce, dislikes: disliked ? sauce.dislikes + 1 : sauce.dislikes - 1 })), 77 | tap(sauce => this.sauce$ = of(sauce)) 78 | )), 79 | ).subscribe(); 80 | } 81 | 82 | onBack() { 83 | this.router.navigate(['/sauces']); 84 | } 85 | 86 | onModify() { 87 | this.sauce$.pipe( 88 | take(1), 89 | tap(sauce => this.router.navigate(['/modify-sauce', sauce._id])) 90 | ).subscribe(); 91 | } 92 | 93 | onDelete() { 94 | this.loading = true; 95 | this.sauce$.pipe( 96 | take(1), 97 | switchMap(sauce => this.sauces.deleteSauce(sauce._id)), 98 | tap(message => { 99 | console.log(message); 100 | this.loading = false; 101 | this.router.navigate(['/sauces']); 102 | }), 103 | catchError(error => { 104 | this.loading = false; 105 | this.errorMessage = error.message; 106 | console.error(error); 107 | return EMPTY; 108 | }) 109 | ).subscribe(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "hot-takes": { 7 | "projectType": "application", 8 | "schematics": { 9 | "@schematics/angular:component": { 10 | "style": "scss", 11 | "skipTests": true 12 | }, 13 | "@schematics/angular:class": { 14 | "skipTests": true 15 | }, 16 | "@schematics/angular:directive": { 17 | "skipTests": true 18 | }, 19 | "@schematics/angular:guard": { 20 | "skipTests": true 21 | }, 22 | "@schematics/angular:interceptor": { 23 | "skipTests": true 24 | }, 25 | "@schematics/angular:pipe": { 26 | "skipTests": true 27 | }, 28 | "@schematics/angular:resolver": { 29 | "skipTests": true 30 | }, 31 | "@schematics/angular:service": { 32 | "skipTests": true 33 | }, 34 | "@schematics/angular:application": { 35 | "strict": true 36 | } 37 | }, 38 | "root": "", 39 | "sourceRoot": "src", 40 | "prefix": "app", 41 | "architect": { 42 | "build": { 43 | "builder": "@angular-devkit/build-angular:browser", 44 | "options": { 45 | "outputPath": "dist/hot-takes", 46 | "index": "src/index.html", 47 | "main": "src/main.ts", 48 | "polyfills": "src/polyfills.ts", 49 | "tsConfig": "tsconfig.app.json", 50 | "inlineStyleLanguage": "scss", 51 | "assets": [ 52 | "src/favicon.ico", 53 | "src/assets" 54 | ], 55 | "styles": [ 56 | "./node_modules/@angular/material/prebuilt-themes/deeppurple-amber.css", 57 | "./node_modules/bootstrap/dist/css/bootstrap.css" 58 | ], 59 | "scripts": [] 60 | }, 61 | "configurations": { 62 | "production": { 63 | "budgets": [ 64 | { 65 | "type": "initial", 66 | "maximumWarning": "500kb", 67 | "maximumError": "1mb" 68 | }, 69 | { 70 | "type": "anyComponentStyle", 71 | "maximumWarning": "2kb", 72 | "maximumError": "4kb" 73 | } 74 | ], 75 | "fileReplacements": [ 76 | { 77 | "replace": "src/environments/environment.ts", 78 | "with": "src/environments/environment.prod.ts" 79 | } 80 | ], 81 | "outputHashing": "all" 82 | }, 83 | "development": { 84 | "buildOptimizer": false, 85 | "optimization": false, 86 | "vendorChunk": true, 87 | "extractLicenses": false, 88 | "sourceMap": true, 89 | "namedChunks": true 90 | } 91 | }, 92 | "defaultConfiguration": "production" 93 | }, 94 | "serve": { 95 | "builder": "@angular-devkit/build-angular:dev-server", 96 | "configurations": { 97 | "production": { 98 | "browserTarget": "hot-takes:build:production" 99 | }, 100 | "development": { 101 | "browserTarget": "hot-takes:build:development" 102 | } 103 | }, 104 | "defaultConfiguration": "development" 105 | }, 106 | "extract-i18n": { 107 | "builder": "@angular-devkit/build-angular:extract-i18n", 108 | "options": { 109 | "browserTarget": "hot-takes:build" 110 | } 111 | }, 112 | "test": { 113 | "builder": "@angular-devkit/build-angular:karma", 114 | "options": { 115 | "main": "src/test.ts", 116 | "polyfills": "src/polyfills.ts", 117 | "tsConfig": "tsconfig.spec.json", 118 | "karmaConfig": "karma.conf.js", 119 | "inlineStyleLanguage": "scss", 120 | "assets": [ 121 | "src/favicon.ico", 122 | "src/assets" 123 | ], 124 | "styles": [ 125 | "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css", 126 | "src/styles.scss" 127 | ], 128 | "scripts": [] 129 | } 130 | } 131 | } 132 | } 133 | }, 134 | "defaultProject": "hot-takes" 135 | } 136 | -------------------------------------------------------------------------------- /src/app/sauce-form/sauce-form.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; 3 | import { ActivatedRoute, Router } from '@angular/router'; 4 | import { SaucesService } from '../services/sauces.service'; 5 | import { Sauce } from '../models/Sauce.model'; 6 | import { AuthService } from '../services/auth.service'; 7 | import { catchError, EMPTY, switchMap, tap } from 'rxjs'; 8 | 9 | @Component({ 10 | selector: 'app-sauce-form', 11 | templateUrl: './sauce-form.component.html', 12 | styleUrls: ['./sauce-form.component.scss'] 13 | }) 14 | export class SauceFormComponent implements OnInit { 15 | 16 | sauceForm!: FormGroup; 17 | mode!: string; 18 | loading!: boolean; 19 | sauce!: Sauce; 20 | errorMsg!: string; 21 | imagePreview!: string; 22 | 23 | constructor(private formBuilder: FormBuilder, 24 | private route: ActivatedRoute, 25 | private router: Router, 26 | private sauces: SaucesService, 27 | private auth: AuthService) { } 28 | 29 | ngOnInit() { 30 | this.loading = true; 31 | this.route.params.pipe( 32 | switchMap(params => { 33 | if (!params['id']) { 34 | this.mode = 'new'; 35 | this.initEmptyForm(); 36 | this.loading = false; 37 | return EMPTY; 38 | } else { 39 | this.mode = 'edit'; 40 | return this.sauces.getSauceById(params['id']) 41 | } 42 | }), 43 | tap(sauce => { 44 | if (sauce) { 45 | this.sauce = sauce; 46 | this.initModifyForm(sauce); 47 | this.loading = false; 48 | } 49 | }), 50 | catchError(error => this.errorMsg = JSON.stringify(error)) 51 | ).subscribe(); 52 | } 53 | 54 | initEmptyForm() { 55 | this.sauceForm = this.formBuilder.group({ 56 | name: [null, Validators.required], 57 | manufacturer: [null, Validators.required], 58 | description: [null, Validators.required], 59 | image: [null, Validators.required], 60 | mainPepper: [null, Validators.required], 61 | heat: [1, Validators.required], 62 | heatValue: [{value: 1, disabled: true}] 63 | }); 64 | this.sauceForm.get('heat')!.valueChanges.subscribe( 65 | (value) => { 66 | this.sauceForm.get('heatValue')!.setValue(value); 67 | } 68 | ); 69 | } 70 | 71 | initModifyForm(sauce: Sauce) { 72 | this.sauceForm = this.formBuilder.group({ 73 | name: [sauce.name, Validators.required], 74 | manufacturer: [sauce.manufacturer, Validators.required], 75 | description: [sauce.description, Validators.required], 76 | image: [sauce.imageUrl, Validators.required], 77 | mainPepper: [sauce.mainPepper, Validators.required], 78 | heat: [sauce.heat, Validators.required], 79 | heatValue: [{value: sauce.heat, disabled: true}] 80 | }); 81 | this.sauceForm.get('heat')!.valueChanges.subscribe( 82 | (value) => { 83 | this.sauceForm.get('heatValue')!.setValue(value); 84 | } 85 | ); 86 | this.imagePreview = this.sauce.imageUrl; 87 | } 88 | 89 | onSubmit() { 90 | this.loading = true; 91 | const newSauce = new Sauce(); 92 | newSauce.name = this.sauceForm.get('name')!.value; 93 | newSauce.manufacturer = this.sauceForm.get('manufacturer')!.value; 94 | newSauce.description = this.sauceForm.get('description')!.value; 95 | newSauce.mainPepper = this.sauceForm.get('mainPepper')!.value; 96 | newSauce.heat = this.sauceForm.get('heat')!.value; 97 | newSauce.userId = this.auth.getUserId(); 98 | if (this.mode === 'new') { 99 | this.sauces.createSauce(newSauce, this.sauceForm.get('image')!.value).pipe( 100 | tap(({ message }) => { 101 | console.log(message); 102 | this.loading = false; 103 | this.router.navigate(['/sauces']); 104 | }), 105 | catchError(error => { 106 | console.error(error); 107 | this.loading = false; 108 | this.errorMsg = error.message; 109 | return EMPTY; 110 | }) 111 | ).subscribe(); 112 | } else if (this.mode === 'edit') { 113 | this.sauces.modifySauce(this.sauce._id, newSauce, this.sauceForm.get('image')!.value).pipe( 114 | tap(({ message }) => { 115 | console.log(message); 116 | this.loading = false; 117 | this.router.navigate(['/sauces']); 118 | }), 119 | catchError(error => { 120 | console.error(error); 121 | this.loading = false; 122 | this.errorMsg = error.message; 123 | return EMPTY; 124 | }) 125 | ).subscribe(); 126 | } 127 | } 128 | 129 | onFileAdded(event: Event) { 130 | const file = (event.target as HTMLInputElement).files![0]; 131 | this.sauceForm.get('image')!.setValue(file); 132 | this.sauceForm.updateValueAndValidity(); 133 | const reader = new FileReader(); 134 | reader.onload = () => { 135 | this.imagePreview = reader.result as string; 136 | }; 137 | reader.readAsDataURL(file); 138 | } 139 | } 140 | --------------------------------------------------------------------------------