1- import { ListValue , ObjectItem } from "mendix" ;
1+ import { DynamicValue , ListValue , ObjectItem } from "mendix" ;
22import { FileUploaderContainerProps , UploadModeEnum } from "../../typings/FileUploaderProps" ;
33import { action , computed , makeObservable , observable } from "mobx" ;
4+ import { Big } from "big.js" ;
45import { getImageUploaderFormats , parseAllowedFormats } from "../utils/parseAllowedFormats" ;
56import { FileStore } from "./FileStore" ;
67import { FileRejection } from "react-dropzone" ;
@@ -26,7 +27,7 @@ export class FileUploaderStore {
2627 _maxFileSizeMiB = 0 ;
2728 _maxFileSize = 0 ;
2829 _ds ?: ListValue ;
29- _maxFilesPerUpload : number ;
30+ _maxFilesPerUpload : DynamicValue < Big > ;
3031
3132 errorMessage ?: string = undefined ;
3233
@@ -79,7 +80,10 @@ export class FileUploaderStore {
7980 files : observable ,
8081 existingItemsLoaded : observable ,
8182 errorMessage : observable ,
82- allowedFormatsDescription : computed
83+ allowedFormatsDescription : computed ,
84+ maxFilesPerUpload : computed ,
85+ _maxFilesPerUpload : observable ,
86+ isFileUploadLimitReached : computed
8387 } ) ;
8488
8589 this . updateProps ( props ) ;
@@ -94,6 +98,9 @@ export class FileUploaderStore {
9498 this . _ds = props . associatedImages ;
9599 }
96100
101+ // Update max files properties
102+ this . _maxFilesPerUpload = props . maxFilesPerUpload ;
103+
97104 this . translations . updateProps ( props ) ;
98105 this . updateProcessor . processUpdate ( this . _ds ) ;
99106 }
@@ -113,6 +120,30 @@ export class FileUploaderStore {
113120 . join ( ", " ) ;
114121 }
115122
123+ get maxFilesPerUpload ( ) : number {
124+ const expressionValue = this . _maxFilesPerUpload . value ;
125+ if ( expressionValue ) {
126+ return expressionValue . toNumber ( ) ;
127+ }
128+ // Fallback to unlimited
129+ return 0 ;
130+ }
131+
132+ get isFileUploadLimitReached ( ) : boolean {
133+ const activeFiles = this . files . filter (
134+ file =>
135+ file . fileStatus !== "missing" &&
136+ file . fileStatus !== "removedFile" &&
137+ file . fileStatus !== "validationError"
138+ ) ;
139+ if ( this . maxFilesPerUpload === 0 ) {
140+ return false ;
141+ }
142+
143+ console . log ( "activeFiles" , activeFiles , this . maxFilesPerUpload ) ;
144+ return activeFiles . length >= this . maxFilesPerUpload ;
145+ }
146+
116147 setMessage ( msg ?: string ) : void {
117148 this . errorMessage = msg ;
118149 }
@@ -128,7 +159,7 @@ export class FileUploaderStore {
128159
129160 if ( fileRejections . length && fileRejections [ 0 ] . errors [ 0 ] . code === "too-many-files" ) {
130161 this . setMessage (
131- this . translations . get ( "uploadFailureTooManyFilesMessage" , this . _maxFilesPerUpload . toString ( ) )
162+ this . translations . get ( "uploadFailureTooManyFilesMessage" , this . maxFilesPerUpload . toString ( ) )
132163 ) ;
133164 return ;
134165 }
@@ -164,6 +195,12 @@ export class FileUploaderStore {
164195 for ( const file of acceptedFiles ) {
165196 const newFileStore = FileStore . newFile ( file , this ) ;
166197
198+ if ( this . isFileUploadLimitReached ) {
199+ newFileStore . markError (
200+ this . translations . get ( "uploadFailureTooManyFilesMessage" , this . maxFilesPerUpload . toString ( ) )
201+ ) ;
202+ }
203+
167204 this . files . unshift ( newFileStore ) ;
168205
169206 if ( newFileStore . validate ( ) ) {
0 commit comments