import { getTimestamp } from 'common/utils' ; import { Category , CategoryActions , Action } from 'common/types' ; import { getCategoryEntity } from 'common/assets' ; export const createCategoryActions = ( state , updateState ) => ( { add : ( category ) => { const newCategory = getCategoryEntity ( category ) ; updateState ( { categories : [ ... state . categories , newCategory ] } ) ; return newCategory ; } , remove : ( categoryId ) => { const updCategories = state . categories . map ( ( category ) => { const updCategory = { ... category } ; if ( category . id === categoryId || category . parentId === categoryId ) { updCategory . isDeleted = true ; updCategory . lastModifiedTime = getTimestamp ( ) ; } return updCategory ; } ) ; updateState ( { categories : updCategories } ) ; } , update : ( category ) => { const updCategories = state . categories . map ( ( entity ) => { let updEntity = { ... entity } ; if ( entity . id === category . id ) updEntity = getCategoryEntity ( category ) ; return updEntity ; } ) ; updateState ( { categories : updCategories } ) ; } , select : ( categoryId ) => updateState ( { currentCategoryId : categoryId } ) , } ) ;