Skip to content

Commit b2917f0

Browse files
Merge pull request #1869 from carbon-design-system/imagecard-upload-fix
fix(dashboardeditor): keep state of images to upload
2 parents 736de54 + 2cc309b commit b2917f0

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

src/components/CardEditor/CardEditForm/CardEditFormItems/ImageCardFormItems/ImageCardFormContent.jsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,27 @@ const ImageCardFormItems = ({ cardConfig, i18n, onChange }) => {
5959
readOnly
6060
value={cardConfig.content?.id || ''}
6161
/>
62-
<Button
63-
kind="ghost"
64-
renderIcon={Close16}
65-
size="field"
66-
iconDescription={mergedI18n.close}
67-
className={`${baseClassName}--form-section ${baseClassName}--form-section-image-clear-button`}
68-
onClick={() =>
69-
// close means clear the image info out of the JSON
70-
onChange(
71-
omit(cardConfig, 'content.id', 'content.src', 'content.alt')
72-
)
73-
}
74-
/>
62+
{cardConfig.content?.id ? (
63+
<Button
64+
kind="ghost"
65+
renderIcon={Close16}
66+
size="field"
67+
iconDescription={mergedI18n.close}
68+
className={`${baseClassName}--form-section ${baseClassName}--form-section-image-clear-button`}
69+
onClick={() =>
70+
// close means clear the image info out of the JSON
71+
onChange(
72+
omit(
73+
cardConfig,
74+
'content.id',
75+
'content.src',
76+
'content.alt',
77+
'content.imgState'
78+
)
79+
)
80+
}
81+
/>
82+
) : null}
7583
</label>
7684
{/* TODO enable once hotspot editing is live <Button
7785
className={`${baseClassName}--form-section-image-btn`}

src/components/DashboardEditor/DashboardEditor.jsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ const DashboardEditor = ({
257257

258258
// show the card gallery if no card is being edited
259259
const [dashboardJson, setDashboardJson] = useState(initialValue);
260+
const [imagesToUpload, setImagesToUpload] = useState([]);
260261
const [selectedCardId, setSelectedCardId] = useState();
261262
const [selectedBreakpointIndex, setSelectedBreakpointIndex] = useState(
262263
breakpointSwitcher?.initialValue
@@ -326,7 +327,18 @@ const DashboardEditor = ({
326327
cardConfig.content.src = availableImages.find(
327328
(image) => image.id === cardConfig.content.id
328329
)?.src;
330+
} else if (
331+
cardConfig.content.imgState === 'new' &&
332+
!imagesToUpload.some((image) => image.id === cardConfig.content.id)
333+
) {
334+
if (cardConfig.content.id && cardConfig.content.src) {
335+
setImagesToUpload((prevImagesToUpload) => [
336+
...prevImagesToUpload,
337+
{ id: cardConfig.content.id, src: cardConfig.content.src },
338+
]);
339+
}
329340
}
341+
330342
// TODO: this is really inefficient
331343
setDashboardJson((oldJSON) => ({
332344
...oldJSON,
@@ -399,10 +411,10 @@ const DashboardEditor = ({
399411
breadcrumbs={headerBreadcrumbs}
400412
onEditTitle={onEditTitle}
401413
onImport={onImport}
402-
onExport={() => onExport(dashboardJson)}
414+
onExport={() => onExport(dashboardJson, imagesToUpload)}
403415
onDelete={onDelete}
404416
onCancel={onCancel}
405-
onSubmit={onSubmit}
417+
onSubmit={(params) => onSubmit(params, imagesToUpload)}
406418
isSubmitDisabled={isSubmitDisabled}
407419
isSubmitLoading={isSubmitLoading}
408420
i18n={mergedI18n}

src/components/DashboardEditor/DashboardEditor.test.jsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,17 @@ describe('DashboardEditor', () => {
238238
const submitBtn = screen.getByText('Save and close');
239239
expect(submitBtn).toBeInTheDocument();
240240
fireEvent.click(submitBtn);
241-
expect(mockOnSubmit).toBeCalledWith({
242-
cards: [],
243-
layouts: {
244-
lg: [],
245-
md: [],
246-
xl: [],
241+
expect(mockOnSubmit).toBeCalledWith(
242+
{
243+
cards: [],
244+
layouts: {
245+
lg: [],
246+
md: [],
247+
xl: [],
248+
},
247249
},
248-
});
250+
[]
251+
);
249252
});
250253

251254
it('selecting cancel should fire onCancel', () => {

0 commit comments

Comments
 (0)