added Services of Services, updated store logic for services, updated layout and logic in components with services, added logic for enable/disable service in ServiceCard componemt, added tests for services store and Services of Services, with mock axios

This commit is contained in:
2024-03-07 16:53:52 +03:00
parent 25e3775117
commit 653ec7b372
12 changed files with 569 additions and 282 deletions

View File

@@ -1,14 +1,71 @@
import { addedService, updatedService, removedNewService, deletedService } from "./helpers"
import { isEmpty } from "ramda"
class ServiceOfServices {
constructor(adapterOfServices, store) {
this.adapterOfServices = adapterOfServices
this.store = store
}
async fetchServicesList() {
async fetchServices() {
const services = await this.adapterOfServices.getServices()
this.store.dispatch('services/saveServices', services)
return services
}
async addNewServiceLayout() {
const newService = {"port": "", "name": "", id: -1}
this.store.dispatch('services/addedNewServiceLayout', newService)
}
async createNewService(newService) {
this.store.commit('services/setIsSaveData', false)
this.store.commit('services/setSelectedService', null)
const createdService = await this.adapterOfServices.createService(newService)
const updatedServices = addedService(createdService, this.store.getters['services/services'])
this.store.dispatch('services/createNewService', updatedServices)
return createdService
}
async selectService(service) {
this.store.dispatch('services/changeSelectedService', service)
}
async cancelSelectedService(id) {
const services = this.store.getters['services/services']
const updatedServices = id === -1 ? removedNewService(services) : null
this.store.dispatch('services/cancelSelectedService', updatedServices)
}
async editSelectedService(params) {
const selectedService = this.store.getters['services/selectedService']
// console.log('selectedService', selectedService)
selectedService[params.key] = params.value
this.store.dispatch('services/editSelectedService', selectedService)
}
async isSaveServices(event) {
this.store.dispatch('services/changeIsSaveData', event)
}
async saveService(editService) {
this.store.commit('services/setIsSaveData', false)
this.store.commit('services/setSelectedService', null)
const editedService = await this.adapterOfServices.updateService(editService)
const services = this.store.getters['services/services']
const updatedServices = !isEmpty(editedService) ? updatedService(editedService, services) : services
// console.log('updatedServices', updatedServices)
this.store.dispatch('services/saveService', updatedServices)
return editedService
}
async removeService(id) {
const deletedSiteId = await this.adapterOfServices.deleteService(id)
const services = this.store.getters['services/services']
const updatedServices = deletedSiteId ? deletedService(deletedSiteId, services) : null
this.store.dispatch('services/saveService', updatedServices)
}
}
export default ServiceOfServices

View File

@@ -0,0 +1,32 @@
const addedService = (addedSite, sites) => {
const sitesWithoutNewSite = removedNewService(sites)
return [addedSite,...sitesWithoutNewSite]
}
const updatedService = (updatedSite, sites) => {
if (updatedSite.id) {
const editIdx = sites.findIndex(service => service.id === updatedSite.id)
const beforeEdit = sites.slice(0, editIdx)
const afterEdit = sites.slice(editIdx + 1)
const withEdit = [...beforeEdit, updatedSite]
return [...withEdit, ...afterEdit]
}
}
const removedNewService = (sites) => {
if (sites.length > 0) {
const firstSite = sites[0]
const isNewSite = firstSite.id === -1
sites = isNewSite ? sites.slice(1) : sites
return sites
}
}
const deletedService = (deletedSiteId, sites) => {
if (deletedSiteId) {
const deleteIdx = sites.findIndex(service => service.id === deletedSiteId)
return sites.slice(0, deleteIdx).concat(sites.slice(deleteIdx + 1))
}
}
export { addedService, updatedService, removedNewService, deletedService }