import { ActionContext , Module } from 'vuex' ; import { ITag , ITagsState , IRootState , } from '../Interface' ; import { createTag , deleteTag , getTags , updateTag } from '../api/tags' ; import Vue from 'vue' ; const module = { namespaced : true , state : { tags : { } , isLoading : false , fetchedAll : false , fetchedUsageCount : false , } , mutations : { setLoading : ( state , isLoading ) => { state . isLoading = isLoading ; } , setAllTags : ( state , tags ) => { state . tags = tags . reduce ( ( accu , tag ) => { accu [ tag . id ] = tag ; return accu ; } , { } ) ; state . fetchedAll = true ; } , upsertTags ( state , tags ) { tags . forEach ( ( tag ) => { const tagId = tag . id ; const currentTag = state . tags [ tagId ] ; if ( currentTag ) { const newTag = { ... currentTag , ... tag , } ; Vue . set ( state . tags , tagId , newTag ) ; } else { Vue . set ( state . tags , tagId , tag ) ; } } ) ; } , deleteTag ( state , id ) { Vue . delete ( state . tags , id ) ; } , } , getters : { allTags ( state ) { return Object . values ( state . tags ) . sort ( ( a , b ) => a . name . localeCompare ( b . name ) ) ; } , isLoading : ( state ) : boolean => { return state . isLoading ; } , hasTags : ( state ) : boolean => { return Object . keys ( state . tags ) . length > 0 ; } , getTagById : ( state ) => { return ( id ) => state . tags [ id ] ; } , } , actions : { fetchAll : async ( context , params ? ) => { const { force = false , withUsageCount = false } = params || { } ; if ( ! force && context . state . fetchedAll && context . state . fetchedUsageCount === withUsageCount ) { return context . state . tags ; } context . commit ( 'setLoading' , true ) ; const tags = await getTags ( context . rootGetters . getRestApiContext , Boolean ( withUsageCount ) ) ; context . commit ( 'setAllTags' , tags ) ; context . commit ( 'setLoading' , false ) ; return tags ; } , create : async ( context , name ) => { const tag = await createTag ( context . rootGetters . getRestApiContext , { name } ) ; context . commit ( 'upsertTags' , [ tag ] ) ; return tag ; } , rename : async ( context , { id , name } ) => { const tag = await updateTag ( context . rootGetters . getRestApiContext , id , { name } ) ; context . commit ( 'upsertTags' , [ tag ] ) ; return tag ; } , delete : async ( context , id ) => { const deleted = await deleteTag ( context . rootGetters . getRestApiContext , id ) ; if ( deleted ) { context . commit ( 'deleteTag' , id ) ; context . commit ( 'removeWorkflowTagId' , id , { root : true } ) ; } return deleted ; } , } , } ; export default module ;