Skip to content

Commit

Permalink
feat: Added Alerter when Paper is saved
Browse files Browse the repository at this point in the history
  • Loading branch information
Merkur39 committed Oct 11, 2021
1 parent 5dee5b9 commit 0e761b8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
53 changes: 47 additions & 6 deletions src/components/Contexts/FormDataProvider.jsx
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -30,6 +33,7 @@ const FormDataProvider = ({ children }) => {
const { metadata } = formData

;(async () => {
let submitSucceeded = null
for (const { file, fileMetadata } of formData.data) {
const newQualification = {
...qualification,
Expand All @@ -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`))
}
})()
}
Expand Down
6 changes: 5 additions & 1 deletion src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 5 additions & 1 deletion src/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 0e761b8

Please sign in to comment.