File size: 611 Bytes
cd99321
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { todoApi } from '../api/client'
import { offlineDb, upsertTodoItems } from '../db/offlineDb'
import { onlineThenCache } from './withOfflineFallback'
import type { TodoItem } from '../types'

export const todoRepo = {
  async list(tripId: number | string): Promise<{ items: TodoItem[] }> {
    return onlineThenCache(
      async () => {
        const result = await todoApi.list(tripId)
        upsertTodoItems(result.items)
        return result
      },
      async () => ({
        items: await offlineDb.todoItems
          .where('trip_id').equals(Number(tripId)).toArray(),
      }),
    )
  },
}