codediodeio / sveltefire

Cybernetically enhanced Firebase apps
MIT License
1.67k stars 127 forks source link

Pagination?? #87

Open Schmell opened 1 year ago

Schmell commented 1 year ago

Again this is not an issue rather a question.

Does anyone know how to make pagination work with the Collection component?

I found this tutorial from fireship.io, but this is using an older version of sveltefire (and firebase). I would assume that it should still work in theory. This is what i tried so far

<script lang="ts">
// ...imports etc..

  let pageSize = 2;
  let field = 'lastModified';
  let path = 'events';
  let lastDoc;

  let collRef = collection(firestore, path);

  const nextPage = (last) => {
    lastDoc = last;
  };

  $: runQuery = () => {
    let q = query(collRef);
    q = query(q, orderBy(field));
    if (lastDoc) {
      q = query(q, startAfter(lastDoc.lastModified));
    }
    q = query(q, limit(pageSize));
    console.log('runQuery: ', q);
    return q;
  };

</script>

<Collection ref={runQuery()} let:data>
    <div slot="loading">Loading Events ...</div>
    {#each data as item}
        <p>{item.name}</p>
    {/each}

    <button  on:click={() => nextPage(data[data.length - 1])}>MORE</button>
</Collection>

If I press the MORE button I know that the runQuery function runs again as I can see the console.log, but the page does not update with the next documents.

I did an experiment using just firestore and the query does work as expected

Any help would be greatly appreciated.

sacrosanctic commented 1 year ago

i think ref in is not reactive.

Force to reload with {#key lastDoc}{/key}