Giter Club home page Giter Club logo

angular16-projectdemo's Introduction

Angular16-DemoProject

Generated with Angular CLI version 16.1.1. This project uses the newly introduced state management called Signals that granularly tracks how and where your state is used throughout an application.

How Signals work

A signal is a wrapper around a value that can notify interested consumers when that value changes. Signals can contain any value, from simple primitives to complex data structures.

Writable signals

Writable signals provide an API for updating their values directly. You create writable signals by calling the signal function with the signal's initial value:

import {Supervisor} from "./supervisor.model";

getUserFromLocal(): [Partial<Supervisor>, boolean]
{
  const authData = localStorage.getItem('auth');

  if (authData) {
    try {
      const user = JSON.parse(authData);
      return user.role === "Professor" ? [user as Professor, true] : [user as Headmaster, true]
    } catch (error) {
      console.error('Error parsing user data from local storage:', error);
    }
  }
  return [{}, false];
}

authenticated : WritableSignal<{ value: Partial<Supervisor>, state: boolean }> = signal({
  value: this.getUserFromLocal()[0],
  state: this.getUserFromLocal()[1]
})

To change the value of a writable signal, you can either .set() it directly:

this.authenticated.set({value: headmasterArray[index], state: true});
this.authenticated.set({value: professorArray[index], state: true});

Computed signals

A computed signal derives its value from other signals. Define one using computed and specifying a derivation function:

getAuthenticatedUser(): Signal<{value: any, state: boolean}>
{
  return computed(() => {
    const data: any  = this.authenticated().value
    return {
      value: data?.user === undefined ? data as Professor | Headmaster | Partial<Supervisor> : data.user,
      state: this.authenticated().state
    }
  })
}

Effects

Signals are useful because they can notify interested consumers when they change. An effect is an operation that runs whenever one or more signal values change. You can create an effect with the effect function:

userInfo: Signal<{value: any, state: boolean}> = this.backendService.getAuthenticatedUser();
supervisorForm = this.fb.group({
  id: ['To determine'],
  code : ['', [Validators.required, Validators.pattern(/^([a-zA-Z]{4})(\d{2})(\d{2})(\d{2})(\d{2})$/)]],
  email : [''],
  firstname : [''],
  lastname : [''],
  studentCode: [''],
});

constructor() 
{
  effect(() => {
    this.supervisorForm.get("code")?.setValue(this.userInfo().state ? this.userInfo().value.code : '');
    this.supervisorForm.get("email")?.setValue(this.userInfo().state ? this.userInfo().value.email : '');
    this.supervisorForm.get("firstname")?.setValue(this.userInfo().state ? this.userInfo().value.firstname : '');
    this.supervisorForm.get("lastname")?.setValue(this.userInfo().state ? this.userInfo().value.lastname : '');
  })
}

Conversions

toSignal() can be used to convert an observable to a signal.

students: Signal<Intern[] | undefined> = toSignal(this.studentInfoForm.valueChanges.pipe(
  switchMap((value) => this.backendService.getStudents(<string>value.permanentCode?.toUpperCase()))
))

toObservable() is used to do the reverse work.

const authService = inject(BackendService);
const router = inject(Router);
const dialog = inject(MatDialog);

toObservable(authService.authenticated).subscribe(val => {
  if (!val.state)
    router.navigate(['/']).then(() => displayAuthModal(router, dialog))
});

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.