Skip to content

Commit

Permalink
[CP-XXX] Implement Refresh Functionality for FilesManager Data (by fe…
Browse files Browse the repository at this point in the history
…ature key)
  • Loading branch information
dkarski committed Dec 3, 2024
1 parent d70fa32 commit 91c6466
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 25 deletions.
34 changes: 34 additions & 0 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,6 +19,7 @@ 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 @@ -158,4 +159,37 @@ 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
@@ -0,0 +1,18 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { ipcRenderer } from "electron-better-ipc"
import { McFileManagerData } 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, {
deviceId,
})
}
1 change: 1 addition & 0 deletions libs/device/feature/src/lib/api-features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +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"
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ 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",
}
26 changes: 24 additions & 2 deletions libs/generic-view/models/src/lib/mc-file-manager-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,29 @@

import { z } from "zod"

const dataValidator = z.undefined()
const storageCategoryValidator = z.object({
spaceUsedBytes: z.number(),
spaceUsedString: z.string(),
storageCategory: z.string(),
})

export const mcFileManagerData = z.object({
storageInformation: z.array(
z.object({
storageType: z.string(),
totalSpaceBytes: z.number(),
usedSpaceBytes: z.number(),
totalSpaceString: z.string(),
usedSpaceString: z.string(),
categoriesSpaceInformation: z.record(
z.string(),
storageCategoryValidator
),
})
),
})

export type McFileManagerData = z.infer<typeof mcFileManagerData>

const configValidator = z.object({
entityTypes: z.array(z.string()).min(1),
Expand All @@ -15,6 +37,6 @@ export type McFileManagerView = z.infer<typeof configValidator>

export const mcFileManagerView = {
key: "mc-file-manager-view",
dataValidator,
dataValidator: mcFileManagerData,
configValidator,
} as const
5 changes: 4 additions & 1 deletion libs/generic-view/models/src/lib/paragraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import { z } from "zod"
import { commonTextValidators } from "./common-text-validators"

const dataValidator = z.undefined()
const dataValidator = z.object({
text: z.string().optional(),
})
export type ParagraphData = z.infer<typeof dataValidator>

const configValidator = z
.object({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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 { ReduxRootState } from "Core/__deprecated__/renderer/store"
import { DeviceId } from "Core/device/constants/device-id"
import { getFileManagerDataRequest } from "device/feature"
import { generateFileManagerData } from "generic-view/views"
import { selectConfiguredDevice } from "../selectors/select-configured-devices"
import { FeaturesActions } from "./featues-action-keys"

export const getFileManagerData = createAsyncThunk<
{
deviceId: DeviceId
data: Record<string, unknown>
},
{ deviceId: DeviceId },
{ state: ReduxRootState }
>(
FeaturesActions.GetFileManagerData,
async ({ deviceId }, { rejectWithValue, getState }) => {
const apiConfig = selectConfiguredDevice(getState(), deviceId)?.apiConfig

const features = selectConfiguredDevice(getState(), deviceId)?.features
const fileManagerConfig = features?.["fileManager"]?.config

if (apiConfig === undefined || fileManagerConfig === undefined) {
return rejectWithValue("no device")
}

const response = await getFileManagerDataRequest(deviceId)

if (response.ok) {
return {
deviceId,
data: generateFileManagerData(response.data, fileManagerConfig),
}
}
return rejectWithValue(response.error)
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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"

export const getSingleFeatureData = createAsyncThunk<
undefined,
Expand All @@ -20,6 +21,9 @@ export const getSingleFeatureData = createAsyncThunk<
case "mc-overview":
await dispatch(getOverviewData({ deviceId }))
break
case "fileManager":
await dispatch(getFileManagerData({ deviceId }))
break
}

return undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +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"

export const getSingleFeatures = createAsyncThunk<
undefined,
Expand All @@ -23,6 +24,10 @@ 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 }))
break
Expand Down
12 changes: 12 additions & 0 deletions libs/generic-view/store/src/lib/views/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ 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 @@ -123,6 +124,17 @@ 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
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,12 @@ const generateFileCategoryListItem = ({
[`${id}CategoryListItemStorageText`]: {
component: "p3-component",
config: {
text: "0 KB",
text: "0",
color: "black",
textTransform: "format-bytes",
textTransformOptions: {
minUnit: "KB",
},
},
},
[`${id}CategoryListItemStorageMarker`]: {
Expand Down
Loading

0 comments on commit 91c6466

Please sign in to comment.