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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Injectable } from '@angular/core';
import {
AngularFirestore,
AngularFirestoreCollection
} from '@angular/fire/firestore';
import { take } from 'rxjs/operators';
import { Address, AddressList } from './services/address';
@Injectable({
providedIn: 'root'
})
export class AddressService {
addressList: AddressList[] = [];
constructor(public afs: AngularFirestore) {}
public async addList(userId: string) {
const This = this;
const state = true;
const orderRef: AngularFirestoreCollection<any> = this.afs.collection(
'address',
ref => ref.where('status', '==', state).where('uid', '==', userId)
);
orderRef.valueChanges().subscribe(value => {
this.addressList = [];
const res = value;
if (res.length > 0) {
res.forEach(item => {
// console.log(item);
const address: AddressList = {
address: item.address,
addressType: item.addressType,
latLng: item.latLng,
defaultVal: item.defaultVal,
building: item.building,
landmark: item.landmark,
addressId: item.addressId
};
this.addressList.push(address);
});
console.log(this.addressList);
} else {
// alert('No Orders Found');
}
});
}
addressCreate(data: Address) {
console.log(data);
this.afs
.collection('address')
.add({})
.then(docRef => {
data.addressId = docRef.id;
this.afs
.collection('address')
.doc(data.addressId)
.set(data)
.then(() => {
this.setDefaultAddress(data.addressId, data.uid);
// console.log('Address created Successfully');
});
});
}
setDefaultAddress(addId: string, userId: string) {
this.afs
.collection('address', ref => ref.where('uid', '==', userId))
.get()
.forEach(item => {
return item.docs.map(m => {
return this.afs.doc(`address/${m.id}`).update({ defaultVal: 0 });
});
});
setTimeout(() => {
console.log(addId);
this.afs
.collection('address')
.doc(addId)
.update({ defaultVal: 1 })
.then(() => {
console.log('Address updated Successfully');
});
}, 1000);
}
deleteAddress(addId: string) {
this.afs
.collection('address')
.doc(addId)
.delete();
}
}