@@ -55,7 +55,7 @@ type ImageFieldset = z.infer<typeof ImageFieldsetSchema>
55
55
function imageHasFile (
56
56
image : ImageFieldset ,
57
57
) : image is ImageFieldset & { file : NonNullable < ImageFieldset [ 'file' ] > } {
58
- return Boolean ( image . file )
58
+ return Boolean ( image . file ?. size && image . file ?. size > 0 )
59
59
}
60
60
61
61
const NoteEditorSchema = z . object ( {
@@ -90,15 +90,19 @@ export async function action({ request }: DataFunctionArgs) {
90
90
} ) . transform ( async ( { images = [ ] , ...data } ) => {
91
91
return {
92
92
...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 (
94
101
images . filter ( imageHasFile ) . map ( async image => ( {
95
102
id : image . id ,
96
103
altText : image . altText ,
97
104
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 ( ) ) ,
102
106
} ) ) ,
103
107
) ,
104
108
}
@@ -114,7 +118,14 @@ export async function action({ request }: DataFunctionArgs) {
114
118
return json ( { status : 'error' , submission } as const , { status : 400 } )
115
119
}
116
120
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
118
129
119
130
const updatedNote = await prisma . $transaction ( async $prisma => {
120
131
const note = await $prisma . note . upsert ( {
@@ -129,33 +140,27 @@ export async function action({ request }: DataFunctionArgs) {
129
140
title,
130
141
content,
131
142
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
+ } ) ) ,
133
148
} ,
134
149
} ,
135
150
} )
136
151
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
+ } )
159
164
}
160
165
161
166
return note
0 commit comments