Skip to content

Commit

Permalink
Rename suggestedAction to navigationAction
Browse files Browse the repository at this point in the history
  • Loading branch information
jho406 committed Dec 4, 2024
1 parent 90956fc commit dab6375
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docs/recipes/progress-bar.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function buildVisitAndRemote(ref, store) {
}

ref.current.navigateTo(meta.pageKey, {
action: meta.suggestedAction,
action: meta.navigationAction,
})

return meta
Expand Down
4 changes: 2 additions & 2 deletions docs/reference/components.Nav.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ passes a `navigateTo` to all page components.

##### navigateTo()

> **navigateTo**(`path`: `string`, `options`: \{`action`: [`SuggestedAction`](types.md#suggestedaction-1);`ownProps`: `Record`\<`string`, `unknown`\>; \}): `boolean`
> **navigateTo**(`path`: `string`, `options`: \{`action`: [`NavigationAction`](types.md#navigationaction-1);`ownProps`: `Record`\<`string`, `unknown`\>; \}): `boolean`
Passed to every page component. Manually navigate using pages that exists
in the store and restores scroll position. This is what [Visit](types.requests.md#visit) in
Expand All @@ -35,7 +35,7 @@ call to `visit` or `remote`.
| ------ | ------ | ------ |
| `path` | `string` | |
| `options` | `object` | when `none`, immediately returns `false` |
| `options.action` | [`SuggestedAction`](types.md#suggestedaction-1) | - |
| `options.action` | [`NavigationAction`](types.md#navigationaction-1) | - |
| `options.ownProps` | `Record`\<`string`, `unknown`\> | - |

###### Returns
Expand Down
14 changes: 7 additions & 7 deletions docs/reference/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ Helpful props passed to the page component.
| Property | Type | Description | Defined in |
| ------ | ------ | ------ | ------ |
| <a id="pagekey-5" name="pagekey-5"></a> `pageKey` | `string` | the pagekey of the current page | [lib/types/index.ts:242](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L242) |
| <a id="navigateto" name="navigateto"></a> `navigateTo` | (`path`: `string`, `options`: \{`action`: [`SuggestedAction`](types.md#suggestedaction-1);`ownProps`: `Record`\<`string`, `unknown`\>; \}) => `boolean` | - | [lib/types/index.ts:243](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L243) |
| <a id="navigateto" name="navigateto"></a> `navigateTo` | (`path`: `string`, `options`: \{`action`: [`NavigationAction`](types.md#navigationaction-1);`ownProps`: `Record`\<`string`, `unknown`\>; \}) => `boolean` | - | [lib/types/index.ts:243](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L243) |
| <a id="visit-1" name="visit-1"></a> `visit` | [`Visit`](types.requests.md#visit) | - | [lib/types/index.ts:244](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L244) |
| <a id="remote-1" name="remote-1"></a> `remote` | [`Remote`](types.requests.md#remote) | - | [lib/types/index.ts:245](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L245) |

Expand All @@ -427,7 +427,7 @@ navigation.
| <a id="fetchargs-3" name="fetchargs-3"></a> `fetchArgs` | [`FetchArgs`](types.md#fetchargs-4) | The original args passed to fetch. | [lib/types/index.ts:277](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L277) |
| <a id="componentidentifier-1" name="componentidentifier-1"></a> `componentIdentifier` | `string` | The [ComponentIdentifier](types.md#componentidentifier-2) extracted from the response. | [lib/types/index.ts:279](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L279) |
| <a id="needsrefresh" name="needsrefresh"></a> `needsRefresh` | `boolean` | `true` when assets locally are detected to be out of date | [lib/types/index.ts:281](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L281) |
| <a id="suggestedaction" name="suggestedaction"></a> `suggestedAction?` | [`SuggestedAction`](types.md#suggestedaction-1) | The [SuggestedAction](types.md#suggestedaction-1). This can be used for navigation. | [lib/types/index.ts:283](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L283) |
| <a id="navigationaction" name="navigationaction"></a> `navigationAction?` | [`NavigationAction`](types.md#navigationaction-1) | The [NavigationAction](types.md#navigationaction-1). This can be used for navigation. | [lib/types/index.ts:283](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/index.ts#L283) |

***

Expand Down Expand Up @@ -587,13 +587,13 @@ When the page already exists in the store:
***
<a id="suggestedaction-1" name="suggestedaction-1"></a>
<a id="navigationaction-1" name="navigationaction-1"></a>
### SuggestedAction
### NavigationAction
> **SuggestedAction**: `"push"` \| `"replace"` \| `"none"`
> **NavigationAction**: `"push"` \| `"replace"` \| `"none"`
A SuggestedAction is used to tell Superglue to history.push, history.replace
A NavigationAction is used to tell Superglue to history.push, history.replace
or do nothing.
#### Defined in
Expand All @@ -606,7 +606,7 @@ or do nothing.
### NavigationAction
> **NavigationAction**: [`SuggestedAction`](types.md#suggestedaction-1)
> **NavigationAction**: [`NavigationAction`](types.md#navigationaction-1)
#### Defined in
Expand Down
2 changes: 1 addition & 1 deletion docs/reference/types.requests.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Options for Visit
| Property | Type | Description | Inherited from | Defined in |
| ------ | ------ | ------ | ------ | ------ |
| <a id="placeholderkey" name="placeholderkey"></a> `placeholderKey?` | `string` | When present, Superglue will use the page state located at that pageKey and optimistally navigates to it as the next page's state while the requests resolves. | - | [lib/types/requests.ts:36](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L36) |
| <a id="revisit" name="revisit"></a> `revisit?` | `boolean` | When `true` and the request method is a GET, changes the `suggestionAction` of the Meta object to `none` so that Superglue does nothing to window.history. When the GET response was redirected, changes `suggestedAction` to `replace` | - | [lib/types/requests.ts:43](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L43) |
| <a id="revisit" name="revisit"></a> `revisit?` | `boolean` | When `true` and the request method is a GET, changes the `suggestionAction` of the Meta object to `none` so that Superglue does nothing to window.history. When the GET response was redirected, changes `navigationAction` to `replace` | - | [lib/types/requests.ts:43](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L43) |
| <a id="method" name="method"></a> `method?` | `string` | The HTTP method | `BaseProps.method` | [lib/types/requests.ts:67](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L67) |
| <a id="body" name="body"></a> `body?` | `BodyInit` | The HTTP body | `BaseProps.body` | [lib/types/requests.ts:69](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L69) |
| <a id="headers" name="headers"></a> `headers?` | \{\} | The HTTP headers | `BaseProps.headers` | [lib/types/requests.ts:71](https://github.com/thoughtbot/superglue/blob/be017596661ef6ab66e199643ed384e7715f12ba/superglue/lib/types/requests.ts#L71) |
Expand Down
16 changes: 8 additions & 8 deletions superglue/lib/action_creators/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
Dispatch,
RemoteCreator,
VisitCreator,
SuggestedAction,
NavigationAction,
} from '../types'

function handleFetchErr(
Expand Down Expand Up @@ -205,7 +205,7 @@ to the same page.

const meta = buildMeta(pageKey, json, superglue, rsp, fetchArgs)

meta.suggestedAction = calculateNavAction(
meta.navigationAction = calculateNavAction(
meta,
rsp,
isGet,
Expand All @@ -229,23 +229,23 @@ function calculateNavAction(
currentPageKey: string,
revisit: boolean
) {
let suggestedAction: SuggestedAction = 'push'
let navigationAction: NavigationAction = 'push'
if (!rsp.redirected && !isGet) {
suggestedAction = 'replace'
navigationAction = 'replace'
}
const isSamePage = pageKey == currentPageKey
if (isSamePage) {
suggestedAction = 'none'
navigationAction = 'none'
}
if (revisit && isGet) {
if (rsp.redirected) {
suggestedAction = 'replace'
navigationAction = 'replace'
} else {
suggestedAction = 'none'
navigationAction = 'none'
}
}

return suggestedAction
return navigationAction
}

function calculatePageKey(
Expand Down
4 changes: 2 additions & 2 deletions superglue/lib/components/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const NavigationProvider = forwardRef(function NavigationProvider(
)
}

if (!!meta && meta.suggestedAction === 'none') {
if (!!meta && meta.navigationAction === 'none') {
setActivePage({ pageKey, ownProps: {} })
scrollTo(posX, posY)
}
Expand All @@ -129,7 +129,7 @@ const NavigationProvider = forwardRef(function NavigationProvider(
)
}

if (!!meta && meta.suggestedAction === 'none') {
if (!!meta && meta.navigationAction === 'none') {
setActivePage({ pageKey, ownProps: {} })
scrollTo(posX, posY)
}
Expand Down
13 changes: 6 additions & 7 deletions superglue/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export type PageKey = string
* When the page already exists in the store:
* - `fromCacheOnly` - Use the cached page that exists on the store, only.
* - `revisitOnly` - Ignore the cache and make a request for the latest page. If
* the response was 200, the {@link SuggestedAction} would be `none` as we don't want
* to push into history. If the response was redirected, the {@link SuggestedAction} would be set to
* the response was 200, the {@link NavigationAction} would be `none` as we don't want
* to push into history. If the response was redirected, the {@link NavigationAction} would be set to
* `replace`.
* - `fromCacheAndRevisitInBackground` - Use the cache version of the page so
* superglue can optimistically navigate to it, then make an additional request
Expand All @@ -45,11 +45,10 @@ export type RestoreStrategy =
| 'fromCacheAndRevisitInBackground'

/**
* A SuggestedAction is used to tell Superglue to history.push, history.replace
* A NavigationAction is used to tell Superglue to history.push, history.replace
* or do nothing.
*/
export type SuggestedAction = 'push' | 'replace' | 'none'
export type NavigationAction = SuggestedAction
export type NavigationAction = 'push' | 'replace' | 'none'

/**
* An identifier that Superglue will uses to determine which page component to render
Expand Down Expand Up @@ -289,8 +288,8 @@ export interface Meta {
componentIdentifier: ComponentIdentifier
/** `true` when assets locally are detected to be out of date */
needsRefresh: boolean
/** The {@link SuggestedAction}. This can be used for navigation.*/
suggestedAction?: SuggestedAction
/** The {@link NavigationAction}. This can be used for navigation.*/
navigationAction?: NavigationAction
}

// I can do Visit['props'] or better yet Visit['options']
Expand Down
2 changes: 1 addition & 1 deletion superglue/lib/types/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export interface VisitProps extends Omit<BaseProps, 'signal'> {
* When `true` and the request method is a GET, changes the
* `suggestionAction` of the Meta object to `none` so that Superglue does
* nothing to window.history.
* When the GET response was redirected, changes `suggestedAction` to `replace`
* When the GET response was redirected, changes `navigationAction` to `replace`
*/
revisit?: boolean
}
Expand Down
14 changes: 7 additions & 7 deletions superglue/spec/lib/NavComponent.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,13 @@ describe('Nav', () => {
const scrollTo = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {})
const suggestedAction = 'none'
const navigationAction = 'none'

const fakeVisit = vi.fn((...args) => {
return {
then: vi.fn((fn) => {
expect(scrollTo).not.toHaveBeenCalled()
fn({ suggestedAction })
fn({ navigationAction })
expect(scrollTo).toHaveBeenCalledWith(5, 5)
}),
}
Expand All @@ -351,7 +351,7 @@ describe('Nav', () => {
expect(fakeVisit).toHaveBeenCalledWith('/home', { revisit: true })
})

it('revisits the page and skips scroll when redirected (suggestedAction is not "none")', () => {
it('revisits the page and skips scroll when redirected (navigationAction is not "none")', () => {
const history = createMemoryHistory({})
history.replace('/home', {
superglue: true,
Expand Down Expand Up @@ -385,13 +385,13 @@ describe('Nav', () => {
const scrollTo = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {})
const suggestedAction = 'push'
const navigationAction = 'push'

const fakeVisit = vi.fn((...args) => {
return {
then: vi.fn((fn) => {
// expect(scrollTo).not.toHaveBeenCalled()
fn({ suggestedAction })
fn({ navigationAction })
expect(scrollTo).not.toHaveBeenCalled()
}),
}
Expand Down Expand Up @@ -451,7 +451,7 @@ describe('Nav', () => {
const scrollTo = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {})
const suggestedAction = 'none'
const navigationAction = 'none'

const fakeVisit = vi.fn()

Expand Down Expand Up @@ -508,7 +508,7 @@ describe('Nav', () => {
const scrollTo = vi
.spyOn(window, 'scrollTo')
.mockImplementation(() => {})
const suggestedAction = 'none'
const navigationAction = 'none'

const fakeVisit = vi.fn((...args) => {
expect(scrollTo).toHaveBeenCalledWith(5, 5)
Expand Down
12 changes: 6 additions & 6 deletions superglue/spec/lib/action_creators.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1297,7 +1297,7 @@ describe('action creators', () => {
}))

describe('when initiated with a revisit indicator', () => {
it('returns a meta with suggestedAction of "replace" if was redirected', () => {
it('returns a meta with navigationAction of "replace" if was redirected', () => {
const initialState = {
pages: {},
superglue: {
Expand All @@ -1321,11 +1321,11 @@ describe('action creators', () => {
.dispatch(visit('/redirecting_url', { revisit: true }))
.then((meta) => {
expect(meta.redirected).toEqual(true)
expect(meta.suggestedAction).toEqual('replace')
expect(meta.navigationAction).toEqual('replace')
})
})

it('returns a meta with suggestedAction of "none" if was not redirected', () => {
it('returns a meta with navigationAction of "none" if was not redirected', () => {
const initialState = {
pages: {},
superglue: {
Expand All @@ -1341,12 +1341,12 @@ describe('action creators', () => {
.dispatch(visit('/first', { revisit: true }))
.then((meta) => {
expect(meta.redirected).toEqual(false)
expect(meta.suggestedAction).toEqual('none')
expect(meta.navigationAction).toEqual('none')
})
})
})

it('returns a meta with suggestedAction of "none" if the next page has the same pageKey as the current page', () => {
it('returns a meta with navigationAction of "none" if the next page has the same pageKey as the current page', () => {
const initialState = {
pages: {},
superglue: {
Expand All @@ -1361,7 +1361,7 @@ describe('action creators', () => {

return store.dispatch(visit('/same_page')).then((meta) => {
expect(meta.redirected).toEqual(false)
expect(meta.suggestedAction).toEqual('none')
expect(meta.navigationAction).toEqual('none')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ export function buildVisitAndRemote(ref, store) {
/**
* Your first expanded UJS option, `data-sg-replace`
*
* This option overrides the `suggestedAction` to allow a link click or
* This option overrides the `navigationAction` to allow a link click or
* a form submission to replace history instead of the usual push.
*/
const navigatonAction = !!dataset.sgReplace ? "replace" : meta.suggestedAction
const navigatonAction = !!dataset.sgReplace ? "replace" : meta.navigationAction
ref.current.navigateTo(meta.pageKey, {
action: navigatonAction,
})
Expand Down

0 comments on commit dab6375

Please sign in to comment.