diff --git a/apps/meteor/client/views/admin/customSounds/AddCustomSound.tsx b/apps/meteor/client/views/admin/customSounds/AddCustomSound.tsx index b3a75763c0932..44c94c5098c81 100644 --- a/apps/meteor/client/views/admin/customSounds/AddCustomSound.tsx +++ b/apps/meteor/client/views/admin/customSounds/AddCustomSound.tsx @@ -19,7 +19,7 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr const dispatchToastMessage = useToastMessageDispatch(); const [name, setName] = useState(''); - const [sound, setSound] = useState<{ name: string }>(); + const [sound, setSound] = useState(); const uploadCustomSound = useMethod('uploadCustomSound'); const insertOrUpdateSound = useMethod('insertOrUpdateSound'); @@ -31,10 +31,9 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr const [clickUpload] = useSingleFileInput(handleChangeFile, 'audio/mp3'); const saveAction = useCallback( - // FIXME - async (name: string, soundFile: any) => { + async (name: string, soundFile: File) => { const soundData = createSoundData(soundFile, name); - const validation = validate(soundData, soundFile) as Array[0]>; + const validation = validate(soundData, soundFile); validation.forEach((invalidFieldName) => { throw new Error(t('Required_field', { field: t(invalidFieldName) })); @@ -73,6 +72,10 @@ const AddCustomSound = ({ goToNew, close, onChange, ...props }: AddCustomSoundPr ); const handleSave = useCallback(async () => { + if (!sound) { + return; + } + try { const result = await saveAction(name, sound); if (result) { diff --git a/apps/meteor/client/views/admin/customSounds/EditSound.tsx b/apps/meteor/client/views/admin/customSounds/EditSound.tsx index f46ce0e175b61..a54e881562ae6 100644 --- a/apps/meteor/client/views/admin/customSounds/EditSound.tsx +++ b/apps/meteor/client/views/admin/customSounds/EditSound.tsx @@ -52,10 +52,11 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl const hasUnsavedChanges = useMemo(() => previousName !== name || previousSound !== sound, [name, previousName, previousSound, sound]); const saveAction = useCallback( - // FIXME - async (sound: any) => { - const soundData = createSoundData(sound, name, { previousName, previousSound, _id, extension: sound.extension }); - const validation = validate(soundData, sound); + async (sound: EditSoundProps['data'] | File) => { + const isNewFile = sound instanceof File; + const extension = isNewFile ? undefined : sound.extension; + const soundData = createSoundData(sound, name, { previousName, previousSound, _id, extension: extension ?? '' }); + const validation = validate(soundData, isNewFile ? sound : undefined); if (validation.length === 0) { let soundId: string; try { @@ -68,7 +69,7 @@ function EditSound({ close, onChange, data, ...props }: EditSoundProps): ReactEl soundData._id = soundId; soundData.random = Math.round(Math.random() * 1000); - if (sound && sound !== previousSound) { + if (isNewFile) { dispatchToastMessage({ type: 'success', message: t('Uploading_file') }); const reader = new FileReader(); diff --git a/apps/meteor/client/views/admin/customSounds/lib.ts b/apps/meteor/client/views/admin/customSounds/lib.ts index c447bad77bdeb..b89765f3b50bd 100644 --- a/apps/meteor/client/views/admin/customSounds/lib.ts +++ b/apps/meteor/client/views/admin/customSounds/lib.ts @@ -2,7 +2,7 @@ import type { ICustomSoundData } from '../../../../app/custom-sounds/server/meth type ICustomSoundFile = { name: string; - type: string; + type?: string; extension?: string; }; @@ -20,7 +20,10 @@ export function validate(soundData: ICustomSoundData, soundFile?: ICustomSoundFi if (soundFile) { if (!soundData.previousSound || soundData.previousSound !== soundFile) { - if (!/audio\/mp3/.test(soundFile.type) && !/audio\/mpeg/.test(soundFile.type) && !/audio\/x-mpeg/.test(soundFile.type)) { + if ( + !soundFile.type || + (!/audio\/mp3/.test(soundFile.type) && !/audio\/mpeg/.test(soundFile.type) && !/audio\/x-mpeg/.test(soundFile.type)) + ) { errors.push('FileType'); } }