inertia-sails 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -205,9 +205,10 @@ settings: sails.inertia.deepMerge(() => updatedSettings)
205
205
 
206
206
  ### Infinite Scroll
207
207
 
208
- Paginate data with automatic merge behavior:
208
+ Paginate data with automatic merge behavior. Works with Inertia.js v2's `<InfiniteScroll>` component:
209
209
 
210
210
  ```js
211
+ // Controller
211
212
  const page = this.req.param('page', 0)
212
213
  const perPage = 20
213
214
  const invoices = await Invoice.find().paginate(page, perPage)
@@ -226,6 +227,23 @@ return {
226
227
  }
227
228
  ```
228
229
 
230
+ ```vue
231
+ <!-- Vue component -->
232
+ <script setup>
233
+ import { InfiniteScroll } from '@inertiajs/vue3'
234
+
235
+ defineProps({ invoices: Object })
236
+ </script>
237
+
238
+ <template>
239
+ <InfiniteScroll data="invoices">
240
+ <div v-for="invoice in invoices.data" :key="invoice.id">
241
+ {{ invoice.invoiceNumber }}
242
+ </div>
243
+ </InfiniteScroll>
244
+ </template>
245
+ ```
246
+
229
247
  ### History Encryption
230
248
 
231
249
  Encrypt sensitive data in browser history:
@@ -3,6 +3,7 @@ const resolveDeferredProps = require('../props/resolve-deferred-props')
3
3
  const resolveMergeProps = require('../props/resolve-merge-props')
4
4
  const { resolveOncePropsMetadata } = require('../props/resolve-once-props')
5
5
  const resolvePageProps = require('../props/resolve-page-props')
6
+ const resolveScrollProps = require('../props/resolve-scroll-props')
6
7
 
7
8
  /**
8
9
  * @typedef {Object} InertiaPageObject
@@ -16,6 +17,7 @@ const resolvePageProps = require('../props/resolve-page-props')
16
17
  * @property {string[]} [deepMergeProps] - Props that should be deep merged
17
18
  * @property {Object.<string, string[]>} [deferredProps] - Deferred props by group
18
19
  * @property {Object.<string, *>} [onceProps] - Once-prop metadata
20
+ * @property {Object.<string, *>} [scrollProps] - Scroll props for InfiniteScroll component
19
21
  * @property {Object.<string, *>} [flash] - Flash data (not persisted in history)
20
22
  */
21
23
 
@@ -60,7 +62,8 @@ module.exports = async function buildPageObject(req, component, pageProps) {
60
62
  encryptHistory: sails.inertia.shouldEncryptHistory(),
61
63
  ...resolveMergeProps(req, allProps),
62
64
  ...resolveDeferredProps(req, component, allProps),
63
- ...resolveOncePropsMetadata(allProps)
65
+ ...resolveOncePropsMetadata(allProps),
66
+ ...resolveScrollProps(allProps)
64
67
  }
65
68
 
66
69
  // Consume flash data from session and add to props
@@ -0,0 +1,52 @@
1
+ const ScrollProp = require('./scroll-prop')
2
+
3
+ /**
4
+ * Resolve scroll props metadata for the page response.
5
+ * Extracts ScrollProp instances and builds the scrollProps object
6
+ * expected by Inertia.js v2's <InfiniteScroll> component.
7
+ *
8
+ * @param {Object} pageProps - The page props
9
+ * @returns {Object} - Object with scrollProps if any exist
10
+ *
11
+ * @example
12
+ * // Input props with ScrollProp instance:
13
+ * { invoices: ScrollProp { pageName: 'page', currentPage: 1, ... } }
14
+ *
15
+ * // Output:
16
+ * {
17
+ * scrollProps: {
18
+ * invoices: {
19
+ * pageName: 'page',
20
+ * currentPage: 1,
21
+ * previousPage: null,
22
+ * nextPage: 2,
23
+ * reset: false
24
+ * }
25
+ * }
26
+ * }
27
+ */
28
+ module.exports = function resolveScrollProps(pageProps) {
29
+ const scrollProps = {}
30
+
31
+ for (const [key, value] of Object.entries(pageProps || {})) {
32
+ if (value instanceof ScrollProp) {
33
+ // Build scroll prop metadata in the format Inertia.js expects
34
+ const hasNextPage = value.currentPage < value.totalPages
35
+ const hasPreviousPage = value.currentPage > 1
36
+
37
+ scrollProps[key] = {
38
+ pageName: value.pageName,
39
+ currentPage: value.currentPage,
40
+ previousPage: hasPreviousPage ? value.currentPage - 1 : null,
41
+ nextPage: hasNextPage ? value.currentPage + 1 : null,
42
+ reset: false
43
+ }
44
+ }
45
+ }
46
+
47
+ if (Object.keys(scrollProps).length === 0) {
48
+ return {}
49
+ }
50
+
51
+ return { scrollProps }
52
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inertia-sails",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "The Sails adapter for Inertia.",
5
5
  "main": "index.js",
6
6
  "sails": {