Skip to content

Commit 555c562

Browse files
committed
14.18 image upload (alternate cloudinary)
1 parent ab9b18b commit 555c562

File tree

5 files changed

+65
-24
lines changed

5 files changed

+65
-24
lines changed

src/app/places/offers/new-offer/new-offer.page.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
</ion-buttons>
66
<ion-title>New Offer</ion-title>
77
<ion-buttons slot="primary">
8-
<ion-button (click)="onCreateOffer()" [disabled]="form.invalid">
8+
<ion-button (click)="onCreateOffer()" [disabled]="form.invalid || !form.get('image').value">
99
<ion-icon name="checkmark" slot="icon-only"></ion-icon>
1010
</ion-button>
1111
</ion-buttons>
@@ -71,7 +71,7 @@
7171
</ion-col>
7272
</ion-row>
7373
<ion-row>
74-
<ion-col size-sm="3" offset-sm="3">
74+
<ion-col size-sm="6" offset-sm="3" >
7575
<app-image-picker (imagePick)="onImagePicked($event)"></app-image-picker>
7676
</ion-col>
7777
</ion-row>

src/app/places/offers/new-offer/new-offer.page.ts

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { FormControl, FormGroup, Validators } from '@angular/forms';
33
import { PlacesService } from '../../places.service';
44
import { Router } from '@angular/router';
55
import { LoadingController } from '@ionic/angular';
6+
import { switchMap } from 'rxjs';
67

78
@Component({
89
selector: 'app-new-offer',
@@ -40,37 +41,45 @@ export class NewOfferPage implements OnInit {
4041
updateOn: 'blur',
4142
validators: [Validators.required],
4243
}),
44+
image: new FormControl(null),
4345
});
4446
}
4547

4648
onCreateOffer() {
47-
if (this.form.invalid) {
49+
if (this.form.invalid || !this.form.get('image').value) {
4850
return;
4951
}
52+
5053
this.loaderCtrl
5154
.create({
5255
message: 'Creating place..',
5356
})
5457
.then((loadingEl) => {
5558
loadingEl.present();
5659
this.placeService
57-
.addplaces(
58-
this.form.value.title,
59-
this.form.value.description,
60-
+this.form.value.price,
61-
new Date(this.form.value.dateFrom),
62-
new Date(this.form.value.dateTo)
63-
)
64-
.subscribe(() => {
65-
loadingEl.dismiss();
66-
this.form.reset();
67-
this.Router.navigate(['/places/tabs/offers']);
68-
});
60+
.uploadImage(this.form.get('image').value)
61+
.pipe(
62+
switchMap(uploadRes => {
63+
return this.placeService.addplaces(
64+
this.form.value.title,
65+
this.form.value.description,
66+
+this.form.value.price,
67+
new Date(this.form.value.dateFrom),
68+
new Date(this.form.value.dateTo),
69+
uploadRes
70+
);
71+
})
72+
)
73+
.subscribe(() => {
74+
loadingEl.dismiss();
75+
this.form.reset();
76+
this.Router.navigate(['/places/tabs/offers']);
77+
});
6978
});
7079
}
7180

7281

7382
onImagePicked(imgData) {
74-
83+
this.form.patchValue({ image: imgData });
7584
}
7685
}

src/app/places/places.service.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,34 @@ export class PlacesService {
8080
);
8181
}
8282

83+
uploadImage(image: File) {
84+
return this.http
85+
.post(
86+
'http://localhost:5000/api/uploadImage',
87+
{ image },
88+
{ responseType: 'text' }
89+
)
90+
.pipe(
91+
map((url) => {
92+
return url;
93+
})
94+
);
95+
}
96+
8397
addplaces(
8498
title: string,
8599
description: string,
86100
price: number,
87101
dateFrom: Date,
88-
dateTo: Date
102+
dateTo: Date,
103+
imageUrl: string
89104
) {
90105
let generatedId: string;
91106
const newPlace = new Place(
92107
Math.random().toString(),
93108
title,
94109
description,
95-
'https://cf.bstatic.com/xdata/images/hotel/max1280x900/251732450.jpg?k=c291e2faa860f79f1a2f81766e5d518ab05bc27f0e421b7d5e1ec6f4e1dd7b54&o=&hp=1',
110+
imageUrl,
96111
price,
97112
dateFrom,
98113
dateTo,
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.picker {
2+
width: 30rem;
3+
max-width: 80%;
4+
height: 20rem;
5+
max-height: 30vh;
6+
border: 1px solid var(--ion-color-primary);
7+
margin: auto;
8+
display: flex;
9+
justify-content: center;
10+
align-items: center;
11+
}
12+
13+
.image {
14+
width: 100%;
15+
height: 100%;
16+
object-fit: cover;
17+
}

src/app/shared/pickers/image-picker/image-picker.component.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Camera, CameraResultType } from '@capacitor/camera';
77
styleUrls: ['./image-picker.component.scss'],
88
})
99
export class ImagePickerComponent implements OnInit {
10-
@Output() imagePick = new EventEmitter<string>();
10+
@Output() imagePick = new EventEmitter<string | File>();
1111

1212
selectedImage: string;
1313

@@ -18,14 +18,14 @@ export class ImagePickerComponent implements OnInit {
1818
onPickImage() {
1919
Camera.getPhoto({
2020
quality: 90,
21-
allowEditing: true,
21+
allowEditing: false,
2222
height: 320,
23-
width: 200,
24-
resultType: CameraResultType.Uri,
23+
width: 600,
24+
resultType: CameraResultType.DataUrl,
2525
})
2626
.then((image) => {
27-
this.selectedImage = image.webPath;
28-
this.imagePick.emit(image.webPath);
27+
this.selectedImage = image.dataUrl;
28+
this.imagePick.emit(image.dataUrl);
2929
})
3030
.catch((err) => {
3131
console.log(err);

0 commit comments

Comments
 (0)