Skip to content

Commit c7147cd

Browse files
fix notes image upload (#408)
Co-authored-by: Kent C. Dodds <[email protected]>
1 parent 232249c commit c7147cd

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

app/routes/users+/$username_+/__note-editor.tsx

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type ImageFieldset = z.infer<typeof ImageFieldsetSchema>
5555
function imageHasFile(
5656
image: ImageFieldset,
5757
): image is ImageFieldset & { file: NonNullable<ImageFieldset['file']> } {
58-
return Boolean(image.file)
58+
return Boolean(image.file?.size && image.file?.size > 0)
5959
}
6060

6161
const NoteEditorSchema = z.object({
@@ -90,15 +90,19 @@ export async function action({ request }: DataFunctionArgs) {
9090
}).transform(async ({ images = [], ...data }) => {
9191
return {
9292
...data,
93-
images: await Promise.all(
93+
imageIds: images.map(i => i.id).filter(Boolean),
94+
imageUpdates: images
95+
.filter(i => i.id && !imageHasFile(i))
96+
.map(i => ({
97+
id: i.id,
98+
altText: i.altText,
99+
})),
100+
imageUploads: await Promise.all(
94101
images.filter(imageHasFile).map(async image => ({
95102
id: image.id,
96103
altText: image.altText,
97104
contentType: image.file.type,
98-
blob:
99-
image.file.size > 0
100-
? Buffer.from(await image.file.arrayBuffer())
101-
: null,
105+
blob: Buffer.from(await image.file.arrayBuffer()),
102106
})),
103107
),
104108
}
@@ -114,7 +118,14 @@ export async function action({ request }: DataFunctionArgs) {
114118
return json({ status: 'error', submission } as const, { status: 400 })
115119
}
116120

117-
const { id: noteId, title, content, images = [] } = submission.value
121+
const {
122+
id: noteId,
123+
title,
124+
content,
125+
imageUploads = [],
126+
imageUpdates = [],
127+
imageIds,
128+
} = submission.value
118129

119130
const updatedNote = await prisma.$transaction(async $prisma => {
120131
const note = await $prisma.note.upsert({
@@ -129,33 +140,27 @@ export async function action({ request }: DataFunctionArgs) {
129140
title,
130141
content,
131142
images: {
132-
deleteMany: { id: { notIn: images.map(i => i.id).filter(Boolean) } },
143+
deleteMany: { id: { notIn: imageIds } },
144+
updateMany: imageUpdates.map(updates => ({
145+
where: { id: updates.id },
146+
data: updates,
147+
})),
133148
},
134149
},
135150
})
136151

137-
for (const image of images) {
138-
const { blob } = image
139-
if (blob) {
140-
await $prisma.noteImage.upsert({
141-
select: { id: true },
142-
where: { id: image.id ?? '__new_image__' },
143-
create: { ...image, blob, noteId: note.id },
144-
update: {
145-
...image,
146-
blob,
147-
// update the id since it is used for caching
148-
id: cuid(),
149-
noteId: note.id,
150-
},
151-
})
152-
} else if (image.id) {
153-
await $prisma.noteImage.update({
154-
select: { id: true },
155-
where: { id: image.id },
156-
data: { altText: image.altText },
157-
})
158-
}
152+
for (const image of imageUploads) {
153+
await $prisma.noteImage.upsert({
154+
select: { id: true },
155+
where: { id: image.id ?? '__new_image__' },
156+
create: { ...image, noteId: note.id },
157+
update: {
158+
...image,
159+
// update the id since it is used for caching
160+
id: cuid(),
161+
noteId: note.id,
162+
},
163+
})
159164
}
160165

161166
return note

0 commit comments

Comments
 (0)