Skip to content

Instantly share code, notes, and snippets.

View lydemann's full-sized avatar

Christian Lüdemann lydemann

View GitHub Profile
@lydemann
lydemann / course.component.ts
Created May 14, 2021 11:55
course.component.ts
ngOnDestroy(): void {
this.renderer.removeChild(this.elementRef.nativeElement, this.styleElement);
this.destroy$.next();
this.destroy$.complete();
}
ngOnInit() {
switchMap((courseId) =>
this.courseFacade.getCourseCustomStyling(courseId)
)
@lydemann
lydemann / course-facade.service.ts
Last active March 23, 2021 16:38
course-facade.service.ts
editCourseSubmitted(editedCourse: Course) {
return this.apollo.mutate<Course>({
mutation: EDIT_COURSE_MUTATION,
variables: {
id: editedCourse.id,
name: editedCourse.name,
description: editedCourse.description,
} as Course,
});
}
@lydemann
lydemann / graphql-helpers.ts
Last active March 23, 2021 16:28
graphql-helpers.ts
export function removeFromCache<ReadQueryResponseT>(
toRemove: EntityObject,
readQuery: DocumentNode,
cache: ApolloCache<any>,
entityName: keyof ReadQueryResponseT
) {
const existingEntities = cache.readQuery<
Record<keyof ReadQueryResponseT, EntityObject[]>
>({
query: readQuery,
@lydemann
lydemann / router.selectors.ts
Created March 16, 2021 08:52
router.selectors.ts
const getFlattenedParentRouteParams = (route: ActivatedRouteSnapshot) => {
if (!route.parent) {
return route.params;
}
const parentRouteParams = getFlattenedParentRouteParams(route.parent);
return {
...route.params,
...parentRouteParams,
};
@lydemann
lydemann / prevent-ghost-clicks.directive.ts
Created March 15, 2021 10:35
prevent-ghost-clicks.directive.ts
import { Directive, ElementRef, HostListener, OnInit } from '@angular/core';
import 'hammerjs';
/*
Preventing clicks after a HammerJS press (long press) event.
Inspired from HammerJS ghost click helper: https://gist.github.com/jtangelder/361052976f044200ea17
*/
@Directive({
selector: '[preventGhostClicks]',
})
@lydemann
lydemann / graphql-helpers.ts
Created March 7, 2021 11:01
graphql-helpers.ts
export function createInCache<ReadQueryResponseT>(
toCreate: EntityObject,
readQuery: DocumentNode,
cache: ApolloCache<any>,
entityName: keyof ReadQueryResponseT
) {
const existingEntities = cache.readQuery<
Record<keyof ReadQueryResponseT, EntityObject[]>
>({
query: readQuery,
@lydemann
lydemann / graphql-helpers.ts
Created March 7, 2021 11:00
graphql-helpers.ts
export interface EntityObject {
id: string;
}
@lydemann
lydemann / category-transaction-list.component.ts
Created March 2, 2021 11:57
category-transaction-list.component.ts
/*
For avoiding clicks on mouse up after HammerJS long press.
This event is triggered right before (click) to ensure we will not trigger click on long press.
*/
preventClickAfterLongPress(event: MouseEvent) {
const element = event.target as HTMLElement;
// needs to add class directly so it is applied before click event
element.classList.add('prevent-pointer-events');
setTimeout(() => {
element.classList.remove('prevent-pointer-events');
@lydemann
lydemann / course-content.component.ts
Created February 23, 2021 17:12
course-content.component.ts
private getTrustedVideoUrl(url: string): SafeResourceUrl {
// Url domains are whitelisted using CSP header
// Content-Security-Policy: frame-src <source> <source>;
const sanitizedUrl = this.sanitizer.sanitize(
SecurityContext.URL,
url
);
return this.sanitizer.bypassSecurityTrustResourceUrl(sanitizedUrl);
}
@lydemann
lydemann / course-content.component.ts
Created February 22, 2021 17:21
course-content.component.ts
private getTrustedVideoUrl(url: string): SafeResourceUrl {
// Url domains are whitelisted using CSP header
// Content-Security-Policy: frame-src <source> <source>;
const sanitizedUrl = this.sanitizer.sanitize(
SecurityContext.RESOURCE_URL,
url
);
return this.sanitizer.bypassSecurityTrustResourceUrl(sanitizedUrl);
}