Skip to content

Commit 5d0c263

Browse files
use altText to handle state error on image input (#1197)
use altText to handle state error on image input
1 parent e122921 commit 5d0c263

3 files changed

Lines changed: 95 additions & 19 deletions

File tree

libs/api/metadata-converter/src/lib/iso19139/write-parts.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,25 +1184,27 @@ export function writeGraphicOverviews(
11841184
findOrCreateIdentification(),
11851185
removeChildrenByName('gmd:graphicOverview'),
11861186
appendChildren(
1187-
...record.overviews.map((overview) =>
1188-
pipe(
1189-
createNestedElement('gmd:graphicOverview', 'gmd:MD_BrowseGraphic'),
1190-
appendChildren(
1191-
pipe(
1192-
createElement('gmd:fileName'),
1193-
writeCharacterString(overview.url.toString())
1194-
)
1195-
),
1196-
'description' in overview
1197-
? appendChildren(
1198-
pipe(
1199-
createElement('gmd:fileDescription'),
1200-
writeCharacterString(overview.description)
1201-
)
1187+
...record.overviews
1188+
.filter((overview) => overview.url)
1189+
.map((overview) =>
1190+
pipe(
1191+
createNestedElement('gmd:graphicOverview', 'gmd:MD_BrowseGraphic'),
1192+
appendChildren(
1193+
pipe(
1194+
createElement('gmd:fileName'),
1195+
writeCharacterString(overview.url.toString())
12021196
)
1203-
: noop
1197+
),
1198+
'description' in overview
1199+
? appendChildren(
1200+
pipe(
1201+
createElement('gmd:fileDescription'),
1202+
writeCharacterString(overview.description)
1203+
)
1204+
)
1205+
: noop
1206+
)
12041207
)
1205-
)
12061208
)
12071209
)(rootEl)
12081210
}

libs/ui/elements/src/lib/image-input/image-input.component.spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,61 @@ describe('ImageInputComponent', () => {
157157
}, 0)
158158
}))
159159
})
160+
161+
describe('reinitialize errors at dataset rollback', () => {
162+
beforeEach(() => {
163+
component.maxSizeMB = 1
164+
})
165+
166+
it('should emit KO as altText when downloading invalid url and reset errors when dataset is rollback', waitForAsync(() => {
167+
jest.spyOn(component.altTextChange, 'emit')
168+
const nonImageFile = new File([], 'test.txt', { type: 'text/plain' })
169+
component.handleDropFiles([nonImageFile])
170+
expect(component.altTextChange.emit).toHaveBeenCalledWith('KO')
171+
component.altText = 'KO' // Simulate setting altText to KO by the parent component
172+
expect(component.imageFileError).toBe(true)
173+
174+
// Simulate component reset ( dataset rollback )
175+
component.altText = null
176+
177+
expect(component.uploadError).toBe(false)
178+
expect(component.imageFileError).toBe(false)
179+
}))
180+
181+
it('should emit KO as altText when downloading 404 url and reset errors when dataset is rollback', waitForAsync(async () => {
182+
jest.spyOn(component.altTextChange, 'emit')
183+
184+
const downloadPromise = component.downloadUrl(
185+
'http://test.com/invalid.png'
186+
)
187+
188+
const reqHead = httpTestingController.expectOne(
189+
'http://test.com/invalid.png'
190+
)
191+
expect(reqHead.request.method).toEqual('HEAD')
192+
193+
const responseHeaders = new HttpHeaders()
194+
.set('content-type', 'image/png')
195+
.set('content-length', '1048575')
196+
reqHead.flush(null, {
197+
headers: responseHeaders,
198+
status: 404,
199+
statusText: 'OK',
200+
})
201+
202+
await downloadPromise //Await download promise to be resolve and finish emits
203+
204+
expect(component.altTextChange.emit).toHaveBeenCalledWith('KO')
205+
component.altText = 'KO' // Simulate setting altText to KO by the parent component
206+
expect(component.imageFileError).toBe(true)
207+
208+
// Simulate component reset ( dataset rollback )
209+
component.altText = null
210+
211+
expect(component.uploadError).toBe(false)
212+
expect(component.imageFileError).toBe(false)
213+
214+
httpTestingController.verify()
215+
}))
216+
})
160217
})

libs/ui/elements/src/lib/image-input/image-input.component.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,22 @@ import { ImageOverlayPreviewComponent } from '../image-overlay-preview/image-ove
6666
],
6767
})
6868
export class ImageInputComponent {
69-
@Input() maxSizeMB: number
69+
private _altText?: string
70+
7071
@Input() previewUrl?: string
71-
@Input() altText?: string
72+
@Input()
73+
get altText(): string | undefined {
74+
return this._altText
75+
}
76+
set altText(value: string | undefined) {
77+
if (value !== 'KO' && this._altText === 'KO') {
78+
//This is a dataset rollback after upload error
79+
this.resetErrors()
80+
}
81+
this._altText = value
82+
}
83+
84+
@Input() maxSizeMB: number
7285
@Input() uploadProgress?: number
7386
@Input() uploadError?: boolean
7487
@Input() disabled?: boolean = false
@@ -132,6 +145,7 @@ export class ImageInputComponent {
132145
this.resizeAndEmit(validFiles[0])
133146
} else {
134147
this.imageFileError = true
148+
this.handleAltTextChange('KO')
135149
}
136150
}
137151

@@ -143,6 +157,7 @@ export class ImageInputComponent {
143157
this.resizeAndEmit(validFiles[0])
144158
} else {
145159
this.imageFileError = true
160+
this.handleAltTextChange('KO')
146161
}
147162
}
148163

@@ -171,13 +186,15 @@ export class ImageInputComponent {
171186
},
172187
error: () => {
173188
this.imageFileError = true
189+
this.handleAltTextChange('KO')
174190
this.cd.markForCheck()
175191
this.urlChange.emit(url)
176192
},
177193
})
178194
}
179195
} catch {
180196
this.imageFileError = true
197+
this.handleAltTextChange('KO')
181198
this.cd.markForCheck()
182199
return
183200
}

0 commit comments

Comments
 (0)