@@ -149,7 +149,7 @@ const createTodoCollection = (type: CollectionType) => {
149149 timestamptz : ( date : string ) => new Date ( date ) ,
150150 } ,
151151 } ,
152- primaryKey : [ `id` ] ,
152+ getId : ( item ) => item . id ,
153153 schema : updateTodoSchema ,
154154 } )
155155 } else {
@@ -198,7 +198,7 @@ const createConfigCollection = (type: CollectionType) => {
198198 } ,
199199 } ,
200200 } ,
201- primaryKey : [ `id` ] ,
201+ getId : ( item ) => item . id ,
202202 schema : updateConfigSchema ,
203203 } )
204204 } else {
@@ -220,7 +220,7 @@ const createConfigCollection = (type: CollectionType) => {
220220 : undefined ,
221221 } ) )
222222 } ,
223- getId : ( item ) => String ( item . id ) ,
223+ getId : ( item ) => item . id ,
224224 schema : updateConfigSchema ,
225225 queryClient,
226226 } )
@@ -290,10 +290,19 @@ export default function App() {
290290
291291 const updateTodo = useOptimisticMutation ( {
292292 mutationFn : async ( { transaction } ) => {
293- const mutation = transaction . mutations [ 0 ] as PendingMutation < UpdateTodo >
294- const { original, changes } = mutation
295- const response = await api . todos . update ( original . id , changes )
296- await collectionSync ( collectionType , mutation , response . txid )
293+ const txids = await Promise . all (
294+ transaction . mutations . map ( async ( mutation ) => {
295+ const { original, changes } = mutation
296+ const response = await api . todos . update ( original . id , changes )
297+ return response . txid
298+ } )
299+ )
300+ await Promise . all (
301+ txids . map ( async ( txid , i ) => {
302+ const mutation = transaction . mutations [ i ]
303+ return await collectionSync ( collectionType , mutation , txid )
304+ } )
305+ )
297306 } ,
298307 } )
299308
@@ -349,12 +358,9 @@ export default function App() {
349358 for ( const config of configData ) {
350359 if ( config . key === key ) {
351360 updateConfig . mutate ( ( ) =>
352- configCollection . update (
353- Array . from ( configCollection . state . values ( ) ) [ 0 ] ! as UpdateConfig ,
354- ( draft ) => {
355- draft . value = value
356- }
357- )
361+ configCollection . update ( config . id , ( draft ) => {
362+ draft . value = value
363+ } )
358364 )
359365
360366 return
@@ -439,14 +445,9 @@ export default function App() {
439445
440446 const toggleTodo = ( todo : UpdateTodo ) => {
441447 updateTodo . mutate ( ( ) =>
442- todoCollection . update (
443- Array . from ( todoCollection . state . values ( ) ) . find (
444- ( t ) => t . id === todo . id
445- ) ! ,
446- ( draft ) => {
447- draft . completed = ! draft . completed
448- }
449- )
448+ todoCollection . update ( todo . id , ( draft ) => {
449+ draft . completed = ! draft . completed
450+ } )
450451 )
451452 }
452453
@@ -544,9 +545,7 @@ export default function App() {
544545 todosToToggle . forEach ( ( t ) => togglingIds . add ( t . id ) )
545546 updateTodo . mutate ( ( ) =>
546547 todoCollection . update (
547- Array . from ( todoCollection . state . values ( ) ) . filter ( ( t ) =>
548- togglingIds . has ( t . id )
549- ) ,
548+ todosToToggle . map ( ( todo ) => todo . id ) ,
550549 ( drafts ) => {
551550 drafts . forEach (
552551 ( draft ) => ( draft . completed = ! allCompleted )
@@ -596,11 +595,7 @@ export default function App() {
596595 < button
597596 onClick = { ( ) => {
598597 deleteTodo . mutate ( ( ) =>
599- todoCollection . delete (
600- Array . from ( todoCollection . state . values ( ) ) . find (
601- ( t ) => t . id === todo . id
602- ) !
603- )
598+ todoCollection . delete ( todo . id )
604599 )
605600 } }
606601 className = "hidden group-hover:block absolute right-[10px] w-[40px] h-[40px] my-auto top-0 bottom-0 text-[30px] text-[#cc9a9a] hover:text-[#af5b5e] transition-colors"
@@ -623,9 +618,7 @@ export default function App() {
623618 onClick = { ( ) => {
624619 deleteTodo . mutate ( ( ) =>
625620 todoCollection . delete (
626- Array . from ( todoCollection . state . values ( ) ) . filter (
627- ( t ) => completedTodos . some ( ( ct ) => ct . id === t . id )
628- )
621+ completedTodos . map ( ( todo ) => todo . id )
629622 )
630623 )
631624 } }
0 commit comments