Skip to content

Commit

Permalink
comments fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kholdrex committed Jun 24, 2024
1 parent 3a5ab37 commit c379297
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 40 deletions.
11 changes: 8 additions & 3 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ export function visit(
placeholderKey,
beforeSave = (prevPage, receivedPage) => receivedPage,
revisit = false,
suggestedAction = 'push',
action = 'push',
} = {}
) {
path = withoutBusters(path)
Expand All @@ -175,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 @@ -207,9 +207,14 @@ export function visit(
rsp,
fetchArgs,
}

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

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

if (!rsp.redirected && !isGet) {
meta.suggestedAction = 'replace'
Expand Down
21 changes: 10 additions & 11 deletions superglue/lib/utils/ujs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ export class HandlerBuilder {
const hasRemote = !!node.getAttribute(
this.attributePrefix + '-remote'
)
const hasReplace = !!node.getAttribute(
this.attributePrefix + '-replace'
)

return hasVisit || hasRemote || hasReplace
return hasVisit || hasRemote
}

handleSubmit(event) {
Expand Down Expand Up @@ -95,7 +92,6 @@ export class HandlerBuilder {

visitOrRemote(linkOrForm, url, opts: any = {}) {
let target
let suggestedAction = 'push'

if (linkOrForm.getAttribute(this.attributePrefix + '-visit')) {
target = this.visit
Expand All @@ -105,21 +101,24 @@ 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')) {
target = this.remote
}

if (linkOrForm.getAttribute(this.attributePrefix + '-replace')) {
target = this.visit
suggestedAction = 'replace'
}

if (!target) {
return
} else {
return target(url, { ...opts, suggestedAction })
return target(url, opts)
}
}

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
26 changes: 1 addition & 25 deletions superglue/spec/lib/utils/ujs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,30 +166,7 @@ describe('ujs', () => {
const {onClick} = builder.handlers()
onClick(createFakeRemoteEvent())

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

it('calls visit with replace action if link has data-replace attribute', () => {
const ujsAttributePrefix = 'data'
const visit = jest.fn()
const navigatorRef = {
current: {
navigateTo: () => {}
}
}
const store = {}

const builder = new HandlerBuilder({
ujsAttributePrefix,
store,
visit,
navigatorRef
})

const {onClick} = builder.handlers()
onClick(createFakeReplaceEvent())

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

it('does not call visit on a link that does not have the visit attribute data-visit', () => {
Expand Down Expand Up @@ -383,7 +360,6 @@ describe('ujs', () => {
headers: {
"content-type": null,
},
suggestedAction: 'push',
body: {some: 'Body'}
})
})
Expand Down

0 comments on commit c379297

Please sign in to comment.