Skip to content

Commit

Permalink
[CP-3310][MC] Implement Refresh Functionality for FilesManager Data (…
Browse files Browse the repository at this point in the history
…by feature key) - proof
  • Loading branch information
dkarski committed Nov 27, 2024
1 parent cca7b3f commit 9d629d9
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 8 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 { ResultObject } from "Core/core/builder"
import { ipcRenderer } from "electron-better-ipc"
import { APIFeaturesServiceEvents } from "device/models"
import { DeviceId } from "Core/device/constants/device-id"
import { McFileManagerData } from "generic-view/models"

export const getFileManagerDataRequest = (
deviceId: DeviceId
): Promise<ResultObject<McFileManagerData>> => {
return ipcRenderer.callMain(APIFeaturesServiceEvents.GetFileManagerData, {
deviceId,
})
}
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",
}
23 changes: 21 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,14 +5,33 @@

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.undefined()

export type McFileManagerView = z.infer<typeof configValidator>

export const mcFileManagerView = {
key: "mc-file-manager-view",
dataValidator,
dataValidator: mcFileManagerData,
configValidator,
} as const
6 changes: 4 additions & 2 deletions libs/generic-view/models/src/lib/paragraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +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 DataValidator = z.infer<typeof dataValidator>
const configValidator = z
.object({
text: z.string().optional(),
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 { selectConfiguredDevice } from "../selectors/select-configured-devices"
import { FeaturesActions } from "./featues-action-keys"
import { getFileManagerDataRequest } from "../../../../../device/feature/src/lib/api-features/get-file-manager-data.request"
import { generateFileManagerData } from "../../../../views/src/lib/file-manager"

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

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

const features = selectConfiguredDevice(getState(), deviceId)?.features
const fileManagerConfig = features?.["fileManager"]?.config
console.log(fileManagerConfig)
const response = await getFileManagerDataRequest(deviceId)

if (response.ok) {
return {
deviceId,
data: generateFileManagerData(response.data),
}
}
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
13 changes: 13 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,18 @@ export const genericViewsReducer = createReducer(initialState, (builder) => {
: {}),
}
})
builder.addCase(getFileManagerData.fulfilled, (state, action) => {
console.log("getFileManagerData action:", 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 @@ -58,6 +58,19 @@ export const generateOtherFilesList = ({
} => {
const initialListConfig: { [key: string]: ComponentPropsByName } = {
otherFilesList: {
component: "block-plain",
layout: {
padding: "6px 0",
flexLayout: {
direction: "column",
},
},
childrenKeys: [
"otherFilesListChildReference",
"otherFilesListChildProof",
],
},
otherFilesListChildReference: {
component: "block-plain",
layout: {
padding: "6px 0",
Expand All @@ -67,11 +80,19 @@ export const generateOtherFilesList = ({
},
childrenKeys: [],
},
otherFilesListChildProof: {
component: "p3-component",
// data: {
// text: `used space: loading...`,
// },
},
}

return configs.reduce((previousValue, config) => {
const categoryItemKey = `${config.id}otherFilesListItem`
previousValue["otherFilesList"]?.childrenKeys?.push(categoryItemKey)
previousValue["otherFilesListChildReference"]?.childrenKeys?.push(
categoryItemKey
)
previousValue = {
...previousValue,
...generateOtherFilesListItem(config),
Expand Down
8 changes: 5 additions & 3 deletions libs/generic-view/ui/src/lib/texts/paragraphs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import React from "react"
import { APIFC } from "generic-view/utils"
import styled, { css } from "styled-components"
import { ParagraphConfig } from "generic-view/models"
import { DataValidator, ParagraphConfig } from "generic-view/models"
import { isEmpty } from "lodash"
import { Content } from "../data-rows/text-formatted"
import { CommonTextProps, commonTextStyles } from "./common-text-styles"
Expand Down Expand Up @@ -85,11 +85,13 @@ export const P2 = styled.p<ParagraphsProps>`
${commonStyles};
`

export const Paragraph3: APIFC<undefined, ParagraphConfig> = ({
export const Paragraph3: APIFC<DataValidator, ParagraphConfig> = ({
config,
data,
children,
...props
}) => {
const text = config?.text ?? data?.text
return (
<P3
{...props}
Expand All @@ -98,7 +100,7 @@ export const Paragraph3: APIFC<undefined, ParagraphConfig> = ({
$singleLine={config?.singleLine}
$textAlign={config?.textAlign}
>
{isEmpty(children) ? config?.text : children}
{isEmpty(children) ? text : children}
</P3>
)
}
Expand Down
14 changes: 14 additions & 0 deletions libs/generic-view/views/src/lib/file-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Copyright (c) Mudita sp. z o.o. All rights reserved.
* For licensing, see https://github.com/mudita/mudita-center/blob/master/LICENSE.md
*/

import { McFileManagerData } from "generic-view/models"

export const generateFileManagerData = (data: McFileManagerData) => {
return {
otherFilesListChildProof: {
text: `used space: loading ${data.storageInformation[0].usedSpaceBytes}`,
},
}
}

0 comments on commit 9d629d9

Please sign in to comment.