From 07a5e4e92400ee8bd43b60783f1186f8e15381f3 Mon Sep 17 00:00:00 2001 From: Aditya Mitra <55396651+aditya-mitra@users.noreply.github.com> Date: Fri, 21 Jul 2023 04:31:44 +0530 Subject: [PATCH] refactor(client-core)(location service): remove action receptor usage (#8318) Co-authored-by: HexaField --- .../src/admin/services/LocationService.ts | 127 +++--------------- .../client-core/src/systems/AdminSystem.tsx | 11 -- 2 files changed, 22 insertions(+), 116 deletions(-) diff --git a/packages/client-core/src/admin/services/LocationService.ts b/packages/client-core/src/admin/services/LocationService.ts index e317ec1066..982136585d 100644 --- a/packages/client-core/src/admin/services/LocationService.ts +++ b/packages/client-core/src/admin/services/LocationService.ts @@ -28,15 +28,13 @@ import { Paginated } from '@feathersjs/feathers' import { Location } from '@etherealengine/common/src/interfaces/Location' import { LocationType } from '@etherealengine/common/src/interfaces/LocationType' import multiLogger from '@etherealengine/common/src/logger' -import { matches, Validator } from '@etherealengine/engine/src/common/functions/MatchesUtils' -import { defineAction, defineState, dispatchAction, getMutableState } from '@etherealengine/hyperflux' +import { Engine } from '@etherealengine/engine/src/ecs/classes/Engine' +import { defineState, getMutableState } from '@etherealengine/hyperflux' -import { API } from '../../API' import { NotificationService } from '../../common/services/NotificationService' const logger = multiLogger.child({ component: 'client-core:LocationService' }) -//State export const LOCATION_PAGE_LIMIT = 100 export const AdminLocationState = defineState({ @@ -55,72 +53,27 @@ export const AdminLocationState = defineState({ }) }) -export const locationsRetrievedReceptor = (action: typeof AdminLocationActions.locationsRetrieved.matches._TYPE) => { - const state = getMutableState(AdminLocationState) - return state.merge({ - locations: action.locations.data, - skip: action.locations.skip, - limit: action.locations.limit, - total: action.locations.total, - retrieving: false, - fetched: true, - updateNeeded: false, - lastFetched: Date.now() - }) -} - -export const locationCreatedReceptor = (action: typeof AdminLocationActions.locationCreated.matches._TYPE) => { - const state = getMutableState(AdminLocationState) - return state.merge({ updateNeeded: true, created: true }) -} - -export const locationPatchedReceptor = (action: typeof AdminLocationActions.locationPatched.matches._TYPE) => { - const state = getMutableState(AdminLocationState) - return state.merge({ updateNeeded: true }) -} - -export const locationRemovedReceptor = (action: typeof AdminLocationActions.locationRemoved.matches._TYPE) => { - const state = getMutableState(AdminLocationState) - return state.merge({ updateNeeded: true }) -} - -export const locationTypesRetrievedReceptor = ( - action: typeof AdminLocationActions.locationTypesRetrieved.matches._TYPE -) => { - const state = getMutableState(AdminLocationState) - return state.merge({ locationTypes: action.locationTypes.data, updateNeeded: false }) -} - -export const AdminLocationReceptors = { - locationsRetrievedReceptor, - locationCreatedReceptor, - locationPatchedReceptor, - locationRemovedReceptor, - locationTypesRetrievedReceptor -} - -//Service export const AdminLocationService = { fetchLocationTypes: async () => { - const locationTypes = (await API.instance.client.service('location-type').find()) as Paginated - dispatchAction(AdminLocationActions.locationTypesRetrieved({ locationTypes })) + await Engine.instance.api.service('location-type').find() + getMutableState(AdminLocationState).merge({ updateNeeded: true }) }, patchLocation: async (id: string, location: any) => { try { - const result = await API.instance.client.service('location').patch(id, location) - dispatchAction(AdminLocationActions.locationPatched({ location: result })) + await Engine.instance.api.service('location').patch(id, location) + getMutableState(AdminLocationState).merge({ updateNeeded: true }) } catch (err) { NotificationService.dispatchNotify(err.message, { variant: 'error' }) } }, removeLocation: async (id: string) => { - const result = await API.instance.client.service('location').remove(id) - dispatchAction(AdminLocationActions.locationRemoved({ location: result })) + await Engine.instance.api.service('location').remove(id) + getMutableState(AdminLocationState).merge({ updateNeeded: true }) }, createLocation: async (location: any) => { try { - const result = await API.instance.client.service('location').create(location) - dispatchAction(AdminLocationActions.locationCreated({ location: result })) + await Engine.instance.api.service('location').create(location) + getMutableState(AdminLocationState).merge({ updateNeeded: true, created: true }) } catch (err) { NotificationService.dispatchNotify(err.message, { variant: 'error' }) } @@ -142,7 +95,7 @@ export const AdminLocationService = { } } - const locations = (await API.instance.client.service('location').find({ + const locations = (await Engine.instance.api.service('location').find({ query: { $sort: { ...sortData @@ -157,14 +110,23 @@ export const AdminLocationService = { locations.data.forEach((locationData) => { if (locationData.location_setting) locationData.locationSetting = locationData.location_setting }) - dispatchAction(AdminLocationActions.locationsRetrieved({ locations })) + getMutableState(AdminLocationState).merge({ + locations: locations.data, + skip: locations.skip, + limit: locations.limit, + total: locations.total, + retrieving: false, + fetched: true, + updateNeeded: false, + lastFetched: Date.now() + }) } catch (error) { logger.error(error) } }, searchAdminLocations: async (value, orderBy = 'asc') => { try { - const locations = (await API.instance.client.service('location').find({ + const locations = (await Engine.instance.api.service('location').find({ query: { search: value, $sort: { @@ -183,48 +145,3 @@ export const AdminLocationService = { } } } - -//Action -export class AdminLocationActions { - static locationsRetrieved = defineAction({ - type: 'ee.client.AdminLocation.ADMIN_LOCATIONS_RETRIEVED' as const, - locations: matches.object as Validator> - }) - - // static locationRetrieved = defineAction({ - // type: 'ee.client.AdminLocation.ADMIN_LOCATION_RETRIEVED' as const, - // location: matches.object as Validator - // }) - - static locationCreated = defineAction({ - type: 'ee.client.AdminLocation.ADMIN_LOCATION_CREATED' as const, - location: matches.object as Validator - }) - - static locationPatched = defineAction({ - type: 'ee.client.AdminLocation.ADMIN_LOCATION_PATCHED' as const, - location: matches.object as Validator - }) - - static locationRemoved = defineAction({ - type: 'ee.client.AdminLocation.ADMIN_LOCATION_REMOVED' as const, - location: matches.object as Validator - }) - - // static locationBanCreated = defineAction({ - // type: 'ee.client.AdminLocation.ADMIN_LOCATION_BAN_CREATED' as const - // }) - - // static fetchingCurrentLocation = defineAction({ - // type: 'ee.client.AdminLocation.ADMIN_FETCH_CURRENT_LOCATION' as const - // }) - - // static locationNotFound = defineAction({ - // type: 'ee.client.AdminLocation.ADMIN_LOCATION_NOT_FOUND' as const - // }) - - static locationTypesRetrieved = defineAction({ - type: 'ee.client.AdminLocation.ADMIN_LOCATION_TYPES_RETRIEVED' as const, - locationTypes: matches.object as Validator> - }) -} diff --git a/packages/client-core/src/systems/AdminSystem.tsx b/packages/client-core/src/systems/AdminSystem.tsx index b1979f4d63..31ccdca099 100644 --- a/packages/client-core/src/systems/AdminSystem.tsx +++ b/packages/client-core/src/systems/AdminSystem.tsx @@ -27,7 +27,6 @@ import { defineSystem } from '@etherealengine/engine/src/ecs/functions/SystemFun import { defineActionQueue } from '@etherealengine/hyperflux' import { AdminActiveRouteActions, AdminActiveRouteReceptors } from '../admin/services/ActiveRouteService' -import { AdminLocationActions, AdminLocationReceptors } from '../admin/services/LocationService' import { AdminResourceActions, AdminResourceReceptors } from '../admin/services/ResourceService' import { AdminSceneActions, AdminSceneReceptors } from '../admin/services/SceneService' import { AdminServerLogsActions, AdminServerLogsReceptors } from '../admin/services/ServerLogsService' @@ -76,11 +75,6 @@ const resourceNeedsUpdateQueue = defineActionQueue(AdminResourceActions.resource const resourcesResetFilterQueue = defineActionQueue(AdminResourceActions.resourcesResetFilter.matches) const scenesFetchedQueue = defineActionQueue(AdminSceneActions.scenesFetched.matches) const sceneFetchedQueue = defineActionQueue(AdminSceneActions.sceneFetched.matches) -const locationsRetrievedQueue = defineActionQueue(AdminLocationActions.locationsRetrieved.matches) -const locationCreatedQueue = defineActionQueue(AdminLocationActions.locationCreated.matches) -const locationPatchedQueue = defineActionQueue(AdminLocationActions.locationPatched.matches) -const locationRemovedQueue = defineActionQueue(AdminLocationActions.locationRemoved.matches) -const locationTypesRetrievedQueue = defineActionQueue(AdminLocationActions.locationTypesRetrieved.matches) const activeRoutesRetrievedQueue = defineActionQueue(AdminActiveRouteActions.activeRoutesRetrieved.matches) const fetchedInstanceServerQueue = defineActionQueue(InstanceServerSettingActions.fetchedInstanceServer.matches) const chargebeeSettingRetrievedQueue = defineActionQueue(AdminChargebeeSettingActions.chargebeeSettingRetrieved.matches) @@ -116,11 +110,6 @@ const execute = () => { for (const action of resourcesResetFilterQueue()) AdminResourceReceptors.resourcesResetFilterReceptor(action) for (const action of scenesFetchedQueue()) AdminSceneReceptors.scenesFetchedReceptor(action) for (const action of sceneFetchedQueue()) AdminSceneReceptors.sceneFetchedReceptor(action) - for (const action of locationsRetrievedQueue()) AdminLocationReceptors.locationsRetrievedReceptor(action) - for (const action of locationCreatedQueue()) AdminLocationReceptors.locationCreatedReceptor(action) - for (const action of locationPatchedQueue()) AdminLocationReceptors.locationPatchedReceptor(action) - for (const action of locationRemovedQueue()) AdminLocationReceptors.locationRemovedReceptor(action) - for (const action of locationTypesRetrievedQueue()) AdminLocationReceptors.locationTypesRetrievedReceptor(action) for (const action of activeRoutesRetrievedQueue()) AdminActiveRouteReceptors.activeRoutesRetrievedReceptor(action) for (const action of fetchedInstanceServerQueue()) AdminInstanceServerReceptors.fetchedInstanceServerReceptor(action) for (const action of chargebeeSettingRetrievedQueue())