fix(contacts): clear photo on server by sending media: null when removed

This commit is contained in:
Kristofer Pettijohn
2026-07-04 14:59:06 +02:00
committed by Linus Rath
parent e6aa79ed94
commit babacca482
2 changed files with 27 additions and 1 deletions
@@ -62,6 +62,27 @@ describe('ContactForm', () => {
expect(phoneAfter.length).toBe(phoneBefore.length + 1); expect(phoneAfter.length).toBe(phoneBefore.length + 1);
}); });
it('sends media: null when an existing photo is removed', async () => {
const onSave = vi.fn().mockResolvedValue(undefined);
const contactWithPhoto: ContactCard = {
...existingContact,
media: {
photo: { kind: 'photo', uri: 'data:image/png;base64,AAAA', mediaType: 'image/png' },
},
};
render(<ContactForm contact={contactWithPhoto} onSave={onSave} onCancel={vi.fn()} />);
fireEvent.click(screen.getByText('remove_photo'));
fireEvent.submit(screen.getByText('save').closest('form')!);
await waitFor(() => {
expect(onSave).toHaveBeenCalledOnce();
});
const savedData = onSave.mock.calls[0][0];
expect(savedData.media).toBeNull();
});
it('submits form data correctly', async () => { it('submits form data correctly', async () => {
const onSave = vi.fn().mockResolvedValue(undefined); const onSave = vi.fn().mockResolvedValue(undefined);
render(<ContactForm onSave={onSave} onCancel={vi.fn()} />); render(<ContactForm onSave={onSave} onCancel={vi.fn()} />);
+6 -1
View File
@@ -525,6 +525,11 @@ export function ContactForm({ contact, addressBooks, allKeywords, defaultAddress
mediaMap[photoKey] = { kind: "photo", uri: photoUri, mediaType: photoMediaType }; mediaMap[photoKey] = { kind: "photo", uri: photoUri, mediaType: photoMediaType };
} }
// Set mediaVluae to null if we are removing media, so the server removes it
const hadMedia = !!contact?.media && Object.keys(contact.media).length > 0;
const mediaValue: Record<string, ContactMedia> | null | undefined =
Object.keys(mediaMap).length > 0 ? mediaMap : (hadMedia ? null : undefined);
const data: Partial<ContactCard> = { const data: Partial<ContactCard> = {
name: { components: nameComponents, isOrdered: true }, name: { components: nameComponents, isOrdered: true },
nicknames: nickname.trim() ? { n0: { name: nickname.trim() } } : undefined, nicknames: nickname.trim() ? { n0: { name: nickname.trim() } } : undefined,
@@ -551,7 +556,7 @@ export function ContactForm({ contact, addressBooks, allKeywords, defaultAddress
calendarUri: calendarUri.trim() || undefined, calendarUri: calendarUri.trim() || undefined,
schedulingUri: schedulingUri.trim() || undefined, schedulingUri: schedulingUri.trim() || undefined,
freeBusyUri: freeBusyUri.trim() || undefined, freeBusyUri: freeBusyUri.trim() || undefined,
media: Object.keys(mediaMap).length > 0 ? mediaMap : undefined, media: mediaValue as Record<string, ContactMedia> | undefined,
...(selectedBookId ? { addressBookIds: { [selectedBookId]: true } } : {}), ...(selectedBookId ? { addressBookIds: { [selectedBookId]: true } } : {}),
}; };