Skip to content

Commit de5cfe3

Browse files
committed
feat(inertia): complete v3 template migration
1 parent 2bb0c01 commit de5cfe3

28 files changed

Lines changed: 731 additions & 508 deletions

File tree

package-lock.json

Lines changed: 5 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Utilities for describing how mergeable props should be merged by the client.
3+
*/
4+
5+
function createDefaultMergeOperation() {
6+
return {
7+
direction: 'append',
8+
path: null,
9+
matchOn: null,
10+
isDefault: true
11+
}
12+
}
13+
14+
function normalizeMergeOptions(options) {
15+
return typeof options === 'string' ? { matchOn: options } : options || {}
16+
}
17+
18+
function normalizeMergeTargets(paths, options = {}) {
19+
const normalizedOptions = normalizeMergeOptions(options)
20+
21+
if (paths === null || paths === undefined) {
22+
return [
23+
{
24+
path: null,
25+
matchOn: normalizedOptions.matchOn || null
26+
}
27+
]
28+
}
29+
30+
if (Array.isArray(paths)) {
31+
return paths.map((path) => ({
32+
path: normalizePath(path),
33+
matchOn: resolveTargetMatchOn(path, normalizedOptions)
34+
}))
35+
}
36+
37+
if (typeof paths === 'object') {
38+
return Object.entries(paths).map(([path, matchOn]) => ({
39+
path: normalizePath(path),
40+
matchOn
41+
}))
42+
}
43+
44+
return [
45+
{
46+
path: normalizePath(paths),
47+
matchOn: resolveTargetMatchOn(paths, normalizedOptions)
48+
}
49+
]
50+
}
51+
52+
function normalizePath(path) {
53+
return path === '' ? null : path
54+
}
55+
56+
function resolveTargetMatchOn(path, options) {
57+
if (!options.matchOn) return null
58+
if (typeof options.matchOn === 'object') return options.matchOn[path] || null
59+
return options.matchOn
60+
}
61+
62+
function resolvePropPath(key, path) {
63+
return path ? `${key}.${path}` : key
64+
}
65+
66+
function unique(values) {
67+
return [...new Set(values)]
68+
}
69+
70+
module.exports = {
71+
createDefaultMergeOperation,
72+
normalizeMergeOptions,
73+
normalizeMergeTargets,
74+
resolvePropPath,
75+
unique
76+
}

packages/inertia-sails/lib/props/mergeable-prop.js

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
const {
2+
createDefaultMergeOperation,
3+
normalizeMergeOptions,
4+
normalizeMergeTargets
5+
} = require('./merge-targets')
6+
17
/**
28
* MergeableProp - Base class for props that can be merged during partial reloads.
39
*
@@ -27,12 +33,7 @@ module.exports = class MergeableProp {
2733
this.shouldMerge = true
2834
this.shouldDeepMerge = false
2935
if (this.mergeOperations.length === 0) {
30-
this.mergeOperations.push({
31-
direction: 'append',
32-
path: null,
33-
matchOn: null,
34-
isDefault: true
35-
})
36+
this.mergeOperations.push(createDefaultMergeOperation())
3637
}
3738
return this
3839
}
@@ -83,14 +84,13 @@ module.exports = class MergeableProp {
8384
}
8485

8586
_addMergeOperations(direction, paths, options) {
86-
const normalizedOptions =
87-
typeof options === 'string' ? { matchOn: options } : options || {}
87+
const normalizedOptions = normalizeMergeOptions(options)
8888

8989
this.shouldMerge = true
9090
this.shouldDeepMerge = false
9191
this._clearDefaultMergeOperation()
9292

93-
this._normalizeMergeTargets(paths, normalizedOptions).forEach((target) => {
93+
normalizeMergeTargets(paths, normalizedOptions).forEach((target) => {
9494
this.mergeOperations.push({
9595
direction,
9696
path: target.path,
@@ -109,47 +109,4 @@ module.exports = class MergeableProp {
109109
this.mergeOperations = []
110110
}
111111
}
112-
113-
_normalizeMergeTargets(paths, options) {
114-
if (paths === null || paths === undefined) {
115-
return [
116-
{
117-
path: null,
118-
matchOn: options.matchOn || null
119-
}
120-
]
121-
}
122-
123-
if (Array.isArray(paths)) {
124-
return paths.map((path) => ({
125-
path: this._normalizePath(path),
126-
matchOn: this._resolveMatchOn(path, options)
127-
}))
128-
}
129-
130-
if (typeof paths === 'object') {
131-
return Object.entries(paths).map(([path, matchOn]) => ({
132-
path: this._normalizePath(path),
133-
matchOn
134-
}))
135-
}
136-
137-
return [
138-
{
139-
path: this._normalizePath(paths),
140-
matchOn: this._resolveMatchOn(paths, options)
141-
}
142-
]
143-
}
144-
145-
_normalizePath(path) {
146-
return path === '' ? null : path
147-
}
148-
149-
_resolveMatchOn(path, options) {
150-
if (!options.matchOn) return null
151-
if (typeof options.matchOn === 'object')
152-
return options.matchOn[path] || null
153-
return options.matchOn
154-
}
155112
}

packages/inertia-sails/lib/props/resolve-merge-props.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const {
33
RESET
44
} = require('../helpers/inertia-headers')
55
const MergeableProp = require('./mergeable-prop')
6+
const { resolvePropPath, unique } = require('./merge-targets')
67
const ScrollProp = require('./scroll-prop')
78

89
/**
@@ -79,11 +80,3 @@ module.exports = function resolveMergeProps(req, pageProps) {
7980

8081
return result
8182
}
82-
83-
function resolvePropPath(key, path) {
84-
return path ? `${key}.${path}` : key
85-
}
86-
87-
function unique(values) {
88-
return [...new Set(values)]
89-
}

packages/inertia-sails/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "inertia-sails",
3-
"version": "1.3.3",
3+
"version": "1.4.0",
44
"description": "The Sails adapter for Inertia.",
55
"main": "index.js",
66
"sails": {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
const { describe, it } = require('node:test')
2+
const assert = require('node:assert/strict')
3+
const {
4+
createDefaultMergeOperation,
5+
normalizeMergeOptions,
6+
normalizeMergeTargets,
7+
resolvePropPath,
8+
unique
9+
} = require('../../lib/props/merge-targets')
10+
11+
describe('merge-targets', function () {
12+
it('creates the default root append operation', function () {
13+
assert.deepEqual(createDefaultMergeOperation(), {
14+
direction: 'append',
15+
path: null,
16+
matchOn: null,
17+
isDefault: true
18+
})
19+
})
20+
21+
it('normalizes string options as match-on metadata', function () {
22+
assert.deepEqual(normalizeMergeOptions('id'), { matchOn: 'id' })
23+
})
24+
25+
it('normalizes root targets', function () {
26+
assert.deepEqual(normalizeMergeTargets(null, { matchOn: 'id' }), [
27+
{
28+
path: null,
29+
matchOn: 'id'
30+
}
31+
])
32+
})
33+
34+
it('normalizes array targets with shared match-on metadata', function () {
35+
assert.deepEqual(normalizeMergeTargets(['users', 'messages'], 'id'), [
36+
{
37+
path: 'users',
38+
matchOn: 'id'
39+
},
40+
{
41+
path: 'messages',
42+
matchOn: 'id'
43+
}
44+
])
45+
})
46+
47+
it('normalizes object targets as path-to-match maps', function () {
48+
assert.deepEqual(
49+
normalizeMergeTargets({
50+
'users.data': 'id',
51+
messages: 'uuid'
52+
}),
53+
[
54+
{
55+
path: 'users.data',
56+
matchOn: 'id'
57+
},
58+
{
59+
path: 'messages',
60+
matchOn: 'uuid'
61+
}
62+
]
63+
)
64+
})
65+
66+
it('resolves prop paths and unique values', function () {
67+
assert.equal(resolvePropPath('users', 'data'), 'users.data')
68+
assert.equal(resolvePropPath('users', null), 'users')
69+
assert.deepEqual(unique(['users', 'users', 'messages']), [
70+
'users',
71+
'messages'
72+
])
73+
})
74+
})

templates/ascent-react/assets/js/pages/billing/pricing.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Head, Link } from '@inertiajs/react'
22
import AppLayout from '@/layouts/AppLayout.jsx'
33
import { useState } from 'react'
44

5-
Pricing.layout = (page) => <AppLayout children={page} />
5+
Pricing.layout = AppLayout
66
export default function Pricing({ plans }) {
77
const [billingCycle, setBillingCycle] = useState('monthly')
88

templates/ascent-react/assets/js/pages/blog.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Head, Link } from '@inertiajs/react'
22
import AppLayout from '@/layouts/AppLayout.jsx'
33

4-
Blog.layout = (page) => <AppLayout children={page} />
4+
Blog.layout = AppLayout
55

66
export default function Blog({ appName, blogPosts }) {
77
return (

templates/ascent-react/assets/js/pages/dashboard/index.jsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import { Link, Head, usePage } from '@inertiajs/react'
22
import DashboardLayout from '@/layouts/DashboardLayout.jsx'
33

4-
Dashboard.layout = (page) => (
5-
<DashboardLayout title="Dashboard" maxWidth="wide">
6-
{page}
7-
</DashboardLayout>
8-
)
4+
Dashboard.layout = [DashboardLayout, { title: 'Dashboard', maxWidth: 'wide' }]
95
export default function Dashboard() {
106
const page = usePage()
117
const loggedInUser = page.props.loggedInUser

templates/ascent-react/assets/js/pages/features.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Head, Link } from '@inertiajs/react'
22
import AppLayout from '@/layouts/AppLayout.jsx'
33

4-
Features.layout = (page) => <AppLayout children={page} />
4+
Features.layout = AppLayout
55
export default function Features() {
66
return (
77
<>

0 commit comments

Comments
 (0)