Last active
April 13, 2021 00:35
-
-
Save dcagnetta/8bcccaaa9cee1bd0f19603ea5373dc3e to your computer and use it in GitHub Desktop.
ANGULAR STREAMLINE PROVIDERS
This file contains hidden or 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
// token to access a stream with the organisation information we need | |
// this is what we inject e.g. @Inject(ORG_INFO) readonly orgInfo$: Observable<string> | |
export const ORG_INFO = new InjectionToken<Observable<string>>( | |
'Organisation name information' | |
); | |
// This is what we add to `providers` array in module or component | |
export const ORGANISATION_PROVIDERS: Provider[] = [ | |
{ | |
provide: ORG_INFO, | |
deps: [ActivatedRoute, OrgService], | |
useFactory: orgFactory, | |
}, | |
]; | |
/** | |
* @summary extracts org Id from URL and then returns the organisation name | |
* */ | |
export function orgFactory( | |
{ params }: ActivatedRoute, | |
orgService: OrgService): Observable<string> { | |
return params.pipe( | |
switchMap(({orgId}) => { | |
return orgService.getOrganisation(orgId) | |
.pipe( | |
map((organisation: Organisation) => organisation.name) | |
); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment