diff --git a/src/components/Contexts/FormDataProvider.jsx b/src/components/Contexts/FormDataProvider.jsx index e5c0000e..c3896bdd 100644 --- a/src/components/Contexts/FormDataProvider.jsx +++ b/src/components/Contexts/FormDataProvider.jsx @@ -1,13 +1,16 @@ import React, { createContext, useState } from 'react' import { models, useClient } from 'cozy-client' +import { CozyFile } from 'cozy-doctypes' import { useI18n } from 'cozy-ui/transpiled/react/I18n' +import Alerter from 'cozy-ui/transpiled/react/Alerter' import { useStepperDialogContext } from 'src/components/Hooks/useStepperDialogContext' import getOrCreateAppFolderWithReference from 'src/utils/getFolderWithReference' import { FILES_DOCTYPE } from 'src/doctypes' const { Qualification } = models.document +const HTTP_CODE_CONFLICT = 409 const FormDataContext = createContext() @@ -30,6 +33,7 @@ const FormDataProvider = ({ children }) => { const { metadata } = formData ;(async () => { + let submitSucceeded = null for (const { file, fileMetadata } of formData.data) { const newQualification = { ...qualification, @@ -43,12 +47,49 @@ const FormDataProvider = ({ children }) => { t ) - await client.create(FILES_DOCTYPE, { - data: file, - metadata: { qualification: newQualification }, - dirId: appFolderID - }) - setIsStepperDialogOpen(false) + try { + await client.create(FILES_DOCTYPE, { + data: file, + metadata: { qualification: newQualification }, + dirId: appFolderID + }) + if (submitSucceeded === null) submitSucceeded = true + } catch (err) { + const objError = JSON.parse(JSON.stringify(err)) + if (objError.status === HTTP_CODE_CONFLICT) { + const { filename, extension } = CozyFile.splitFilename({ + name: file.name, + type: 'file' + }) + const newFilename = CozyFile.generateNewFileNameOnConflict(filename) + const newFilenameWithExt = `${newFilename}${extension}` + const blob = file.slice(0, file.size, file.type) + const renamedFile = new File([blob], newFilenameWithExt, { + type: file.type + }) + + try { + await client.create(FILES_DOCTYPE, { + data: renamedFile, + metadata: { qualification: newQualification }, + dirId: appFolderID + }) + if (submitSucceeded === null) submitSucceeded = true + } catch (err) { + submitSucceeded = false + break + } + } else { + submitSucceeded = false + break + } + } + } + setIsStepperDialogOpen(false) + if (submitSucceeded) { + Alerter.success(t('common.saveFile.success')) + } else { + Alerter.error(t(`common.saveFile.error`)) } })() } diff --git a/src/locales/en.json b/src/locales/en.json index c467daca..d7322c32 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -6,7 +6,11 @@ "common": { "cancel": "Cancel", "next": "Next", - "replace": "Replace" + "replace": "Replace", + "saveFile": { + "error": "An error has occurred, please try again later", + "success": "Successfully saved file" + } }, "folder": { "administrative": "Administrative", diff --git a/src/locales/fr.json b/src/locales/fr.json index 75e131ac..3c1c846b 100644 --- a/src/locales/fr.json +++ b/src/locales/fr.json @@ -6,7 +6,11 @@ "common": { "cancel": "Annuler", "next": "Suivant", - "replace": "Remplacer" + "replace": "Remplacer", + "saveFile": { + "error": "Une erreur est survenue, veuillez réessayer ultérieurement", + "success": "Fichier enregistré avec succès" + } }, "folder": { "administrative": "Administratif",