-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement adding, editing and displaying plants (#6)
* Displaying basic plant info * Add plants service and plant delete button * Add plants list element * Change plants service directory name * Implement adding new plants * Add displaying image in PlantsListElement and deleting images * Implement editing plants * Change the way images are handled in db * Fix adding image when adding plant
- Loading branch information
Showing
39 changed files
with
1,025 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,4 @@ | |
"projects": { | ||
"default": "watering-reminder" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,5 +48,8 @@ | |
"ui": { | ||
"enabled": true | ||
} | ||
}, | ||
"storage": { | ||
"rules": "storage.rules" | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export interface Plant { | ||
id: string | ||
name: string | ||
description: string | ||
timezone: string | ||
waterTime: string | ||
imageUrl: string | ||
} | ||
|
||
export type PlantWithoutImage = Omit<Plant, 'imageUrl'> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/pages/action/modes/email-verification/email-verification.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/app/pages/action/modes/password-reset/password-reset.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<app-plant-form [formGroup]="form" [(imageUrl)]="imageUrl" [(isCropping)]="isCropping"></app-plant-form> | ||
<br> | ||
<button [disabled]="!canAddPlant" (click)="addPlant()">Add plant</button> | ||
<button routerLink="/dashboard">Cancel</button> | ||
<br> | ||
<div [ngSwitch]="addPlantState"> | ||
<div *ngSwitchCase="AddPlantState.Loading"> | ||
Loading | ||
</div> | ||
|
||
<div *ngSwitchCase="AddPlantState.Error"> | ||
<p>Error adding plant</p> | ||
</div> | ||
</div> |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { AddPlantComponent } from './add-plant.component'; | ||
|
||
describe('AddPlantComponent', () => { | ||
let component: AddPlantComponent; | ||
let fixture: ComponentFixture<AddPlantComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ AddPlantComponent ] | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(AddPlantComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { Component } from '@angular/core' | ||
import { FormControl, FormGroup, Validators } from '@angular/forms' | ||
import { Router } from '@angular/router' | ||
import { PlantsService } from 'src/app/services/plants/plants.service' | ||
|
||
enum AddPlantState { | ||
WaitingForInput, | ||
Loading, | ||
Error, | ||
} | ||
|
||
@Component({ | ||
selector: 'app-add-plant', | ||
templateUrl: './add-plant.component.html', | ||
styleUrls: ['./add-plant.component.scss'] | ||
}) | ||
export class AddPlantComponent { | ||
AddPlantState = AddPlantState | ||
|
||
form = new FormGroup({ | ||
name: new FormControl('', Validators.required), | ||
description: new FormControl(''), | ||
timezone: new FormControl(Intl.DateTimeFormat().resolvedOptions().timeZone, Validators.required), | ||
waterTime: new FormControl('', Validators.required), | ||
}) | ||
imageUrl = '' | ||
isCropping = false | ||
addPlantState = AddPlantState.WaitingForInput | ||
|
||
constructor(public plantsService: PlantsService, public router: Router) { } | ||
|
||
async addPlant() { | ||
if (this.canAddPlant) { | ||
try { | ||
this.addPlantState = AddPlantState.Loading | ||
await this.plantsService.addPlant(this.form.value, this.imageUrl) | ||
await this.router.navigate(['/dashboard']) | ||
} catch (error) { | ||
console.error(error) | ||
this.addPlantState = AddPlantState.Error | ||
} | ||
} | ||
} | ||
|
||
get canAddPlant() { | ||
return this.form.valid && !this.isCropping | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
<p>dashboard works!</p> | ||
<button (click)="logout()">Logout</button> | ||
<button (click)="auth.logUser()">Log user</button> | ||
|
||
<div *ngIf="isLoading; then loadingBlock else loadedBlock"></div> | ||
|
||
<ng-template #loadingBlock> | ||
<p>Loading</p> | ||
</ng-template> | ||
|
||
<ng-template #loadedBlock> | ||
<div *ngIf="plantsService.plantsAreEmpty$ | async; then plantsEmptyBlock else plantsNotEmptyBlock"></div> | ||
|
||
<ng-template #plantsEmptyBlock> | ||
<p>You have no plants.</p> | ||
<button routerLink="/add-plant">Add your first plant</button> | ||
</ng-template> | ||
|
||
<ng-template #plantsNotEmptyBlock> | ||
<button routerLink="/add-plant">Add a plant</button> | ||
<app-plants-list-element *ngFor="let plant of plantsService.plants$ | async" [plant]="plant"> | ||
</app-plants-list-element> | ||
</ng-template> | ||
</ng-template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,22 @@ | ||
import { Component } from '@angular/core' | ||
import { Router } from '@angular/router' | ||
import { AuthService } from 'src/app/services/auth.service' | ||
import { AuthService } from 'src/app/services/auth/auth.service' | ||
import { PlantsService } from 'src/app/services/plants/plants.service' | ||
|
||
@Component({ | ||
selector: 'app-dashboard', | ||
templateUrl: './dashboard.component.html', | ||
styleUrls: ['./dashboard.component.scss'] | ||
}) | ||
export class DashboardComponent { | ||
isLoading = true | ||
|
||
constructor(public auth: AuthService, public router: Router) { } | ||
constructor(public auth: AuthService, public router: Router, public plantsService: PlantsService) { | ||
this.plantsService.plants$.subscribe(() => this.isLoading = false) | ||
} | ||
|
||
async logout() { | ||
await this.auth.logout() | ||
await this.router.navigate(['/login']) | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/app/pages/dashboard/plants-list-element/plants-list-element.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<h3>{{ plant.name }}</h3> | ||
<p>{{ plant.description }} {{ plant.waterTime }}</p> | ||
<button (click)="deletePlant()">Delete</button> | ||
<p *ngIf="deleteState === DeleteState.Loading">Loading</p> | ||
<p *ngIf="deleteState === DeleteState.Error">Could not delete plant</p> | ||
<button [routerLink]="'/edit-plant/' + plant.id">Edit</button> | ||
<img [src]="plant.imageUrl ? plant.imageUrl : 'assets/images/default_plant_image.png'" alt="plant image"> | ||
<hr> |
Empty file.
25 changes: 25 additions & 0 deletions
25
src/app/pages/dashboard/plants-list-element/plants-list-element.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { PlantsListElementComponent } from './plants-list-element.component'; | ||
|
||
describe('PlantsListElementComponent', () => { | ||
let component: PlantsListElementComponent; | ||
let fixture: ComponentFixture<PlantsListElementComponent>; | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
declarations: [ PlantsListElementComponent ] | ||
}) | ||
.compileComponents(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(PlantsListElementComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy(); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
src/app/pages/dashboard/plants-list-element/plants-list-element.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { Component, Input } from '@angular/core' | ||
import { Plant } from 'src/app/models/plant.model' | ||
import { PlantsService } from 'src/app/services/plants/plants.service' | ||
|
||
enum DeleteState { | ||
WaitingForInput, | ||
Loading, | ||
Error, | ||
} | ||
|
||
@Component({ | ||
selector: 'app-plants-list-element', | ||
templateUrl: './plants-list-element.component.html', | ||
styleUrls: ['./plants-list-element.component.scss'] | ||
}) | ||
export class PlantsListElementComponent { | ||
@Input() plant!: Plant | ||
|
||
DeleteState = DeleteState | ||
deleteState = DeleteState.WaitingForInput | ||
|
||
constructor(public plantsService: PlantsService) { } | ||
|
||
async deletePlant() { | ||
try { | ||
this.deleteState = DeleteState.Loading | ||
await this.plantsService.deletePlant(this.plant.id, !!this.plant.imageUrl) | ||
} | ||
catch (e) { | ||
this.deleteState = DeleteState.Error | ||
console.error(e) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.