User Tools

Site Tools


Angular NgRx store

models/user.model.ts

user.model.ts
export interface User {
    auth? : boolean
    id? : number
    name? : string
    password? : string
}

store/reducers/auth.reducer.ts

auth.reducer.ts
import { Actions, userLogin, userLogout } from '../actions/auth.actions'
import { User } from '../../models/user.model'
 
const initialState: User = {
  auth: false
}
 
export function authReducer(state : User = initialState, action : Actions) {
 
    switch(action.type) {
        case userLogin:
            state.auth = true
            return state
        case userLogout:
            state.auth = false
            return state
        default:
            return state
    }
}

store/actions/auth.actions.ts

auth.actions.ts
import { Injectable } from '@angular/core'
import { Action } from '@ngrx/store'
 
import { User } from '../../models/user.model'
 
export const userLogin = 'userLogin'
export const userLogout = 'userLogout'
 
export class setLogin implements Action {
    readonly type = userLogin
    constructor() {}
}
 
export class setLogout implements Action {
    readonly type = userLogout
    constructor() {}
}
 
export type Actions = setLogin | setLogout

store/app.state.ts

app.state.ts
import { User } from '../models/user.model'
 
export interface AppState {
    readonly user : User
}