Skip to content
Snippets Groups Projects
Commit d7ceb184 authored by Bogdan Cochior's avatar Bogdan Cochior
Browse files

fix(mts): fix files with the same name for package export

parent 882fe476
No related branches found
No related tags found
1 merge request!21Sprint #16 features
...@@ -215,7 +215,7 @@ export default compose( ...@@ -215,7 +215,7 @@ export default compose(
changeForm('reviewerReport', field, value) changeForm('reviewerReport', field, value)
}, },
addFile: ({ formValues = {}, uploadFile, changeForm, version }) => file => { addFile: ({ formValues = {}, uploadFile, changeForm, version }) => file => {
uploadFile(file, 'review', version.id) uploadFile(file, 'review', version)
.then(file => { .then(file => {
const files = formValues.files || [] const files = formValues.files || []
const newFiles = [...files, file] const newFiles = [...files, file]
......
...@@ -183,7 +183,7 @@ export default compose( ...@@ -183,7 +183,7 @@ export default compose(
), ),
withHandlers({ withHandlers({
addFile: ({ formValues = {}, uploadFile, changeForm, version }) => file => { addFile: ({ formValues = {}, uploadFile, changeForm, version }) => file => {
uploadFile(file, 'responseToReviewers', version.id) uploadFile(file, 'responseToReviewers', version)
.then(file => { .then(file => {
const { files } = formValues const { files } = formValues
const newFiles = files.responseToReviewers const newFiles = files.responseToReviewers
......
...@@ -145,7 +145,7 @@ export default compose( ...@@ -145,7 +145,7 @@ export default compose(
changeForm, changeForm,
parentForm = 'wizard', parentForm = 'wizard',
}) => type => file => { }) => type => file => {
uploadFile(file, type, version.id) uploadFile(file, type, version)
.then(file => { .then(file => {
const newFiles = { const newFiles = {
...files, ...files,
......
...@@ -32,10 +32,11 @@ const uploadSuccess = () => ({ ...@@ -32,10 +32,11 @@ const uploadSuccess = () => ({
type: UPLOAD_SUCCESS, type: UPLOAD_SUCCESS,
}) })
const createFileData = (file, type, fragmentId) => { const createFileData = ({ file, type, fragmentId, newName }) => {
const data = new FormData() const data = new FormData()
data.append('fileType', type) data.append('fileType', type)
data.append('fragmentId', fragmentId) data.append('fragmentId', fragmentId)
data.append('newName', newName)
data.append('file', file) data.append('file', file)
return { return {
...@@ -47,6 +48,26 @@ const createFileData = (file, type, fragmentId) => { ...@@ -47,6 +48,26 @@ const createFileData = (file, type, fragmentId) => {
} }
} }
const setFileName = (file, { files }) => {
let newFilename = file.name
const fileCount = Object.values(files)
.reduce((acc, f) => [...acc, ...f], [])
.map(f => f.originalName)
.reduce((count, name) => {
if (file.name === name) {
count += 1
}
return count
}, 0)
if (fileCount > 0) {
const ext = file.name.split('.').reverse()[0]
const filename = file.name.replace(`.${ext}`, '')
newFilename = `${filename} (${fileCount}).${ext}`
}
return newFilename
}
const removeRequest = () => ({ const removeRequest = () => ({
type: REMOVE_REQUEST, type: REMOVE_REQUEST,
}) })
...@@ -66,9 +87,13 @@ export const getRequestStatus = state => state.files.isFetching ...@@ -66,9 +87,13 @@ export const getRequestStatus = state => state.files.isFetching
export const getFileError = state => state.files.error export const getFileError = state => state.files.error
// thunked actions // thunked actions
export const uploadFile = (file, type, fragmentId) => dispatch => { export const uploadFile = (file, type, fragment) => dispatch => {
dispatch(uploadRequest(type)) dispatch(uploadRequest(type))
return request('/files', createFileData(file, type, fragmentId)).then( const newName = setFileName(file, fragment)
return request(
'/files',
createFileData({ file, type, fragmentId: fragment.id, newName }),
).then(
r => { r => {
dispatch(uploadSuccess()) dispatch(uploadSuccess())
return r return r
......
...@@ -41,6 +41,7 @@ module.exports = { ...@@ -41,6 +41,7 @@ module.exports = {
Joi.object({ Joi.object({
id: Joi.string(), id: Joi.string(),
name: Joi.string().required(), name: Joi.string().required(),
originalName: Joi.string(),
type: Joi.string(), type: Joi.string(),
size: Joi.number(), size: Joi.number(),
url: Joi.string(), url: Joi.string(),
...@@ -51,6 +52,7 @@ module.exports = { ...@@ -51,6 +52,7 @@ module.exports = {
Joi.object({ Joi.object({
id: Joi.string(), id: Joi.string(),
name: Joi.string().required(), name: Joi.string().required(),
originalName: Joi.string(),
type: Joi.string(), type: Joi.string(),
size: Joi.number(), size: Joi.number(),
url: Joi.string(), url: Joi.string(),
...@@ -61,6 +63,7 @@ module.exports = { ...@@ -61,6 +63,7 @@ module.exports = {
Joi.object({ Joi.object({
id: Joi.string(), id: Joi.string(),
name: Joi.string().required(), name: Joi.string().required(),
originalName: Joi.string(),
type: Joi.string(), type: Joi.string(),
size: Joi.number(), size: Joi.number(),
url: Joi.string(), url: Joi.string(),
...@@ -71,6 +74,7 @@ module.exports = { ...@@ -71,6 +74,7 @@ module.exports = {
Joi.object({ Joi.object({
id: Joi.string(), id: Joi.string(),
name: Joi.string().required(), name: Joi.string().required(),
originalName: Joi.string(),
type: Joi.string(), type: Joi.string(),
size: Joi.number(), size: Joi.number(),
url: Joi.string(), url: Joi.string(),
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment