Skip to content

Commit

Permalink
Revert "@uppy/core: reference updated i18n in Restricter"
Browse files Browse the repository at this point in the history
This reverts commit d8746f0.
  • Loading branch information
Murderlon committed Apr 25, 2024
1 parent d8746f0 commit 09a2a52
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 52 deletions.
26 changes: 11 additions & 15 deletions packages/@uppy/core/src/Restricter.ts
Expand Up @@ -3,6 +3,7 @@
import prettierBytes from '@transloadit/prettier-bytes'
// @ts-ignore untyped
import match from 'mime-match'
import Translator from '@uppy/utils/lib/Translator'
import type { Body, Meta, UppyFile } from '@uppy/utils/lib/UppyFile'
import type { I18n } from '@uppy/utils/lib/Translator'
import type { State, NonNullableUppyOptions } from './Uppy'
Expand Down Expand Up @@ -57,15 +58,12 @@ class RestrictionError<M extends Meta, B extends Body> extends Error {
}

class Restricter<M extends Meta, B extends Body> {
getI18n: () => I18n
i18n: Translator['translate']

getOpts: () => NonNullableUppyOptions<M, B>

constructor(
getOpts: () => NonNullableUppyOptions<M, B>,
getI18n: () => I18n,
) {
this.getI18n = getI18n
constructor(getOpts: () => NonNullableUppyOptions<M, B>, i18n: I18n) {
this.i18n = i18n
this.getOpts = (): NonNullableUppyOptions<M, B> => {
const opts = getOpts()

Expand All @@ -90,7 +88,7 @@ class Restricter<M extends Meta, B extends Body> {
const nonGhostFiles = existingFiles.filter((f) => !f.isGhost)
if (nonGhostFiles.length + addingFiles.length > maxNumberOfFiles) {
throw new RestrictionError(
`${this.getI18n()('youCanOnlyUploadX', {
`${this.i18n('youCanOnlyUploadX', {
smart_count: maxNumberOfFiles,
})}`,
)
Expand All @@ -110,7 +108,7 @@ class Restricter<M extends Meta, B extends Body> {

if (totalFilesSize > maxTotalFileSize) {
throw new RestrictionError(
this.getI18n()('exceedsSize', {
this.i18n('exceedsSize', {
size: prettierBytes(maxTotalFileSize),
file: addingFile.name,
}),
Expand Down Expand Up @@ -143,7 +141,7 @@ class Restricter<M extends Meta, B extends Body> {
if (!isCorrectFileType) {
const allowedFileTypesString = allowedFileTypes.join(', ')
throw new RestrictionError(
this.getI18n()('youCanOnlyUploadFileTypes', {
this.i18n('youCanOnlyUploadFileTypes', {
types: allowedFileTypesString,
}),
{ file } as { file: UppyFile<M, B> },
Expand All @@ -154,7 +152,7 @@ class Restricter<M extends Meta, B extends Body> {
// We can't check maxFileSize if the size is unknown.
if (maxFileSize && file.size != null && file.size > maxFileSize) {
throw new RestrictionError(
this.getI18n()('exceedsSize', {
this.i18n('exceedsSize', {
size: prettierBytes(maxFileSize),
file: file.name,
}),
Expand All @@ -165,7 +163,7 @@ class Restricter<M extends Meta, B extends Body> {
// We can't check minFileSize if the size is unknown.
if (minFileSize && file.size != null && file.size < minFileSize) {
throw new RestrictionError(
this.getI18n()('inferiorSize', {
this.i18n('inferiorSize', {
size: prettierBytes(minFileSize),
}),
{ file } as { file: UppyFile<M, B> },
Expand All @@ -187,9 +185,7 @@ class Restricter<M extends Meta, B extends Body> {
const { minNumberOfFiles } = this.getOpts().restrictions
if (minNumberOfFiles && Object.keys(files).length < minNumberOfFiles) {
throw new RestrictionError(
this.getI18n()('youHaveToAtLeastSelectX', {
smart_count: minNumberOfFiles,
}),
this.i18n('youHaveToAtLeastSelectX', { smart_count: minNumberOfFiles }),
)
}
}
Expand All @@ -199,7 +195,7 @@ class Restricter<M extends Meta, B extends Body> {
error: RestrictionError<M, B>
} {
const error = new RestrictionError<M, B>(
this.getI18n()('missingRequiredMetaFieldOnFile', { fileName: file.name }),
this.i18n('missingRequiredMetaFieldOnFile', { fileName: file.name }),
)
const { requiredMetaFields } = this.getOpts().restrictions
const missingFields: string[] = []
Expand Down
34 changes: 1 addition & 33 deletions packages/@uppy/core/src/Uppy.test.ts
Expand Up @@ -7,7 +7,6 @@ import fs from 'node:fs'
import path from 'node:path'
import prettierBytes from '@transloadit/prettier-bytes'
import type { Body, Meta } from '@uppy/utils/lib/UppyFile'
import type { Locale } from '@uppy/utils/lib/Translator'
import Core from './index.ts'
import UIPlugin from './UIPlugin.ts'
import BasePlugin, {
Expand Down Expand Up @@ -1540,19 +1539,7 @@ describe('src/Core', () => {
})
})

it.only('should change restrictions on the fly', () => {
const fr_FR: Locale<0 | 1> = {
strings: {
youCanOnlyUploadFileTypes:
'Vous pouvez seulement téléverser: %{types}',
},
pluralize(n) {
if (n <= 1) {
return 0
}
return 1
},
}
it('should change restrictions on the fly', () => {
const core = new Core({
restrictions: {
allowedFileTypes: ['image/jpeg'],
Expand All @@ -1573,25 +1560,6 @@ describe('src/Core', () => {
}

core.setOptions({
locale: fr_FR,
})

try {
core.addFile({
source: 'vi',
name: 'foo1.png',
type: 'image/png',
// @ts-ignore
data: new File([sampleImage], { type: 'image/png' }),
})
} catch (err) {
expect(err).toMatchObject(
new Error('Vous pouvez seulement téléverser: image/jpeg'),
)
}

core.setOptions({
locale: fr_FR,
restrictions: {
allowedFileTypes: ['image/png'],
},
Expand Down
5 changes: 1 addition & 4 deletions packages/@uppy/core/src/Uppy.ts
Expand Up @@ -459,10 +459,7 @@ export class Uppy<M extends Meta, B extends Body> {
info: [],
})

this.#restricter = new Restricter<M, B>(
() => this.opts,
() => this.i18n,
)
this.#restricter = new Restricter<M, B>(() => this.opts, this.i18n)

this.#storeUnsubscribe = this.store.subscribe(
// eslint-disable-next-line
Expand Down

0 comments on commit 09a2a52

Please sign in to comment.