@tech4good/angular-schematics 13.2.0 → 13.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/collection.json +6 -0
- package/src/container/files/__name@dasherize__/+state/__name@dasherize__.actions.ts.template +2 -4
- package/src/container/files/__name@dasherize__/+state/__name@dasherize__.effects.ts.template +1 -1
- package/src/container/files/__name@dasherize__/__name@dasherize__.component.ts.template +2 -4
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.actions.ts.template +270 -0
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.effects.ts.template +131 -0
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.model.ts.template +3 -0
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.spec.ts.template +13 -0
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.reducer.ts.template +51 -0
- package/src/entity-in-example/files/__name@dasherize__/__name@dasherize__.service.ts.template +50 -0
- package/src/entity-in-example/index.d.ts +2 -0
- package/src/entity-in-example/index.js +141 -0
- package/src/entity-in-example/index.js.map +1 -0
- package/src/entity-in-example/index_spec.d.ts +1 -0
- package/src/entity-in-example/index_spec.js +23 -0
- package/src/entity-in-example/index_spec.js.map +1 -0
- package/src/entity-in-example/schema.json +42 -0
- package/src/init/files/src/app/app-routing.module.ts.template +19 -0
- package/src/init/files/src/app/{app.component.html → app.component.html.template} +0 -0
- package/src/init/files/src/app/{app.component.scss → app.component.scss.template} +0 -0
- package/src/init/files/src/app/app.component.ts.template +41 -0
- package/src/init/files/src/app/app.module.ts.template +34 -0
- package/src/init/files/src/app/core/analytics/log.service.ts.template +35 -0
- package/src/init/files/src/app/core/analytics/time-analytics.service.ts.template +223 -0
- package/src/init/files/src/app/core/core.module.ts.template +71 -0
- package/src/init/files/src/app/core/firebase/firebase.service.ts.template +284 -0
- package/src/init/files/src/app/core/router/routing-state.service.ts.template +28 -0
- package/src/init/files/src/app/core/setup/firebase-setup.ts.template +25 -0
- package/src/init/files/src/app/core/setup/module-import-guard.ts.template +5 -0
- package/src/init/files/src/app/core/snackbar/snackbar.actions.ts.template +24 -0
- package/src/init/files/src/app/core/snackbar/snackbar.effects.ts.template +41 -0
- package/src/init/files/src/app/core/store/app.actions.ts.template +92 -0
- package/src/init/files/src/app/core/store/app.effects.ts.template +167 -0
- package/src/init/files/src/app/core/store/app.reducer.ts.template +30 -0
- package/src/init/files/src/app/core/store/app.selectors.ts.template +30 -0
- package/src/init/files/src/app/core/store/auth/auth.actions.ts.template +75 -0
- package/src/init/files/src/app/core/store/auth/auth.effects.ts.template +164 -0
- package/src/init/files/src/app/core/store/auth/auth.reducer.spec.ts.template +13 -0
- package/src/init/files/src/app/core/store/auth/auth.reducer.ts.template +26 -0
- package/src/init/files/src/app/core/store/auth/auth.selectors.ts.template +21 -0
- package/src/init/files/src/app/core/store/cached-load-connections.service.ts.template +404 -0
- package/src/init/files/src/app/core/store/cached-selectors.service.ts.template +376 -0
- package/src/init/files/src/app/core/store/effects.helpers.ts.template +66 -0
- package/src/init/files/src/app/core/store/user/user.actions.ts.template +269 -0
- package/src/init/files/src/app/core/store/user/user.effects.ts.template +131 -0
- package/src/init/files/src/app/core/store/user/user.model.ts.template +15 -0
- package/src/init/files/src/app/core/store/user/user.reducer.spec.ts.template +13 -0
- package/src/init/files/src/app/core/store/user/user.reducer.ts.template +50 -0
- package/src/init/files/src/app/core/store/user/user.service.ts.template +50 -0
- package/src/init/files/src/app/core/utils/string-case.utils.ts.template +96 -0
- package/src/init/files/src/app/general/e404/{e404.component.html → e404.component.html.template} +0 -0
- package/src/init/files/src/app/general/e404/{e404.component.scss → e404.component.scss.template} +0 -0
- package/src/init/files/src/app/general/e404/e404.component.spec.ts.template +25 -0
- package/src/init/files/src/app/general/e404/e404.component.ts.template +28 -0
- package/src/init/files/src/app/general/general-routing.module.ts.template +21 -0
- package/src/init/files/src/app/general/general.module.spec.ts.template +13 -0
- package/src/init/files/src/app/general/general.module.ts.template +30 -0
- package/src/init/files/src/app/general/login/{login.component.html → login.component.html.template} +0 -0
- package/src/init/files/src/app/general/login/{login.component.scss → login.component.scss.template} +0 -0
- package/src/init/files/src/app/general/login/login.component.spec.ts.template +25 -0
- package/src/init/files/src/app/general/login/login.component.ts.template +72 -0
- package/src/init/files/src/app/shared/shared.module.ts.template +81 -0
- package/src/init/files/src/environments/environment.prod.ts.template +5 -0
- package/src/init/files/src/environments/environment.ts.template +18 -0
- /package/src/init/files/src/styles/{_variables.scss → _variables.scss.template} +0 -0
- /package/src/init/files/src/{styles.scss → styles.scss.template} +0 -0
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { AngularFirestore } from '@angular/fire/compat/firestore';
|
|
3
|
+
import { AngularFireAuth } from '@angular/fire/compat/auth';
|
|
4
|
+
import { Observable, combineLatest, merge, of, from, pipe } from 'rxjs';
|
|
5
|
+
import { tap, pluck, mergeMap, filter, takeUntil, skip, switchMap, map, take } from 'rxjs/operators';
|
|
6
|
+
import firebase from 'firebase/compat/app';
|
|
7
|
+
import { environment } from '../../../environments/environment';
|
|
8
|
+
|
|
9
|
+
@Injectable({
|
|
10
|
+
providedIn: 'root',
|
|
11
|
+
})
|
|
12
|
+
export class FirebaseService {
|
|
13
|
+
constructor(
|
|
14
|
+
private afStore: AngularFirestore,
|
|
15
|
+
private afAuth: AngularFireAuth,
|
|
16
|
+
) { }
|
|
17
|
+
|
|
18
|
+
/** Creates a unique id */
|
|
19
|
+
createId = (): string => {
|
|
20
|
+
return this.afStore.createId();
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
afUser = () => {
|
|
24
|
+
return this.afAuth.user;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
login = (providerId, scope?: string): Observable<any> => {
|
|
28
|
+
const provider = this.getProvider(providerId, scope);
|
|
29
|
+
|
|
30
|
+
if (provider) {
|
|
31
|
+
return from(this.afAuth.signInWithPopup(provider)).pipe(
|
|
32
|
+
mergeMap((results) => {
|
|
33
|
+
return this.queryObjOnce('users', results.user.uid).pipe(
|
|
34
|
+
map((dbUser) => Object.assign({}, results, { dbUser })),
|
|
35
|
+
);
|
|
36
|
+
}),
|
|
37
|
+
);
|
|
38
|
+
} else {
|
|
39
|
+
return of(undefined);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
private getProvider(providerId, scope?: string) {
|
|
44
|
+
let provider;
|
|
45
|
+
switch (providerId) {
|
|
46
|
+
case 'google.com': {
|
|
47
|
+
provider = new firebase.auth.GoogleAuthProvider();
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'github.com': {
|
|
51
|
+
provider = new firebase.auth.GithubAuthProvider();
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (provider && scope) {
|
|
56
|
+
provider.addScope(scope);
|
|
57
|
+
}
|
|
58
|
+
return provider;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
loginLink = (error) => {
|
|
62
|
+
firebase.auth().fetchSignInMethodsForEmail(error.email)
|
|
63
|
+
.then((providers) => {
|
|
64
|
+
this.queryListOnce('users', [['email', '==', error.email]]).subscribe((users) => {
|
|
65
|
+
firebase.auth().signInWithPopup(this.getProvider(providers[0])).then((auth1) => {
|
|
66
|
+
firebase.auth().signInWithCredential(auth1.credential)
|
|
67
|
+
.then((auth2) => {
|
|
68
|
+
auth2.user.linkWithCredential(error.credential);
|
|
69
|
+
})
|
|
70
|
+
.catch((error2) => console.log(error2));
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
logout = (): Observable<any> => {
|
|
77
|
+
return from(this.afAuth.signOut());
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
queryObjValueChanges = <T>(collection: string, id: string) => {
|
|
81
|
+
return this.afStore.doc<T>(`/${collection}/${id}`).valueChanges();
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
queryObjOnce<S>(
|
|
85
|
+
collection: string,
|
|
86
|
+
id: string,
|
|
87
|
+
): Observable<S>;
|
|
88
|
+
|
|
89
|
+
queryObjOnce<S, T>(
|
|
90
|
+
collection: string,
|
|
91
|
+
id: string,
|
|
92
|
+
childrenFn?: (e: S) => { [index: string]: Observable<any> },
|
|
93
|
+
): Observable<T>;
|
|
94
|
+
|
|
95
|
+
queryObjOnce<S, T>(
|
|
96
|
+
collection: string,
|
|
97
|
+
id: string,
|
|
98
|
+
childrenFn?: (e: S) => { [index: string]: Observable<any> },
|
|
99
|
+
): Observable<T> {
|
|
100
|
+
return this.queryObjValueChanges<S>(collection, id).pipe(
|
|
101
|
+
take(1),
|
|
102
|
+
switchMap((e) => {
|
|
103
|
+
if (!e || !childrenFn) {
|
|
104
|
+
return of(e);
|
|
105
|
+
} else {
|
|
106
|
+
const children = childrenFn(e);
|
|
107
|
+
// transform the hash of observables into an array of observable hashes
|
|
108
|
+
const obsArray = Object.keys(children).map((objKey) => {
|
|
109
|
+
const obs = children[objKey];
|
|
110
|
+
return obs.pipe(
|
|
111
|
+
map((entity) => {
|
|
112
|
+
const obj = {};
|
|
113
|
+
obj[objKey] = entity;
|
|
114
|
+
return obj;
|
|
115
|
+
}),
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
return obsArray.length === 0 ? of(e) : combineLatest(obsArray).pipe(
|
|
119
|
+
map((array) => {
|
|
120
|
+
return Object.assign({}, e, ...array);
|
|
121
|
+
}),
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
queryListStateChanges = <T>(collection, queryParams, queryOptions?) => {
|
|
129
|
+
let stateChangeObs$;
|
|
130
|
+
|
|
131
|
+
if (queryParams.length === 0) {
|
|
132
|
+
stateChangeObs$ = this.afStore.collection<T>(`/${collection}`).stateChanges();
|
|
133
|
+
} else {
|
|
134
|
+
stateChangeObs$ = this.afStore.collection<T>(
|
|
135
|
+
`/${collection}`,
|
|
136
|
+
this.constructQueryListRef(queryParams, queryOptions),
|
|
137
|
+
).stateChanges();
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return stateChangeObs$.pipe(
|
|
141
|
+
map((changes: any[]) => {
|
|
142
|
+
return changes.map((change) => {
|
|
143
|
+
return {
|
|
144
|
+
type: change.type,
|
|
145
|
+
result: change.payload.doc.data(),
|
|
146
|
+
};
|
|
147
|
+
});
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
queryListValueChanges = <T>(collection, queryParams, queryOptions?) => {
|
|
153
|
+
if (queryParams.length === 0) {
|
|
154
|
+
return this.afStore.collection<T>(`/${collection}`).valueChanges();
|
|
155
|
+
} else {
|
|
156
|
+
return this.afStore.collection<T>(
|
|
157
|
+
`/${collection}`,
|
|
158
|
+
this.constructQueryListRef(queryParams, queryOptions),
|
|
159
|
+
).valueChanges();
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
private constructQueryListRef = (queryParams, queryOptions?) => (ref) => {
|
|
164
|
+
const refWithQueries = queryParams.reduce((func, query) => {
|
|
165
|
+
const [prop, comp, val] = query;
|
|
166
|
+
return func.where(prop, comp, val);
|
|
167
|
+
}, ref);
|
|
168
|
+
if (!queryOptions) {
|
|
169
|
+
queryOptions = {};
|
|
170
|
+
}
|
|
171
|
+
return Object.keys(queryOptions).reduce((func, queryKey) => {
|
|
172
|
+
switch (queryKey) {
|
|
173
|
+
case 'orderBy': {
|
|
174
|
+
if (Array.isArray(queryOptions.orderBy)) {
|
|
175
|
+
return func.orderBy(...queryOptions.orderBy);
|
|
176
|
+
} else {
|
|
177
|
+
return func.orderBy(queryOptions.orderBy);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
case 'limit':
|
|
181
|
+
return func.limit(queryOptions.limit);
|
|
182
|
+
|
|
183
|
+
case 'startAt':
|
|
184
|
+
return func.startAt(queryOptions.startAt);
|
|
185
|
+
|
|
186
|
+
case 'startAfter':
|
|
187
|
+
return func.startAfter(queryOptions.startAfter);
|
|
188
|
+
|
|
189
|
+
case 'endAt':
|
|
190
|
+
return func.endAt(queryOptions.endAt);
|
|
191
|
+
|
|
192
|
+
case 'endBefore':
|
|
193
|
+
return func.endBefore(queryOptions.endBefore);
|
|
194
|
+
|
|
195
|
+
default:
|
|
196
|
+
return func;
|
|
197
|
+
}
|
|
198
|
+
}, refWithQueries);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
queryListOnce<S>(
|
|
202
|
+
collection: string,
|
|
203
|
+
queryParams: [string, string, any][],
|
|
204
|
+
queryOptions?: { [index: string]: any },
|
|
205
|
+
): Observable<S[]>;
|
|
206
|
+
|
|
207
|
+
queryListOnce<S, T>(
|
|
208
|
+
collection: string,
|
|
209
|
+
queryParams: [string, string, any][],
|
|
210
|
+
queryOptions?: { [index: string]: any },
|
|
211
|
+
childrenFn?: (e: S) => { [index: string]: Observable<any> },
|
|
212
|
+
): Observable<T[]>;
|
|
213
|
+
|
|
214
|
+
queryListOnce<S, T>(
|
|
215
|
+
collection: string,
|
|
216
|
+
queryParams: [string, string, any][],
|
|
217
|
+
queryOptions?: { [index: string]: any },
|
|
218
|
+
childrenFn?: (e: S) => { [index: string]: Observable<any> },
|
|
219
|
+
): Observable<T[]> {
|
|
220
|
+
return this.queryListValueChanges<S>(collection, queryParams, queryOptions).pipe(
|
|
221
|
+
take(1),
|
|
222
|
+
switchMap((entities) => {
|
|
223
|
+
if (!entities || !childrenFn || entities.length === 0) {
|
|
224
|
+
return of(entities);
|
|
225
|
+
} else {
|
|
226
|
+
return combineLatest(entities.map((e) => {
|
|
227
|
+
// If there are children, then we need to transform the hash of observables into an observable with the data object
|
|
228
|
+
const children = childrenFn(e);
|
|
229
|
+
|
|
230
|
+
const obsArray = Object.keys(children).map((objKey) => {
|
|
231
|
+
const obs = children[objKey];
|
|
232
|
+
return obs.pipe(
|
|
233
|
+
map((entity) => {
|
|
234
|
+
const obj = {};
|
|
235
|
+
obj[objKey] = entity;
|
|
236
|
+
return obj;
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
});
|
|
240
|
+
return obsArray.length === 0 ? of(e) : combineLatest(obsArray).pipe(
|
|
241
|
+
map((array) => {
|
|
242
|
+
return Object.assign({}, e, ...array);
|
|
243
|
+
}),
|
|
244
|
+
);
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
}),
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
addEntity = (collection, entity) => {
|
|
252
|
+
return from(this.afStore.doc(`/${collection}/${entity.__id}`).set(Object.assign({}, entity, {
|
|
253
|
+
_createdAt: firebase.firestore.Timestamp.now(),
|
|
254
|
+
_updatedAt: firebase.firestore.Timestamp.now(),
|
|
255
|
+
})));
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
updateEntity = (collection, id, changes) => {
|
|
259
|
+
return from(this.afStore.doc(`/${collection}/${id}`).update(Object.assign({}, changes, {
|
|
260
|
+
_updatedAt: firebase.firestore.Timestamp.now(),
|
|
261
|
+
})));
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
upsertEntity = (collection, entity): Observable<{ type: string, value: any }> => {
|
|
265
|
+
return from((async () => {
|
|
266
|
+
const dbEntity = await this.queryObjOnce(collection, entity.__id).toPromise();
|
|
267
|
+
if (dbEntity) {
|
|
268
|
+
return {
|
|
269
|
+
type: 'update',
|
|
270
|
+
value: await this.updateEntity(collection, entity.__id, entity),
|
|
271
|
+
};
|
|
272
|
+
} else {
|
|
273
|
+
return {
|
|
274
|
+
type: 'add',
|
|
275
|
+
value: await this.addEntity(collection, entity),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
})());
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
removeEntity = (collection, id) => {
|
|
282
|
+
return from(this.afStore.doc(`/${collection}/${id}`).delete());
|
|
283
|
+
};
|
|
284
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { Router, NavigationEnd } from '@angular/router';
|
|
3
|
+
import { filter } from 'rxjs/operators';
|
|
4
|
+
|
|
5
|
+
@Injectable({
|
|
6
|
+
providedIn: 'root',
|
|
7
|
+
})
|
|
8
|
+
export class RoutingStateService {
|
|
9
|
+
private prevUrl: string;
|
|
10
|
+
private currUrl: string;
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
private router: Router,
|
|
14
|
+
) { }
|
|
15
|
+
|
|
16
|
+
public loadRouting(): void {
|
|
17
|
+
this.router.events.pipe(
|
|
18
|
+
filter((event) => event instanceof NavigationEnd),
|
|
19
|
+
).subscribe(({ urlAfterRedirects }: NavigationEnd) => {
|
|
20
|
+
this.prevUrl = this.currUrl || '/';
|
|
21
|
+
this.currUrl = urlAfterRedirects;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public getPrevUrl(): string {
|
|
26
|
+
return this.prevUrl || '/';
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Injectable, NgZone } from '@angular/core';
|
|
2
|
+
import { AngularFirestore } from '@angular/fire/compat/firestore';
|
|
3
|
+
import { ɵAngularFireSchedulers } from '@angular/fire';
|
|
4
|
+
import { AppCheckInstances } from '@angular/fire/app-check';
|
|
5
|
+
import { AngularFireAuth } from '@angular/fire/compat/auth';
|
|
6
|
+
import { environment } from '../../../environments/environment';
|
|
7
|
+
|
|
8
|
+
@Injectable()
|
|
9
|
+
export class MainAngularFirestore extends AngularFirestore { }
|
|
10
|
+
|
|
11
|
+
@Injectable()
|
|
12
|
+
export class LoggingAngularFirestore extends AngularFirestore { }
|
|
13
|
+
|
|
14
|
+
export function MainAngularFirestoreFactory(platformId: Object, zone: NgZone): MainAngularFirestore {
|
|
15
|
+
const schedulers = new ɵAngularFireSchedulers(zone);
|
|
16
|
+
const auth = new AngularFireAuth(environment.firebase, 'mainAuth', platformId, zone, schedulers, null, null, null, null, null, null, new AppCheckInstances());
|
|
17
|
+
|
|
18
|
+
return new MainAngularFirestore(environment.firebase, 'main', true, {}, platformId, zone, schedulers, {}, null, auth, null, null, null, null, null, null, new AppCheckInstances());
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function LoggingAngularFirestoreFactory(platformId: Object, zone: NgZone): LoggingAngularFirestore {
|
|
22
|
+
const schedulers = new ɵAngularFireSchedulers(zone);
|
|
23
|
+
const auth = new AngularFireAuth(environment.firebase, 'loggingAuth', platformId, zone, schedulers, null, null, null, null, null, null, new AppCheckInstances());
|
|
24
|
+
return new LoggingAngularFirestore(environment.firebaseLogs, 'logging', true, {}, platformId, zone, schedulers, {}, null, auth, null, null, null, null, null, null, new AppCheckInstances());
|
|
25
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Action } from '@ngrx/store';
|
|
2
|
+
import { MatSnackBarConfig, MatSnackBarRef } from '@angular/material/snack-bar';
|
|
3
|
+
|
|
4
|
+
export enum SnackbarActionTypes {
|
|
5
|
+
SHOW_MESSAGE = '[Snackbar] show snackbar message',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/** If there is a component, will use openFromComponent. Otherwise, will use message. At least one of component or message needs to be provided. */
|
|
9
|
+
export class ShowSnackbar implements Action {
|
|
10
|
+
readonly type = SnackbarActionTypes.SHOW_MESSAGE;
|
|
11
|
+
constructor(
|
|
12
|
+
public payload: {
|
|
13
|
+
component?: any,
|
|
14
|
+
message?: string,
|
|
15
|
+
action?: string,
|
|
16
|
+
config?: MatSnackBarConfig<any>,
|
|
17
|
+
onAction?: (snackBarRef: MatSnackBarRef<any>) => any,
|
|
18
|
+
},
|
|
19
|
+
public correlationId?: string,
|
|
20
|
+
) {}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type SnackbarActions =
|
|
24
|
+
ShowSnackbar;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { Action } from '@ngrx/store';
|
|
3
|
+
import { Actions, createEffect, ofType } from '@ngrx/effects';
|
|
4
|
+
import { of, Observable } from 'rxjs';
|
|
5
|
+
import { tap, take } from 'rxjs/operators';
|
|
6
|
+
import { SnackbarActionTypes, ShowSnackbar } from './snackbar.actions';
|
|
7
|
+
import { MatSnackBar } from '@angular/material/snack-bar';
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class SnackbarEffects {
|
|
11
|
+
|
|
12
|
+
/** Process the snackbar action. */
|
|
13
|
+
showSnackbar$: Observable<Action> = createEffect(
|
|
14
|
+
() => this.actions$.pipe(
|
|
15
|
+
ofType<ShowSnackbar>(SnackbarActionTypes.SHOW_MESSAGE),
|
|
16
|
+
tap((action: ShowSnackbar) => {
|
|
17
|
+
|
|
18
|
+
let snackBarRef;
|
|
19
|
+
const config = action.payload.config;
|
|
20
|
+
|
|
21
|
+
if (action.payload.component) {
|
|
22
|
+
snackBarRef = this.snackBar.openFromComponent(action.payload.component, config);
|
|
23
|
+
} else if (action.payload.message) {
|
|
24
|
+
snackBarRef = this.snackBar.open(action.payload.message, action.payload.action || '', config);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (action.payload.onAction) {
|
|
28
|
+
snackBarRef.onAction().pipe(take(1)).subscribe(() => {
|
|
29
|
+
action.payload.onAction(snackBarRef);
|
|
30
|
+
snackBarRef.dismiss();
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}),
|
|
34
|
+
), { dispatch: false },
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
constructor(
|
|
38
|
+
private actions$: Actions,
|
|
39
|
+
private snackBar: MatSnackBar,
|
|
40
|
+
) {}
|
|
41
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { Action } from '@ngrx/store';
|
|
2
|
+
import { ObservableInput, Subject } from 'rxjs';
|
|
3
|
+
import { NavigationExtras } from '@angular/router';
|
|
4
|
+
|
|
5
|
+
export interface SAction extends Action {
|
|
6
|
+
payload: { [index: string]: any; };
|
|
7
|
+
correlationId?: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface LoadAction extends Action {
|
|
11
|
+
queryParams: [string, string, any][];
|
|
12
|
+
queryOptions: {
|
|
13
|
+
orderBy?: string | [string, string],
|
|
14
|
+
limit?: number,
|
|
15
|
+
startAt?: string,
|
|
16
|
+
startAfter?: string,
|
|
17
|
+
endAt?: string,
|
|
18
|
+
endBefore?: string,
|
|
19
|
+
};
|
|
20
|
+
correlationId: string;
|
|
21
|
+
followupActions?: (entity: any) => LoadAction[];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export enum GeneralActionTypes {
|
|
25
|
+
UNSUBSCRIBE = '[General] unsubscribe',
|
|
26
|
+
ROUTER_NAVIGATE = '[Router] navigate',
|
|
27
|
+
ACTION_FLOW = '[General] action flow',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export enum ActionSetResult {
|
|
31
|
+
SUCCESS = 'success',
|
|
32
|
+
FAILURE = 'failure',
|
|
33
|
+
NONE = 'none', // was not tracked
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface ActionSet {
|
|
37
|
+
action: Action;
|
|
38
|
+
responseActionTypes?: {
|
|
39
|
+
success: string | string[];
|
|
40
|
+
failure: string | string[];
|
|
41
|
+
};
|
|
42
|
+
// this can be used if you need to find a specific action set in the callback
|
|
43
|
+
key?: string;
|
|
44
|
+
callbacks?: {
|
|
45
|
+
// Actions that should be dispatched if this one fails
|
|
46
|
+
onFailure?: ObservableInput<Action>;
|
|
47
|
+
// Action that should be dispatched if this one succeeds,
|
|
48
|
+
// but some other Action in its actionSets batch fails
|
|
49
|
+
onSuccessButOtherFailure?: ObservableInput<Action>;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface ActionSetResponse extends ActionSet {
|
|
54
|
+
result: ActionSetResult;
|
|
55
|
+
responseAction: Action;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* An action that helps orchestrate a flow of actions
|
|
60
|
+
* @param actionSets the group of actions to dispatch. Each set represents an action, the success action type, and the fail action type.
|
|
61
|
+
* @param successActionFn a function executed if all actions are successful, and that returns an array of new actions to dispatch
|
|
62
|
+
* @param failActionFn a function executed if any of the actions fail, and that returns an array of new actions to dispatch
|
|
63
|
+
*/
|
|
64
|
+
export class ActionFlow implements Action {
|
|
65
|
+
readonly type = GeneralActionTypes.ACTION_FLOW;
|
|
66
|
+
constructor(public payload: {
|
|
67
|
+
actionSets: ActionSet[],
|
|
68
|
+
loading$?: Subject<boolean>,
|
|
69
|
+
successActionFn?: (actionSetResponses: ActionSetResponse[]) => ObservableInput<Action>,
|
|
70
|
+
failActionFn?: (actionSetResponses: ActionSetResponse[]) => ObservableInput<Action>,
|
|
71
|
+
}) {}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export class Unsubscribe implements Action {
|
|
75
|
+
readonly type = GeneralActionTypes.UNSUBSCRIBE;
|
|
76
|
+
constructor(
|
|
77
|
+
public correlationId?: string,
|
|
78
|
+
) {}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export class RouterNavigate implements Action {
|
|
82
|
+
readonly type = GeneralActionTypes.ROUTER_NAVIGATE;
|
|
83
|
+
constructor(
|
|
84
|
+
public commands: any[],
|
|
85
|
+
public extras?: NavigationExtras,
|
|
86
|
+
) {}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export type GeneralActions =
|
|
90
|
+
Unsubscribe |
|
|
91
|
+
ActionFlow |
|
|
92
|
+
RouterNavigate;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import { Router } from '@angular/router';
|
|
3
|
+
import { Action } from '@ngrx/store';
|
|
4
|
+
import { Actions, Effect, createEffect, ofType } from '@ngrx/effects';
|
|
5
|
+
import { of, Observable, merge } from 'rxjs';
|
|
6
|
+
import { tap, filter, mergeMap, map, first } from 'rxjs/operators';
|
|
7
|
+
import { pipe, forkJoin } from 'rxjs';
|
|
8
|
+
import { EffectsHelpers } from './effects.helpers';
|
|
9
|
+
import { FirebaseService } from '../firebase/firebase.service';
|
|
10
|
+
import { Store, select } from '@ngrx/store';
|
|
11
|
+
import * as fromStore from './app.reducer';
|
|
12
|
+
|
|
13
|
+
import { GeneralActionTypes, Unsubscribe, ActionSetResult, ActionSet, ActionSetResponse, ActionFlow, RouterNavigate } from './app.actions';
|
|
14
|
+
import { CachedLoadConnectionsService } from './cached-load-connections.service';
|
|
15
|
+
|
|
16
|
+
@Injectable()
|
|
17
|
+
export class AppEffects {
|
|
18
|
+
/** Process the router navigate action to use the Angular Router. */
|
|
19
|
+
routerNavigate$: Observable<Action> = createEffect(
|
|
20
|
+
() => this.actions$.pipe(
|
|
21
|
+
ofType(GeneralActionTypes.ROUTER_NAVIGATE),
|
|
22
|
+
tap((action: RouterNavigate) => {
|
|
23
|
+
this.router.navigate(action.commands, action.extras);
|
|
24
|
+
}),
|
|
25
|
+
),
|
|
26
|
+
{ dispatch: false },
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
/** Process the unsubscribe action. */
|
|
30
|
+
unsubscribe$: Observable<Action> = createEffect(
|
|
31
|
+
() => this.actions$.pipe(
|
|
32
|
+
ofType(GeneralActionTypes.UNSUBSCRIBE),
|
|
33
|
+
tap((action: Unsubscribe) => {
|
|
34
|
+
this.cachedConnections.disconnect(action);
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
{ dispatch: false },
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
/** Process a generic action flow. */
|
|
41
|
+
actionFlow$: Observable<Action> = createEffect(
|
|
42
|
+
() => this.actions$.pipe(
|
|
43
|
+
ofType<ActionFlow>(GeneralActionTypes.ACTION_FLOW),
|
|
44
|
+
// should have at least one action
|
|
45
|
+
filter((af) => af.payload.actionSets.length > 0),
|
|
46
|
+
mergeMap((actionFlow: ActionFlow) => {
|
|
47
|
+
const responseFilters = [];
|
|
48
|
+
|
|
49
|
+
// Set loading subject to true
|
|
50
|
+
if (actionFlow.payload.loading$) {
|
|
51
|
+
actionFlow.payload.loading$.next(true);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Dispatch the primary actions and listen to the responses
|
|
55
|
+
const responseObsArr = actionFlow.payload.actionSets.map((actionSet) => {
|
|
56
|
+
// Set a correlationId
|
|
57
|
+
let correlationId;
|
|
58
|
+
if (!actionSet.action['correlationId']) {
|
|
59
|
+
correlationId = this.db.createId();
|
|
60
|
+
actionSet.action['correlationId'] = correlationId;
|
|
61
|
+
} else {
|
|
62
|
+
correlationId = actionSet.action['correlationId'];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Dispatch the action
|
|
66
|
+
this.store.dispatch(actionSet.action);
|
|
67
|
+
|
|
68
|
+
// If no filter, then responseAction is undefined
|
|
69
|
+
if (!actionSet.responseActionTypes) {
|
|
70
|
+
return of(Object.assign({}, actionSet, {
|
|
71
|
+
result: ActionSetResult.NONE,
|
|
72
|
+
responseAction: undefined,
|
|
73
|
+
}));
|
|
74
|
+
} else {
|
|
75
|
+
let successResponseTypeArr = [];
|
|
76
|
+
if (Array.isArray(actionSet.responseActionTypes.success)) {
|
|
77
|
+
successResponseTypeArr = actionSet.responseActionTypes.success;
|
|
78
|
+
} else if (actionSet.responseActionTypes.success) {
|
|
79
|
+
successResponseTypeArr = [actionSet.responseActionTypes.success];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let failureResponseTypeArr = [];
|
|
83
|
+
if (Array.isArray(actionSet.responseActionTypes.failure)) {
|
|
84
|
+
failureResponseTypeArr = actionSet.responseActionTypes.failure;
|
|
85
|
+
} else if (actionSet.responseActionTypes.failure) {
|
|
86
|
+
failureResponseTypeArr = [actionSet.responseActionTypes.failure];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const filterTypes = [
|
|
90
|
+
...successResponseTypeArr,
|
|
91
|
+
...failureResponseTypeArr,
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
// listen for either success or fail action
|
|
95
|
+
return this.actions$.pipe(
|
|
96
|
+
ofType(...filterTypes),
|
|
97
|
+
this.eh.hasCorrelationId(correlationId),
|
|
98
|
+
first(),
|
|
99
|
+
map((responseAction) => {
|
|
100
|
+
if (successResponseTypeArr.includes(responseAction.type)) {
|
|
101
|
+
return Object.assign({}, actionSet, {
|
|
102
|
+
result: ActionSetResult.SUCCESS,
|
|
103
|
+
responseAction,
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
return Object.assign({}, actionSet, {
|
|
107
|
+
result: ActionSetResult.FAILURE,
|
|
108
|
+
responseAction,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
// Call successActionFn or failActionFn depending on the results
|
|
117
|
+
// If no actions, we should run the success callback
|
|
118
|
+
if (responseObsArr.length === 0) {
|
|
119
|
+
// If there are no actions, just run the success callback
|
|
120
|
+
return actionFlow.payload.successActionFn([]);
|
|
121
|
+
} else {
|
|
122
|
+
return forkJoin(responseObsArr).pipe(
|
|
123
|
+
mergeMap((actionSetResponses) => {
|
|
124
|
+
// Set the loading subject to false
|
|
125
|
+
if (actionFlow.payload.loading$) {
|
|
126
|
+
actionFlow.payload.loading$.next(false);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Figure out if it's a success or failure and emit the things necessary
|
|
130
|
+
if (actionSetResponses.some((r) => r.result === ActionSetResult.FAILURE)) {
|
|
131
|
+
// Emit all the callbacks for individual action sets
|
|
132
|
+
const actionSetCallbacks = actionSetResponses.reduce((obs, asr) => {
|
|
133
|
+
if (!asr.callbacks) {
|
|
134
|
+
return obs;
|
|
135
|
+
} else if (asr.result === ActionSetResult.FAILURE && asr.callbacks.onFailure) {
|
|
136
|
+
return merge(obs, asr.callbacks.onFailure);
|
|
137
|
+
} else if (asr.result === ActionSetResult.SUCCESS && asr.callbacks.onSuccessButOtherFailure) {
|
|
138
|
+
return merge(obs, asr.callbacks.onSuccessButOtherFailure);
|
|
139
|
+
} else {
|
|
140
|
+
return obs;
|
|
141
|
+
}
|
|
142
|
+
}, []);
|
|
143
|
+
|
|
144
|
+
if (actionFlow.payload.failActionFn) {
|
|
145
|
+
return merge(actionSetCallbacks, actionFlow.payload.failActionFn(actionSetResponses));
|
|
146
|
+
} else {
|
|
147
|
+
return actionSetCallbacks;
|
|
148
|
+
}
|
|
149
|
+
} else {
|
|
150
|
+
return actionFlow.payload.successActionFn(actionSetResponses);
|
|
151
|
+
}
|
|
152
|
+
}),
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
}),
|
|
156
|
+
),
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
constructor(
|
|
160
|
+
private actions$: Actions,
|
|
161
|
+
private router: Router,
|
|
162
|
+
private cachedConnections: CachedLoadConnectionsService,
|
|
163
|
+
private eh: EffectsHelpers,
|
|
164
|
+
private db: FirebaseService,
|
|
165
|
+
private store: Store<fromStore.State>,
|
|
166
|
+
) {}
|
|
167
|
+
}
|