generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from eea/develop
Take screenshot of preview image middleware
- Loading branch information
Showing
7 changed files
with
225 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { | ||
CREATE_CONTENT, | ||
UPDATE_CONTENT, | ||
} from '@plone/volto/constants/ActionTypes'; | ||
|
||
export const preview_image = (middlewares) => [ | ||
(store) => (next) => async (action) => { | ||
if (![CREATE_CONTENT, UPDATE_CONTENT].includes(action.type)) { | ||
return next(action); | ||
} | ||
const state = store.getState(); | ||
const contentData = state.content.data; | ||
const type = action?.request?.data?.['@type'] || contentData['@type']; | ||
const lastPreviewImage = Object.keys(action?.request?.data).includes( | ||
'preview_image', | ||
) | ||
? action?.request?.data.preview_image | ||
: contentData?.preview_image; | ||
if ( | ||
!contentData || | ||
type !== 'map_visualization' || | ||
contentData.preview_image_saved || | ||
!action?.request?.data?.map_visualization_data?.preview | ||
) { | ||
return next(action); | ||
} | ||
|
||
if ( | ||
lastPreviewImage && | ||
lastPreviewImage?.filename !== 'preview_image_generated_map_simple.png' | ||
) { | ||
if (action?.request?.data?.map_visualization_data?.preview) { | ||
const mapVisualizationData = { | ||
...action.request.data.map_visualization_data, | ||
}; | ||
delete mapVisualizationData.preview; | ||
|
||
return next({ | ||
...action, | ||
request: { | ||
...action.request, | ||
data: { | ||
...action.request.data, | ||
map_visualization_data: mapVisualizationData, | ||
}, | ||
}, | ||
}); | ||
} else return next(action); | ||
} | ||
const preview = action?.request?.data?.map_visualization_data?.preview; | ||
if ( | ||
preview === 'loading' && | ||
// eslint-disable-next-line no-alert | ||
window.confirm('Do you want to save a preview image?') | ||
) { | ||
// eslint-disable-next-line no-alert | ||
window.alert('Wait for the preview image to generate!'); | ||
return; | ||
} else if ( | ||
preview !== 'loading' && | ||
// eslint-disable-next-line no-alert | ||
!window.confirm('Do you want to save a preview image?') | ||
) { | ||
return next(action); | ||
} | ||
try { | ||
const previewImage = { | ||
preview_image: { | ||
data: action.request.data.map_visualization_data.preview.split( | ||
',', | ||
)[1], | ||
encoding: 'base64', | ||
'content-type': 'image/png', | ||
filename: 'preview_image_generated_map_simple.png', | ||
}, | ||
preview_image_saved: true, | ||
}; | ||
|
||
const mapVisualizationData = { | ||
...action.request.data.map_visualization_data, | ||
}; | ||
delete mapVisualizationData.preview; | ||
|
||
return next({ | ||
...action, | ||
request: { | ||
...action.request, | ||
data: { | ||
...action.request.data, | ||
...previewImage, | ||
map_visualization_data: mapVisualizationData, | ||
}, | ||
}, | ||
}); | ||
} catch (error) { | ||
return next(action); | ||
} | ||
}, | ||
...middlewares, | ||
]; |