Skip to content

Instantly share code, notes, and snippets.

View rakia's full-sized avatar

Rakia Ben Sassi rakia

View GitHub Profile
@rakia
rakia / esc-fieldsets-with-details-name-already-exists.component.html
Last active July 21, 2024 18:48
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
<app-ecs-fieldset
[entity]="ecsFieldset"
[fields]="(ecsFields$ | async) || []"
[nameAlreadyExists]="nameAlreadyExists$ | async"
(checkIfNameExists)="checkIfNameExists($event)"
(cancelEdit)="onCancelEdit()"
>
</app-ecs-fieldset>
@rakia
rakia / escfield-name-already-exists.component.ts
Created July 21, 2024 18:44
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-create-custom-field',
templateUrl: './create-custom-field.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CreateCustomFieldComponent implements OnInit {
private readonly destroyRef = inject(DestroyRef);
@Input() nameAlreadyExists: boolean | null = false;
@Output() checkIfNameExists = new EventEmitter<string>();
@rakia
rakia / name-already-exists.component.ts
Created July 21, 2024 18:40
Angular: Logic to show real-time feedback to users when they type a name that already exists in the system
@Component({
selector: 'app-ecs-fieldsets-with-details',
templateUrl: './ecs-fieldsets-with-details.component.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class EcsFieldsetsWithDetailsComponent implements OnChanges {
private storeService = inject(EcsFieldsStoreService);
nameAlreadyExists$ = new BehaviorSubject<boolean>(false);
@rakia
rakia / show-warning.html
Created July 21, 2024 18:36
Show real-time feedback to users when they type a name that already exists in the system
<!-- Show warning: Name already exists -->
<ng-container *ngIf="nameAlreadyExists">
<div class="mb-4 text-red-600">
{{ "WARNING.DUPLICATE_NAME" | transloco }}
</div>
</ng-container>
@rakia
rakia / redirect-to.ts
Created May 23, 2024 07:57
Angular Routing: redirectTo as function
const routes: Routes = [
{ path: "dashboard", component: DashboardComponent },
{
path: "profiles",
redirectTo: ({ queryParams }) => {
const errorHandler = inject(ErrorHandler);
const userId = queryParams['userId'];
if (userId) {
// return the appropriate redirect path based on the userId query-parameter
return `/profiles/${userId}`;
@rakia
rakia / per-route-renderMode.ts
Created May 22, 2024 21:01
Angular routing: per-route renderMode
const routes: Routes = [
{
path: 'route1',
component: Route1Component,
renderMode: 'stategy1' // renderMode option 1
},
{
path: 'route2',
component: Route2Component,
renderMode: 'strategy2' // renderMode option 2
@rakia
rakia / redirect-command.ts
Created May 22, 2024 20:47
Angular Routing: RedirectCommand
const routes: Routes = [
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [
() => {
const router: Router = inject(Router);
const urlTree: UrlTree = router.parseUrl('/error');
return new RedirectCommand(urlTree, { skipLocationChange: true });
},
@rakia
rakia / control-state-change-events.ts
Created May 22, 2024 20:25
Angular: Unified Control State Change Events
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
export class ProfileFormComponent {
private fb = inject(FormBuilder);
profileForm: FormGroup;
ngOnInit() {
this.profileForm = this.fb.group({
firstName: ['', [Validators.required, Validators.minLength(2)]],
@rakia
rakia / default-content.ts
Created May 22, 2024 20:12
Angular: default content
@Component({
selector: 'main-layout',
template: `
<ng-content select="header">Hello again</ng-content>
<ng-content><p>Default content if none is provided.</p></ng-content>
<ng-content select="footer"></ng-content>
`
})
export class MainLayoutComponent {}
@rakia
rakia / model-input.ts
Created May 22, 2024 19:59
Angular: Model input example
import { Component, model, input } from '@angular/core';
@Component({
selector: 'app-user',
template: '<button (click)="upgradeSubscription()">Upgrade Subscription</button>',
})
export class UserComponent {
// a standard input, it's read-only
isSubscribed = input(true);