Coder
2 years ago
20 changed files with 434 additions and 188 deletions
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
import http from "../http.service"; |
||||
import * as Res from "./responses.interfaces"; |
||||
|
||||
const fetchFilesLimitsConfig = () => { |
||||
return http.get<Res.IGetFilesConfigRes>("configs/files-config", {}, 'common/'); |
||||
}; |
||||
|
||||
export const configsApi = { |
||||
fetchFilesLimitsConfig |
||||
}; |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
interface IFilesConfigResp { |
||||
avatarWidth?: string; |
||||
avatarHeight?: string; |
||||
avatarSize?: string; |
||||
avatarTypes?: string; |
||||
taskFilesSize?: string; |
||||
taskFilesTypes?: string; |
||||
chatFilesSize?: string; |
||||
chatVideosSize?: string; |
||||
} |
||||
|
||||
export interface IGetFilesConfigRes { |
||||
config: IFilesConfigResp; |
||||
} |
@ -1,133 +1,163 @@
@@ -1,133 +1,163 @@
|
||||
import { TaskAttachmentsActionMode } from './../enum/task-attachments.enums' |
||||
import { |
||||
alertFileSizeExceeded, |
||||
checkFileSize, |
||||
MAX_FILE_SIZE, |
||||
alertNotAllowedFiles, |
||||
checkTaskFiles, |
||||
} from '@/shared/helpers' |
||||
import { mediaService } from '@/services/system' |
||||
import _ from 'lodash' |
||||
import { appEvents, IFile } from '@/shared' |
||||
import { SetStateAction } from 'react' |
||||
import { taskDocumentsService } from '@/services/domain' |
||||
import { Alert } from 'react-native' |
||||
|
||||
export const getAttachmentsMenusOptions = (params: { |
||||
taskId: number | string |
||||
setFiles: (docs: SetStateAction<Array<IFile>>) => void |
||||
mode: TaskAttachmentsActionMode |
||||
taskId: number | string |
||||
setFiles: (docs: SetStateAction<Array<IFile>>) => void |
||||
mode: TaskAttachmentsActionMode |
||||
sizeLimit: number |
||||
types: string |
||||
}) => |
||||
Object.values({ |
||||
camera: { |
||||
name: 'Фото з камери', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
const photo = await mediaService.openCamera() |
||||
|
||||
if (photo.size > MAX_FILE_SIZE) { |
||||
alertFileSizeExceeded([photo]) |
||||
return |
||||
} |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: [photo], |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { taskId: params.taskId }) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
{ ...photo, id: Math.random() }, |
||||
]) |
||||
}, 200) |
||||
}, |
||||
}, |
||||
gallery: { |
||||
name: 'Фото з галереї', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
const images = await mediaService.openMultiplePicker() |
||||
|
||||
const { allowed, exceeded } = checkFileSize(images) |
||||
|
||||
if (!_.isEmpty(exceeded)) alertFileSizeExceeded(exceeded) |
||||
if (_.isEmpty(allowed)) return |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: allowed, |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { taskId: params.taskId }) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
...images.map(it => ({ ...it, id: Math.random() })), |
||||
]) |
||||
}, 200) |
||||
}, |
||||
}, |
||||
file: { |
||||
name: 'Файл', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
try { |
||||
const files = await mediaService.openFilesPicker() |
||||
|
||||
if (_.isEmpty(files)) return |
||||
|
||||
const preparedFiles = files.map(it => ({ |
||||
name: it.name, |
||||
size: it.size, |
||||
type: it.type, |
||||
uri: it.uri, |
||||
})) |
||||
|
||||
const { allowed, exceeded } = |
||||
checkFileSize(preparedFiles) |
||||
|
||||
if (!_.isEmpty(exceeded)) |
||||
alertFileSizeExceeded(exceeded) |
||||
if (_.isEmpty(allowed)) return |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: allowed, |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { taskId: params.taskId }) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
...allowed.map(it => ({ |
||||
...it, |
||||
id: Math.random(), |
||||
})), |
||||
]) |
||||
} catch (err) { |
||||
console.log({ 'mediaService.openFilesPicker': err }) |
||||
} |
||||
}, 200) |
||||
}, |
||||
}, |
||||
}) |
||||
Object.values({ |
||||
camera: { |
||||
name: 'Фото з камери', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
const photo = await mediaService.openCamera() |
||||
|
||||
if (photo.size > params.sizeLimit * 1000) { |
||||
Alert.alert( |
||||
`Не вдалось додати фото`, |
||||
`Розмір файла перевищує допустимий розмір ${ |
||||
params.sizeLimit / 1000 |
||||
}Мб`,
|
||||
) |
||||
|
||||
return |
||||
} |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: [photo], |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { |
||||
taskId: params.taskId, |
||||
}) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
{ ...photo, id: Math.random() }, |
||||
]) |
||||
}, 200) |
||||
}, |
||||
}, |
||||
gallery: { |
||||
name: 'Фото з галереї', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
const images = await mediaService.openMultiplePicker() |
||||
|
||||
const { allowed, notAllowed } = checkTaskFiles( |
||||
images, |
||||
params.sizeLimit, |
||||
params.types, |
||||
) |
||||
|
||||
if (!_.isEmpty(notAllowed)) |
||||
alertNotAllowedFiles( |
||||
notAllowed, |
||||
params.sizeLimit, |
||||
params.types, |
||||
) |
||||
if (_.isEmpty(allowed)) return |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: allowed, |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { |
||||
taskId: params.taskId, |
||||
}) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
...images.map(it => ({ ...it, id: Math.random() })), |
||||
]) |
||||
}, 200) |
||||
}, |
||||
}, |
||||
file: { |
||||
name: 'Файл', |
||||
onPress: () => { |
||||
setTimeout(async () => { |
||||
try { |
||||
const files = await mediaService.openFilesPicker() |
||||
|
||||
if (_.isEmpty(files)) return |
||||
|
||||
const preparedFiles = files.map(it => ({ |
||||
name: it.name, |
||||
size: it.size, |
||||
type: it.type, |
||||
uri: it.uri, |
||||
})) |
||||
|
||||
const { allowed, notAllowed } = checkTaskFiles( |
||||
preparedFiles, |
||||
params.sizeLimit, |
||||
params.types, |
||||
) |
||||
|
||||
if (!_.isEmpty(notAllowed)) |
||||
alertNotAllowedFiles( |
||||
notAllowed, |
||||
params.sizeLimit, |
||||
params.types, |
||||
) |
||||
if (_.isEmpty(allowed)) return |
||||
|
||||
if (params.mode === TaskAttachmentsActionMode.DETAIL) { |
||||
await taskDocumentsService.add({ |
||||
taskId: params.taskId, |
||||
files: allowed, |
||||
}) |
||||
|
||||
appEvents.emit('reloadTaskDocs', { |
||||
taskId: params.taskId, |
||||
}) |
||||
|
||||
appEvents.emit('onFirstDocument', { |
||||
taskId: params.taskId, |
||||
}) |
||||
return |
||||
} |
||||
|
||||
params.setFiles(prevState => [ |
||||
...prevState, |
||||
...allowed.map(it => ({ |
||||
...it, |
||||
id: Math.random(), |
||||
})), |
||||
]) |
||||
} catch (err) { |
||||
console.log({ 'mediaService.openFilesPicker': err }) |
||||
} |
||||
}, 200) |
||||
}, |
||||
}, |
||||
}) |
||||
|
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
import { configsApi } from "@/api"; |
||||
import { transformFilesLimitsConfig } from "@/shared/helpers"; |
||||
import { SetFilesConfig } from "@/store/shared"; |
||||
import { simpleDispatch } from "@/store/store-helpers"; |
||||
|
||||
const loadFilesLimitsConfig = async () => { |
||||
try { |
||||
const { data } = await configsApi.fetchFilesLimitsConfig(); |
||||
if (data) { |
||||
const config = transformFilesLimitsConfig(data.config); |
||||
simpleDispatch(new SetFilesConfig({ config })); |
||||
} |
||||
} catch (e) { |
||||
console.log("Error on load files limits config", e); |
||||
} |
||||
}; |
||||
|
||||
export const configsService = { |
||||
loadFilesLimitsConfig |
||||
}; |
@ -0,0 +1,16 @@
@@ -0,0 +1,16 @@
|
||||
import _ from "lodash"; |
||||
import { IFilesConfig } from "../interfaces"; |
||||
|
||||
export const transformFilesLimitsConfig = ( |
||||
data: Partial<Record<keyof IFilesConfig, string>> |
||||
) => { |
||||
const config: Partial<IFilesConfig> = {}; |
||||
|
||||
if (_.isEmpty(data)) return config; |
||||
|
||||
Object.keys(data).map(it => { |
||||
if (!_.isNaN(Number(data[it]))) config[it] = Number(data[it]); |
||||
else config[it] = data[it].toLowerCase(); |
||||
}); |
||||
return config; |
||||
}; |
@ -0,0 +1,10 @@
@@ -0,0 +1,10 @@
|
||||
export interface IFilesConfig { |
||||
avatarWidth: number; |
||||
avatarHeight: number; |
||||
avatarSize: number; |
||||
avatarTypes: string; |
||||
taskFilesSize: number; |
||||
taskFilesTypes: string; |
||||
chatFilesSize: number; |
||||
chatVideosSize: number; |
||||
} |
Loading…
Reference in new issue