import { Injectable } from '@angular/core' ; import { Action , Selector , State , StateContext } from '@ngxs/store' ; import { Server } from '../../utils/config' ; import { Todo } from 'src/app/models/Todo' ; import { Api } from 'src/app/helpers/api' ; import { GlobalActions } from '../global' ; /** State Model */ export class TodoStateModel { todos : Array < any > ; } export namespace Todos { /** Actions */ export class Fetch { static readonly type = '[Todo] FetchTodos' ; } export class Add { static readonly type = '[Todo] AddTodo' ; constructor ( public payload ) { } } export class Update { static readonly type = '[Todo] UpdateTodo' ; constructor ( public payload ) { } } export class Delete { static readonly type = '[Todo] DeleteTodo' ; constructor ( public payload ) { } } } @ State < TodoStateModel > ( { name : 'todo' , defaults : { todos : [ ] , } , } ) @ Injectable ( ) export class TodoState { @ Selector ( ) static getTodos ( state ) { return state . todos ; } @ Action ( Todos . Fetch ) async fetchTodos ( { setState , dispatch } , action ) { try { let todos = ( await Api . provider ( ) . database . listDocuments ( Server . collectionID ) ) as Record < string , any > ; setState ( { todos : todos . documents , } ) ; } catch ( e ) { console . log ( 'Failed to fetch todos' ) ; dispatch ( new GlobalActions . setAlert ( { message : e . message , show : true , color : 'red' , } ) ) ; } } @ Action ( Todos . Add ) async addTodo ( { patchState , getState , dispatch } , action ) { try { let { data , read , write } = action . payload ; let todo = await Api . provider ( ) . database . createDocument ( Server . collectionID , data , read , write ) ; var todos = getState ( ) . todos ; patchState ( { todos : [ ... todos , todo ] , } ) ; } catch ( e ) { console . log ( 'Failed to add todo' ) ; dispatch ( new GlobalActions . setAlert ( { message : e . message , show : true , color : 'red' , } ) ) ; } } @ Action ( Todos . Update ) async updateTodo ( { patchState , getState , dispatch } , action ) { let { documentId , data , read , write } = action . payload ; try { let updatedTodo = ( await Api . provider ( ) . database . updateDocument ( Server . collectionID , documentId , data , read , write ) ) as any ; let todoList = [ ... getState ( ) . todos ] ; const index = todoList . findIndex ( ( todo ) => todo [ '$id' ] === updatedTodo [ '$id' ] ) ; if ( index !== - 1 ) { todoList [ index ] = updatedTodo ; patchState ( { todos : todoList , } ) ; } } catch ( e ) { console . log ( 'Failed to update todo' ) ; dispatch ( new GlobalActions . setAlert ( { message : e . message , show : true , color : 'red' , } ) ) ; } } @ Action ( Todos . Delete ) async deleteTodo ( { patchState , getState , dispatch } , action ) { let { documentId } = action . payload ; try { await Api . provider ( ) . database . deleteDocument ( Server . collectionID , documentId ) ; let todos = getState ( ) . todos ; todos = todos . filter ( ( todo ) => todo [ '$id' ] !== documentId ) ; patchState ( { todos , } ) ; } catch ( e ) { console . log ( 'Failed to delete todo' ) ; dispatch ( new GlobalActions . setAlert ( { message : e . message , show : true , color : 'red' , } ) ) ; } } }