-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WiP] basic mechanism of adding files
- Loading branch information
1 parent
542a6ea
commit 76e6e11
Showing
8 changed files
with
268 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
libs/generic-view/store/src/lib/file-transfer/select-and-send-files.action.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* 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 { ActionName } from "../action-names" | ||
import { ReduxRootState } from "Core/__deprecated__/renderer/store" | ||
import { openFileRequest } from "system-utils/feature" | ||
import { sendFile } from "./send-file.action" | ||
import { selectActiveApiDeviceId } from "../selectors" | ||
import * as path from "node:path" | ||
import { isEmpty } from "lodash" | ||
|
||
interface SendSelectedFilesActionPayload { | ||
typesName: string | ||
fileTypes: string[] | ||
storagePath: string | ||
onFileSuccess?: (file: string) => Promise<void> | ||
onFileError?: (file: string) => Promise<void> | ||
onSuccess?: () => Promise<void> | ||
onError?: () => Promise<void> | ||
} | ||
|
||
export const selectAndSendFilesAction = createAsyncThunk< | ||
undefined, | ||
SendSelectedFilesActionPayload, | ||
{ state: ReduxRootState } | ||
>( | ||
ActionName.SendSelectedFiles, | ||
async ( | ||
{ | ||
typesName, | ||
fileTypes, | ||
storagePath, | ||
onFileSuccess, | ||
onFileError, | ||
onSuccess, | ||
onError, | ||
}, | ||
{ rejectWithValue, dispatch, getState } | ||
) => { | ||
const failedFiles = [] | ||
|
||
const deviceId = selectActiveApiDeviceId(getState()) | ||
const openFileResult = await openFileRequest({ | ||
filters: [ | ||
{ | ||
name: typesName, | ||
extensions: fileTypes, | ||
}, | ||
], | ||
properties: ["openFile", "multiSelections"], | ||
}) | ||
|
||
if (!openFileResult.ok) { | ||
return rejectWithValue("cancelled") | ||
} | ||
|
||
if (!deviceId) { | ||
return rejectWithValue(undefined) | ||
} | ||
|
||
for (const file of openFileResult.data) { | ||
const fileName = path.basename(file) | ||
const sentFile = await dispatch( | ||
sendFile({ | ||
filePath: file, | ||
targetPath: storagePath + fileName, | ||
deviceId, | ||
}) | ||
) | ||
if (sentFile.meta.requestStatus === "fulfilled") { | ||
await onFileSuccess?.(file) | ||
} else { | ||
failedFiles.push(file) | ||
await onFileError?.(file) | ||
} | ||
} | ||
if (isEmpty(failedFiles)) { | ||
await onSuccess?.() | ||
} else { | ||
await onError?.() | ||
} | ||
return | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.