roles.ts

state management using pinia
mail@pastecode.io avatar
unknown
typescript
7 months ago
6.1 kB
4
Indexable
Never
export const useRolesStore = defineStore({
    id: 'roles-store',
    state: () => {
        return {
            listLoading : false,
            loading : false,
            // data
            list: [] as RoleData[],
            detail : {} as RoleData,
            paginationList : null,
            // alert message
            errorMessage : null as string | null,
            successMessage : null as string | null
        }
    },
    actions: {
        /**
         * API Process handle on state management
         * For standard CRUD when using default component
         * Please name the standard function
         * This standard function name used on Tables Component
         * Used for simple CRUD Needed
         */

        // standard name function @fetchIndex(parameter query string)
        async fetchIndex(params : string) {
            this.listLoading = true
            try {
                let res = await useAxios().get('/managements/roles?'+params)
                this.list = res.data.data
                this.paginationList = res.data.pagination

                this.listLoading = false
                return <IRequestState> {
                    status : res.data.status,
                    code : 200,
                    data : res.data.data
                }
            } catch (error : any) {
                this.listLoading = false
                this.errorMessage = error.response.message
                return <IRequestState> {
                    status : false,
                    code : error.response.status,
                    error : error
                }
            }
        },
        // standard name function @fetchDetail
        async fetchDetail(id : string) {
            this.listLoading = true
            try {
                let res = await useAxios().get('/managements/roles/' + id)
                this.detail = res.data.data

                this.listLoading = false
                return <IRequestState> {
                    status : res.data.status,
                    code : 200,
                    data : res.data.data
                }
            } catch (error : any) {
                this.listLoading = false
                this.errorMessage = error.response.message
                return <IRequestState> {
                    status : false,
                    code : error.response.status,
                    error : error
                }
            }
        },
        // standard name function @sendCreateData
        async sendCreateData(data : IRolesFormData) {
            this.loading = true
            try {
                let res = await useAxios().post('/managements/roles' , data)
                this.loading = false
                this.successMessage = res.data.message
                return <IRequestState> {
                    status : res.data.status,
                    code : 200,
                    data : res.data.data
                }
            } catch (error : any) {
                this.loading = false
                this.errorMessage = error.response.message
                return <IRequestState> {
                    status : false,
                    code : error.response.status,
                    error : error
                }
                
            }
        },
        // standard name function @sendEditData
        async sendEditData(data : IRolesFormData , id : string) {
            this.loading = true
            try {
                let res = await useAxios().put('/managements/roles/'+id+'/update' , data)

                this.loading = false
                this.successMessage = res.data.message
                return <IRequestState> {
                    status : res.data.status,
                    code : 200,
                    data : res.data.data
                }
            } catch (error : any) {
                this.loading = false
                this.errorMessage = error.response.message
                return <IRequestState> {
                    status : false,
                    code : error.response.status,
                    error : error
                }
            }
        },
        // standard name function @sendDeleteData
        async sendDeleteData(ids : {}) {
            this.loading = true
            try {
                let res = await useAxios().patch('/managements/roles/multi-delete' , ids)

                this.loading = false
                this.successMessage = res.data.message
                return <IRequestState> {
                    status : res.data.status,
                    code : 200,
                    data : res.data.data
                }
            } catch (error : any) {
                this.loading = false
                this.errorMessage = error.response.message
                return <IRequestState> {
                    status : false,
                    code : error.response.status,
                    error : error
                }
            }
        },

        // local logic
        setSuccessMessage(value : string) {
            this.successMessage = value
        },
        setErrorMessage(value : string) {
            this.errorMessage = value
        }
        
    },
    getters: {
        // logic Helpers
        listLoadInd : state => state.listLoading,
        saveLoadInd : state => state.loading,
        getError: state => state.errorMessage,
        getSuccess: state => state.successMessage,

        // api data
        // standard name getters @getList
        getList: state => state.list,
        // standard name getters @getPagination
        getPagination: state => state.paginationList,
        // standard name getters @getDetail
        getDetail: state => state.detail
        
    }
})

// interfaces
interface RoleData {
    _id: any
    name : string
    status : boolean
    createdAt : string
    updatedAt : string
}