Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data-sg-replace functionality #80

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export function visit(
placeholderKey,
beforeSave = (prevPage, receivedPage) => receivedPage,
revisit = false,
action = 'push',
jho406 marked this conversation as resolved.
Show resolved Hide resolved
} = {}
) {
path = withoutBusters(path)
Expand All @@ -174,7 +175,7 @@ export function visit(

if (!placeholderKey && hasPropsAt(path)) {
console.warn(
`visit was called with props_at param in the path ${path}, this will be ignore unless you provide a placeholder.`
`visit was called with props_at param in the path ${path}, this will be ignored unless you provide a placeholder.`
)
path = removePropsAt(path)
}
Expand Down Expand Up @@ -206,9 +207,15 @@ export function visit(
rsp,
fetchArgs,
}

const isGet = fetchArgs[1].method === 'GET'

meta.suggestedAction = 'push'
if (action !== undefined) {
meta.suggestedAction = action
} else {
meta.suggestedAction = 'push'
}

jho406 marked this conversation as resolved.
Show resolved Hide resolved
if (!rsp.redirected && !isGet) {
meta.suggestedAction = 'replace'
}
Expand Down
8 changes: 8 additions & 0 deletions superglue/lib/utils/ujs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ export class HandlerBuilder {
if (placeholderKey) {
opts.placeholderKey = urlToPageKey(placeholderKey)
}

if (
linkOrForm.getAttribute(this.attributePrefix + '-replace')
) {
opts.suggestedAction = 'replace'
} else {
opts.suggestedAction = 'push'
}
}

if (linkOrForm.getAttribute(this.attributePrefix + '-remote')) {
Expand Down
2 changes: 1 addition & 1 deletion superglue/spec/lib/action_creators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ Consider using data-sg-visit, the visit function, or redirect_back.`
const expectedFetchUrl = '/first?props_at=foo'
store.dispatch(visit(expectedFetchUrl)).then((meta) => {
expect(console.warn).toHaveBeenCalledWith(
'visit was called with props_at param in the path /first?props_at=foo, this will be ignore unless you provide a placeholder.'
'visit was called with props_at param in the path /first?props_at=foo, this will be ignored unless you provide a placeholder.'
)
done()
})
Expand Down
40 changes: 32 additions & 8 deletions superglue/spec/lib/utils/ujs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ describe('ujs', () => {
}
}

function createFakeReplaceEvent() {
return {
preventDefault: () => {},
target: {
nodeName: 'A',
parentNode: 'DIV',
href: '/foo',
getAttribute: (attr) => {
if(attr === 'href') {
return '/foo'
}
if(attr === 'data-replace') {
return true
}
}
}
}
}

describe('onClick', () => {
it('calls visit on a valid link', () => {
const ujsAttributePrefix = 'data'
Expand All @@ -89,7 +108,7 @@ describe('ujs', () => {
const {onClick} = builder.handlers()
onClick(createFakeEvent())

expect(visit).toHaveBeenCalledWith('/foo', {method: 'GET'})
expect(visit).toHaveBeenCalledWith('/foo', {method: 'GET', suggestedAction: 'push'})
})

it('calls visit with a placeholder when props_at is present on a valid link', () => {
Expand Down Expand Up @@ -120,7 +139,11 @@ describe('ujs', () => {
const {onClick} = builder.handlers()
onClick(createFakeVisitGraftEvent())

expect(visit).toHaveBeenCalledWith('/foo?props_at=data.hello', {method: 'GET', placeholderKey: '/current'})
expect(visit).toHaveBeenCalledWith('/foo?props_at=data.hello', {
method: 'GET',
placeholderKey: '/current',
suggestedAction: 'push',
})
})

it('calls remote if a link is enabled with remote', () => {
Expand All @@ -146,7 +169,7 @@ describe('ujs', () => {
expect(remote).toHaveBeenCalledWith('/foo', {method: 'GET'})
})

it('does not call visit on an link does not have the visit attribute data-visit', () => {
it('does not call visit on a link that does not have the visit attribute data-visit', () => {
const store = {}
const ujsAttributePrefix = 'data'
const visit = jest.fn()
Expand Down Expand Up @@ -176,7 +199,7 @@ describe('ujs', () => {
expect(visit).not.toHaveBeenCalledWith('/foo', {})
})

it('does not call visit on an non-standard link', () => {
it('does not call visit on a non-standard link', () => {
const store = {}
const ujsAttributePrefix = 'data'
const visit = jest.fn()
Expand Down Expand Up @@ -227,7 +250,7 @@ describe('ujs', () => {

fakeEvent = createFakeEvent()
onClick(fakeEvent)
expect(visit).toHaveBeenCalledWith('/foo', {method: 'GET'})
expect(visit).toHaveBeenCalledWith('/foo', {method: 'GET', suggestedAction: 'push'})
})
})

Expand Down Expand Up @@ -274,7 +297,7 @@ describe('ujs', () => {
}
}

it('succssfully posts a form with a visit attribute', () => {
it('successfully posts a form with a visit attribute', () => {
const store = {}
const ujsAttributePrefix = 'data'
const visit = jest.fn()
Expand Down Expand Up @@ -303,11 +326,12 @@ describe('ujs', () => {
headers: {
"content-type": null,
},
suggestedAction: 'push',
body: {some: 'Body'}
})
})

it('succssfully posts a form with a remote attribut', () => {
it('successfully posts a form with a remote attribute', () => {
const store = {}
const ujsAttributePrefix = 'data'
const remote = jest.fn()
Expand Down Expand Up @@ -340,7 +364,7 @@ describe('ujs', () => {
})
})

it('does not posts a form without a visit attribute', () => {
it('does not post a form without a visit attribute', () => {
const store = {}
const ujsAttributePrefix = 'data'
const visit = jest.fn()
Expand Down
Loading