Skip to content

Commit ec67ab4

Browse files
authored
refactor(attachments): match FE to the updated response for submission attachments TASK-1778 (#5671)
### 💭 Notes Update to match #5642 Updated via changing the `SubmissionAttachment` type in `dataInterface.ts`. Compiler blows up and I cleaned up the mess. I updated the mock responses by copying the `xform_id_string` then changed the last character. Seems more reasonable and better solution than turning the old mocked `id` to a stringified version of the integer. `npm run test` and `npx jest --config ./jsapp/jest/unit.config.ts` passes. ### 👀 Preview steps <!-- Delete this section if behavior can't change. --> <!-- If behavior changes or merely may change, add a preview of a minimal happy path. --> 🟢 Make sure everywhere that needed this update "works as it should" 🤷 AFAIK it seems to be these areas: - Attachment dropdown (I believe this is broken in the single submission modal as of this edit) - Attachment dropdown stories - Media cells - Single submission modal - Single submission view sidebar - Form gallery
1 parent 504bed7 commit ec67ab4

11 files changed

+27
-45
lines changed

jsapp/js/attachments/AttachmentActionsDropdown.mocks.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -309,9 +309,7 @@ export const assetWithImageSubmission: SubmissionResponse = {
309309
mimetype: 'image/jpeg',
310310
filename:
311311
'zefir/attachments/1c0496e69e624ab6a265316eed7127c1/d861c7c7-9c49-42cb-8652-cbe3b6c148ad/the-origin-of-the-world-12_21_57.jpg',
312-
instance: 67,
313-
xform: 21,
314-
id: 22,
312+
uid: 'aALUCUEKcgKk28owQo3LrA',
315313
question_xpath: 'Add_a_picture',
316314
},
317315
],

jsapp/js/attachments/AttachmentActionsDropdown.stories.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { assetWithImage, assetWithImageSubmission } from './AttachmentActionsDro
66
const mockQueryClient = new QueryClient()
77
const mockAsset = assetWithImage
88
const mockSubmission = assetWithImageSubmission
9-
const mockAttachmentId = assetWithImageSubmission._attachments[0].id
9+
const mockAttachmentUid = assetWithImageSubmission._attachments[0].uid
1010

1111
const meta: Meta<typeof AttachmentActionsDropdown> = {
1212
title: 'Components/AttachmentActionsDropdown',
@@ -18,7 +18,7 @@ const meta: Meta<typeof AttachmentActionsDropdown> = {
1818
description:
1919
'To see what happens when attachment is deleted, please add `is_deleted=true` flag to the attachment object in the data.',
2020
},
21-
attachmentId: { control: 'number' },
21+
attachmentUid: { control: 'text' },
2222
onDeleted: { action: 'onDeleted' },
2323
},
2424
args: {},
@@ -37,7 +37,7 @@ export default meta
3737
export const Default: StoryObj<typeof AttachmentActionsDropdown> = {
3838
args: {
3939
asset: mockAsset,
40-
attachmentId: mockAttachmentId,
40+
attachmentUid: mockAttachmentUid,
4141
submissionData: mockSubmission,
4242
onDeleted: () => console.log('Attachment deleted'),
4343
},

jsapp/js/attachments/AttachmentActionsDropdown.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { useRemoveAttachment } from './attachmentsQuery'
1414
interface AttachmentActionsDropdownProps {
1515
asset: AssetResponse
1616
submissionData: SubmissionResponse
17-
attachmentId: number
17+
attachmentUid: string
1818
/**
1919
* Being called after attachment was deleted succesfully. Is meant to be used
2020
* by parent component to reflect this change in the data it holds, and
@@ -33,7 +33,7 @@ export default function AttachmentActionsDropdown(props: AttachmentActionsDropdo
3333
const removeAttachmentMutation = useRemoveAttachment(props.asset.uid, props.submissionData['meta/rootUuid'])
3434
const isFeatureEnabled = useFeatureFlag(FeatureFlag.removingAttachmentsEnabled)
3535

36-
const attachment = props.submissionData._attachments.find((item) => item.id === props.attachmentId)
36+
const attachment = props.submissionData._attachments.find((item) => item.uid === props.attachmentUid)
3737
if (!attachment) {
3838
return null
3939
}
@@ -47,7 +47,7 @@ export default function AttachmentActionsDropdown(props: AttachmentActionsDropdo
4747
setIsDeletePending(true)
4848

4949
try {
50-
await removeAttachmentMutation.mutateAsync(String(attachment.id))
50+
await removeAttachmentMutation.mutateAsync(String(attachment.uid))
5151
setIsDeleteModalOpen(false)
5252
notify(t('##Attachment_type## deleted').replace('##Attachment_type##', attachmentTypeName))
5353
props.onDeleted()

jsapp/js/components/formGallery/formGallery.component.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,11 @@ export default function FormGallery(props: FormGalleryProps) {
121121
<bem.GalleryGrid>
122122
{attachments.map((attachment) =>
123123
attachment.is_deleted ? (
124-
<Center key={attachment.id} title={attachment.filename} className='gallery-grid-deleted-attachment'>
124+
<Center key={attachment.uid} title={attachment.filename} className='gallery-grid-deleted-attachment'>
125125
<DeletedAttachment />
126126
</Center>
127127
) : (
128-
<a key={attachment.id} href={attachment.download_url} target='_blank'>
128+
<a key={attachment.uid} href={attachment.download_url} target='_blank'>
129129
<img src={attachment.download_url} alt={attachment.filename} width='150' loading='lazy' />
130130
</a>
131131
),

jsapp/js/components/processing/sidebar/sidebarSubmissionMedia.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default function SidebarSubmissionMedia(props: SidebarSubmissionMediaProp
5454
<AttachmentActionsDropdown
5555
asset={props.asset}
5656
submissionData={store.data.submissionData}
57-
attachmentId={attachment.id}
57+
attachmentUid={attachment.uid}
5858
onDeleted={() => {
5959
// TODO: this might be done with a bit more elegant UX, as calling the function causes a whole page
6060
// spinner to appear. I feel like redoing `singleProcessingStore` in a `react-query` way would

jsapp/js/components/submissions/mediaCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class MediaCell extends React.Component<MediaCellProps, {}> {
125125
<AttachmentActionsDropdown
126126
asset={asset}
127127
submissionData={submissionData}
128-
attachmentId={attachment.id}
128+
attachmentUid={attachment.uid}
129129
onDeleted={() => {
130130
// Trigger refresh on the Data Table and close the modal
131131
actions.resources.refreshTableSubmissions()

jsapp/js/components/submissions/submissionDataTable.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface SubmissionDataTableProps {
3636
submissionData: SubmissionResponse
3737
translationIndex: number
3838
showXMLNames?: boolean
39-
onAttachmentDeleted: (attachmentId: number) => void
39+
onAttachmentDeleted: (attachmentUid: string) => void
4040
}
4141

4242
/**
@@ -270,9 +270,9 @@ class SubmissionDataTable extends React.Component<SubmissionDataTableProps> {
270270
<AttachmentActionsDropdown
271271
asset={this.props.asset}
272272
submissionData={this.props.submissionData}
273-
attachmentId={attachment.id}
273+
attachmentUid={attachment.uid}
274274
onDeleted={() => {
275-
this.props.onAttachmentDeleted(attachment.id)
275+
this.props.onAttachmentDeleted(attachment.uid)
276276
}}
277277
/>
278278
)

jsapp/js/components/submissions/submissionModal.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,12 +435,12 @@ export default class SubmissionModal extends React.Component<SubmissionModalProp
435435
return undefined
436436
}
437437

438-
handleDeletedAttachment(attachmentId: number) {
438+
handleDeletedAttachment(attachmentUid: string) {
439439
if (this.state.submission) {
440440
// Override the attachment object in memory to mark it as deleted (without
441441
// making an API call for fresh submission data)
442442
this.setState({
443-
submission: markAttachmentAsDeleted(this.state.submission, attachmentId),
443+
submission: markAttachmentAsDeleted(this.state.submission, attachmentUid),
444444
})
445445

446446
// Prompt table to refresh submission list

jsapp/js/components/submissions/submissionUtils.mocks.ts

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,9 @@ export const everythingSurveySubmission = {
17601760
'http://kc.kobo.local/media/original?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2FIMG_3619-13_33_22.MOV',
17611761
filename:
17621762
'kobo/attachments/712e5fb8d7364482a57c60df876c57fb/fdd252ee-860a-426c-be90-cbbf61787cb9/IMG_3619-13_33_22.MOV',
1763-
instance: 25,
1763+
uid: 'aXPSbtQcYDm8mcJuVZTUhI',
17641764
download_medium_url:
17651765
'http://kc.kobo.local/media/medium?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2FIMG_3619-13_33_22.MOV',
1766-
id: 6,
1767-
xform: 18,
17681766
question_xpath: 'A_video',
17691767
},
17701768
{
@@ -1777,11 +1775,9 @@ export const everythingSurveySubmission = {
17771775
'http://kc.kobo.local/media/original?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2F07.+Crazy+Love-13_32_31.mp3',
17781776
filename:
17791777
'kobo/attachments/712e5fb8d7364482a57c60df876c57fb/fdd252ee-860a-426c-be90-cbbf61787cb9/07. Crazy Love-13_32_31.mp3',
1780-
instance: 25,
17811778
download_medium_url:
17821779
'http://kc.kobo.local/media/medium?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2F07.+Crazy+Love-13_32_31.mp3',
1783-
id: 5,
1784-
xform: 18,
1780+
uid: 'aXPSbtQcYDm8mcJuVZTUhQ',
17851781
question_xpath: 'Voice_password',
17861782
},
17871783
{
@@ -1794,11 +1790,9 @@ export const everythingSurveySubmission = {
17941790
'http://kc.kobo.local/media/original?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2Fzamki-13_35_5.txt',
17951791
filename:
17961792
'kobo/attachments/712e5fb8d7364482a57c60df876c57fb/fdd252ee-860a-426c-be90-cbbf61787cb9/zamki-13_35_5.txt',
1797-
instance: 25,
17981793
download_medium_url:
17991794
'http://kc.kobo.local/media/medium?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2Fzamki-13_35_5.txt',
1800-
id: 4,
1801-
xform: 18,
1795+
uid: 'aXPSbtQcYDm8mcJuVZTUhE',
18021796
question_xpath: 'We_need_your_CV',
18031797
},
18041798
{
@@ -1811,11 +1805,9 @@ export const everythingSurveySubmission = {
18111805
'http://kc.kobo.local/media/original?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2F784397e28b5041d59bef15d5d0b2d0bf--cutaway-dio-13_31_48.jpg',
18121806
filename:
18131807
'kobo/attachments/712e5fb8d7364482a57c60df876c57fb/fdd252ee-860a-426c-be90-cbbf61787cb9/784397e28b5041d59bef15d5d0b2d0bf--cutaway-dio-13_31_48.jpg',
1814-
instance: 25,
18151808
download_medium_url:
18161809
'http://kc.kobo.local/media/medium?media_file=kobo%2Fattachments%2F712e5fb8d7364482a57c60df876c57fb%2Ffdd252ee-860a-426c-be90-cbbf61787cb9%2F784397e28b5041d59bef15d5d0b2d0bf--cutaway-dio-13_31_48.jpg',
1817-
id: 3,
1818-
xform: 18,
1810+
uid: 'aXPSbtQcYDm8mcJuVZTUhR',
18191811
question_xpath: 'Selfportrait',
18201812
},
18211813
],
@@ -2359,9 +2351,7 @@ export const submissionWithAttachmentsWithUnicode = {
23592351
mimetype: 'image/jpeg',
23602352
filename:
23612353
'kobo/attachments/45748fd461814880bd9545c8c8827d78/4cfa16e8-f29b-41a9-984c-2bf7fe05064b/Un_ete_au_Quebec_Canada-19_41_32.jpg',
2362-
instance: 18,
2363-
xform: 4,
2364-
id: 13,
2354+
uid: 'azCy24QgjprZGrdvbHQXrA',
23652355
question_xpath: 'A_picture',
23662356
},
23672357
],
@@ -3147,9 +3137,7 @@ export const submissionWithSupplementalDetails = {
31473137
download_small_url: 'http://kf.kobo.local/api/v2/assets/aDDywpeYGnvuDLTeiveyxZ/data/3/attachments/3/',
31483138
mimetype: 'audio/mpeg',
31493139
filename: 'kobo/attachments/c71e63f6a...b9a05/8BP076-09-rushjet1-unknown_sector-12_42_20.mp3',
3150-
instance: 3,
3151-
xform: 1,
3152-
id: 3,
3140+
uid: 'aDDywpeYGnvuDLTeiveyxV',
31533141
question_xpath: 'Secret_password_as_an_audio_file',
31543142
},
31553143
],
@@ -3277,9 +3265,7 @@ export const submissionWithNestedSupplementalDetails = {
32773265
mimetype: 'audio/x-m4a',
32783266
filename:
32793267
'zefir/attachments/54445e08ab6d4011ae648adc6ae8c9bc/64c7fe7c-4542-472d-8eab-6e42f68c7a0b/test-spoken-16_49_36.m4a',
3280-
instance: 77,
3281-
xform: 27,
3282-
id: 70,
3268+
uid: 'aw6mhS4KnoG5E8EbQp9KgP',
32833269
question_xpath: 'level_a/level_b/level_c/sounds',
32843270
},
32853271
],

jsapp/js/components/submissions/submissionUtils.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,14 @@ function appendTextToPathAtLevel(path: string, level: string, stringToAdd: strin
757757
*/
758758
export function markAttachmentAsDeleted(
759759
submissionData: SubmissionResponse,
760-
targetAttachmentId: number,
760+
targetAttachmentUid: string,
761761
): SubmissionResponse {
762762
const data = clonedeep(submissionData)
763-
const targetAttachment = data._attachments.find((item) => item.id === targetAttachmentId)
763+
const targetAttachment = data._attachments.find((item) => item.uid === targetAttachmentUid)
764764

765765
data._attachments.forEach((attachment) => {
766766
if (
767-
attachment.id === targetAttachment?.id &&
767+
attachment.uid === targetAttachment?.uid &&
768768
attachment.question_xpath === targetAttachment?.question_xpath &&
769769
attachment.filename === targetAttachment?.filename
770770
) {

0 commit comments

Comments
 (0)