import { createUserAction as createAUserAction , destroyUserAction , selectUserActions , updateUserAction as updateAUserAction , useDispatch , useSelector } from '@store' ; import { ACTION_NAME , ACTION_STATE , ActionTemplate , ExtendedUserAction , TUuid , UserAction } from '@types' ; import { generateUUID } from '@utils' ; export interface IUserActionContext { userActions : UserAction [ ] ; createUserAction ( actionTemplate ) : void ; updateUserAction ( uuid , userAction ) : void ; deleteUserAction ( userAction ) : void ; findUserAction ( actionName ) : ExtendedUserAction | undefined ; } function useUserActions ( ) { const dispatch = useDispatch ( ) ; const userActions = useSelector ( selectUserActions ) ; const createUserAction = ( actionTemplate ) => { const userAction = { name : actionTemplate . name , state : ACTION_STATE . NEW } ; dispatch ( createAUserAction ( { ... userAction , uuid : generateUUID ( ) } ) ) ; } ; const updateUserAction = ( uuid , userAction ) => dispatch ( updateAUserAction ( { ... userAction , uuid } ) ) ; const deleteUserAction = ( userAction ) => { dispatch ( destroyUserAction ( userAction . uuid ) ) ; } ; const findUserAction = ( actionName ) : ExtendedUserAction | undefined => userActions . find ( ( a ) => a . name === actionName ) ; return { userActions , createUserAction , updateUserAction , deleteUserAction , findUserAction } ; } export default useUserActions ;