Skip to content

Commit

Permalink
[CP-3310] Refresh feature data by generic strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
dkarski committed Dec 6, 2024
1 parent 68e48cc commit 8c269c4
Show file tree
Hide file tree
Showing 21 changed files with 120 additions and 120 deletions.
47 changes: 11 additions & 36 deletions libs/device/feature/src/lib/api-features/api-features.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
import { AppError } from "Core/core/errors"
import { DeviceId } from "Core/device/constants/device-id"
import { View } from "generic-view/utils"
import { mcFileManagerData, McFileManagerData } from "generic-view/models"

export class APIFeaturesService {
constructor(private deviceProtocol: DeviceProtocol) {}
Expand Down Expand Up @@ -99,8 +98,17 @@ export class APIFeaturesService {
}

@IpcEvent(APIFeaturesServiceEvents.FeatureData)
public async getFeatureData(feature: string): Promise<ResultObject<unknown>> {
const device = this.deviceProtocol.apiDevice
public async getFeatureData({
feature,
deviceId,
}: {
feature: string
deviceId?: DeviceId
}): Promise<ResultObject<unknown>> {
const device = deviceId
? this.deviceProtocol.getAPIDeviceById(deviceId)
: this.deviceProtocol.apiDevice

if (!device) {
return Result.failed(new AppError(GeneralError.NoDevice, ""))
}
Expand Down Expand Up @@ -159,37 +167,4 @@ export class APIFeaturesService {

return Result.failed(response.error)
}

@IpcEvent(APIFeaturesServiceEvents.GetFileManagerData)
public async getFileManagerData({
deviceId,
}: {
deviceId?: DeviceId
}): Promise<ResultObject<McFileManagerData>> {
const device = deviceId
? this.deviceProtocol.getAPIDeviceById(deviceId)
: this.deviceProtocol.apiDevice

if (!device) {
return Result.failed(new AppError(GeneralError.NoDevice, ""))
}

const response = await device.request({
endpoint: "FEATURE_DATA",
method: "GET",
body: {
feature: "fileManager",
lang: "en-US",
},
})
if (response.ok) {
const overviewData = mcFileManagerData.safeParse(response.data.body)

return overviewData.success
? Result.success(overviewData.data)
: Result.failed(new AppError(GeneralError.IncorrectResponse, ""))
}

return Result.failed(response.error)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
*/

import { ipcRenderer } from "electron-better-ipc"
import { McFileManagerData } from "generic-view/models"
import { Feature } from "generic-view/models"
import { APIFeaturesServiceEvents } from "device/models"
import { ResultObject } from "Core/core/builder"
import { DeviceId } from "Core/device/constants/device-id"

export const getFileManagerDataRequest = (
deviceId: DeviceId
): Promise<ResultObject<McFileManagerData>> => {
return ipcRenderer.callMain(APIFeaturesServiceEvents.GetFileManagerData, {
export const getFeatureDataRequest = (
deviceId: DeviceId,
feature: string
): Promise<ResultObject<Feature["data"]>> => {
return ipcRenderer.callMain(APIFeaturesServiceEvents.FeatureData, {
deviceId,
feature,
})
}
2 changes: 1 addition & 1 deletion libs/device/feature/src/lib/api-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ export * from "./api-features.service"
export * from "./get-overview-data.request"
export * from "./get-overview-configuration.request"
export * from "./get-feature-configuration.request"
export * from "./get-file-manager-data.request"
export * from "./get-feature-data.request"
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export enum APIFeaturesServiceEvents {
FeatureData = "apiservice_feature-data",
GetOverviewConfiguration = "apiservice_feature-get-overview-configuration",
GetOverviewData = "apiservice_feature-get-overview-data",
GetFileManagerData = "apiservice_feature-get-file-manager-data",
}
2 changes: 1 addition & 1 deletion libs/generic-view/models/src/lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DeviceProperties } from "device-manager/models"
import { ApiConfig, MenuConfig, OverviewData } from "device/models"
import { DeviceId } from "Core/device/constants/device-id"

interface Feature<Data = Record<string, unknown>> {
export interface Feature<Data = Record<string, unknown>> {
config?: View
data?: Data
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum FeaturesActions {
GetOverviewConfig = "api-actions-get-overview-config",
GetGenericConfig = "api-actions-get-generic-config",
GetOverviewData = "api-actions-get-overview-data",
GetFileManagerData = "api-actions-get-file-manager-data",
GetGenericData = "api-actions-get-generic-data",
GetAllFeatures = "api-actions-get-all-features",
GetSingleFeature = "api-actions-get-single-feature",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { createAsyncThunk } from "@reduxjs/toolkit"
import { Feature } from "generic-view/models"
import { getFeatureDataRequest } from "device/feature"
import { DeviceId } from "Core/device/constants/device-id"
import { ReduxRootState } from "Core/__deprecated__/renderer/store"
import { selectConfiguredDevice } from "../selectors"
import { FeaturesActions } from "./featues-action-keys"
import { transformDataComponents } from "./transform-data-components"

export const getGenericData = createAsyncThunk<
{
deviceId: DeviceId
feature: string
data: Feature["data"]
},
{ deviceId: DeviceId; feature: string },
{ state: ReduxRootState }
>(
FeaturesActions.GetGenericData,
async ({ deviceId, feature }, { rejectWithValue, getState }) => {
const { ok, data } = await getFeatureDataRequest(deviceId, feature)

const features = selectConfiguredDevice(getState(), deviceId)?.features
const featureConfig = features?.[feature]?.config

if (featureConfig === undefined) {
return rejectWithValue("no device")
}

if (ok) {
return {
deviceId,
feature,
data: transformDataComponents(feature, data, featureConfig),
}
}

return rejectWithValue(undefined)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import { DeviceId } from "Core/device/constants/device-id"
import { ReduxRootState } from "Core/__deprecated__/renderer/store"
import { FeaturesActions } from "./featues-action-keys"
import { getOverviewData } from "./get-overview-data.actions"
import { getFileManagerData } from "./get-file-manager-data.actions"
import { getGenericData } from "./get-generic-data.actions"

export const getSingleFeatureData = createAsyncThunk<
undefined,
{ deviceId: DeviceId; feature: string },
{ state: ReduxRootState }
>(
FeaturesActions.GetSingleFeature,
async ({ deviceId, feature }, { getState, dispatch }) => {
async ({ deviceId, feature }, { dispatch }) => {
switch (feature) {
case "mc-overview":
await dispatch(getOverviewData({ deviceId }))
break
case "fileManager":
await dispatch(getFileManagerData({ deviceId }))
break
default:
await dispatch(getGenericData({ deviceId, feature }))
}

return undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { FeaturesActions } from "./featues-action-keys"
import { getOverviewConfig } from "./get-overview-config.actions"
import { getOverviewData } from "./get-overview-data.actions"
import { getGenericConfig } from "./get-generic-config.actions"
import { getFileManagerData } from "./get-file-manager-data.actions"
import { getGenericData } from "./get-generic-data.actions"

export const getSingleFeatures = createAsyncThunk<
undefined,
Expand All @@ -24,12 +24,9 @@ export const getSingleFeatures = createAsyncThunk<
await dispatch(getOverviewConfig({ deviceId }))
await dispatch(getOverviewData({ deviceId }))
break
case "fileManager":
await dispatch(getGenericConfig({ deviceId, feature }))
await dispatch(getFileManagerData({ deviceId }))
break
default:
await dispatch(getGenericConfig({ deviceId, feature }))
await dispatch(getGenericData({ deviceId, feature }))
break
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { Feature } from "generic-view/models"
import { View } from "generic-view/utils"
import { generatedData } from "../../../../ui/src/lib/generated"

export const transformDataComponents = (
feature: string,
data: Feature["data"],
config: View
): Feature["data"] => {
return generatedData[feature](data, config)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/

import { View } from "generic-view/utils"
import { generated } from "../../../../ui/src/lib/generated"
import { generatedViews } from "../../../../ui/src/lib/generated"

export const transformGenericComponents = (view: View): View => {
const fullView = { ...view }
for (const [key, { component, config, layout }] of Object.entries(fullView)) {
const transformComponent = generated[component as keyof typeof generated]
const transformComponent =
generatedViews[component as keyof typeof generatedViews]
if (transformComponent) {
Object.assign(
fullView,
Expand Down
24 changes: 12 additions & 12 deletions libs/generic-view/store/src/lib/views/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { getAPIAny } from "../get-api-any"
import { getMenuConfig } from "../get-menu-config"
import { getOutboxData } from "../outbox/get-outbox-data.action"
import { getGenericConfig } from "../features/get-generic-config.actions"
import { getGenericData } from "../features/get-generic-data.actions"
import {
addDevice,
removeDevice,
Expand All @@ -25,7 +26,6 @@ import {
setMenu,
} from "./actions"
import { transformGenericComponents } from "../features/transform-generic-components"
import { getFileManagerData } from "../features/get-file-manager-data.actions"

export interface GenericState {
menu: MenuElement[] | undefined
Expand Down Expand Up @@ -124,17 +124,6 @@ export const genericViewsReducer = createReducer(initialState, (builder) => {
: {}),
}
})
builder.addCase(getFileManagerData.fulfilled, (state, action) => {
state.lastResponse = action.payload
const deviceId = action.payload.deviceId
state.devices[deviceId].features = {
...state.devices[deviceId].features,
fileManager: {
config: state.devices[deviceId].features?.["fileManager"]?.config,
data: action.payload.data,
},
}
})
builder.addCase(getOverviewConfig.fulfilled, (state, action) => {
state.lastResponse = action.payload
const deviceId = action.payload.deviceId
Expand Down Expand Up @@ -175,6 +164,17 @@ export const genericViewsReducer = createReducer(initialState, (builder) => {
},
}
})
builder.addCase(getGenericData.fulfilled, (state, action) => {
const { deviceId, feature, data } = action.payload
state.lastResponse = action.payload
state.devices[deviceId].features = {
...state.devices[deviceId].features,
[feature]: {
config: state.devices[deviceId].features?.[feature]?.config,
data,
},
}
})
builder.addCase(setDeviceState, (state, action) => {
if (action.payload) {
const id = action.payload.id
Expand Down
14 changes: 12 additions & 2 deletions libs/generic-view/ui/src/lib/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@
*/

import {
Feature,
mcContactsView,
mcFileManagerView,
mcImportContactsButton,
} from "generic-view/models"
import { generateMcContactsView } from "./mc-contacts-view"
import { generateMcFileManagerView } from "./mc-file-manager-view/mc-file-manager-view"
import { generateMcFileManagerView } from "./mc-file-manager/mc-file-manager-view"
import { generateFileManagerData } from "./mc-file-manager/mc-file-manager-data"
import { generateMcImportContactsButton } from "./mc-import-contacts-button"
import { View } from "generic-view/utils"

export * from "./mc-import-contacts-button"
export * from "./mc-contacts-view"

export const generated = {
export const generatedViews = {
[mcContactsView.key]: generateMcContactsView,
[mcFileManagerView.key]: generateMcFileManagerView,
[mcImportContactsButton.key]: generateMcImportContactsButton,
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type generatedDataFn = (data: any, config: View) => Feature["data"]

export const generatedData: Record<string, generatedDataFn> = {
["filesManager"]: generateFileManagerData,
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { EntitiesLoaderConfig, McFileManagerData } from "generic-view/models"
import {
EntitiesLoaderConfig,
Feature,
McFileManagerData,
} from "generic-view/models"
import { View } from "generic-view/utils"

const isEntitiesLoaderConfig = (
Expand Down Expand Up @@ -65,7 +69,7 @@ const generateOtherFilesSpaceInformation = (
export const generateFileManagerData = (
data: McFileManagerData,
config: View
) => {
): Feature["data"] => {
const entitiesLoaderConfig = config.fileManagerLoader.config
const entityTypes = isEntitiesLoaderConfig(entitiesLoaderConfig)
? entitiesLoaderConfig.entityTypes
Expand Down
Loading

0 comments on commit 8c269c4

Please sign in to comment.