-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR migrates most actions over to `createAction`. The goal of this is 1. Better typescript support using `.match` which allows us to use a [typescript guard](https://redux-toolkit.js.org/api/createAction#as-a-typescript-type-guard) 2. Easier for users. We previously exposed the const to the user so that they can createAction themselves before using it in a slice. Now they can just import it. For example: ```js import { createSlice } from '@reduxjs/toolkit' import { saveResponse, beforeVisit } from '@thoughtbot/superglue' export const flashSlice = createSlice({ name: 'flash', initialState: {}, extraReducers: (builder) => { builder.addCase(beforeVisit, (state, action) => { return {} }) builder.addCase(saveResponse, (state, action) => { const { page } = action.payload; return { ...state, ...page.slices.flash } }) } }) ```
- Loading branch information
Showing
18 changed files
with
214 additions
and
402 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,80 @@ | ||
export const BEFORE_FETCH = '@@superglue/BEFORE_FETCH' | ||
export const BEFORE_VISIT = '@@superglue/BEFORE_VISIT' | ||
export const BEFORE_REMOTE = '@@superglue/BEFORE_REMOTE' | ||
import { createAction } from '@reduxjs/toolkit' | ||
import { | ||
FetchArgs, | ||
PageKey, | ||
JSONValue, | ||
GraftResponse, | ||
VisitResponse, | ||
} from './types' | ||
import { urlToPageKey } from './utils' | ||
|
||
export const SAVE_RESPONSE = '@@superglue/SAVE_RESPONSE' | ||
export const HANDLE_GRAFT = '@@superglue/HANDLE_GRAFT' | ||
|
||
export const SUPERGLUE_ERROR = '@@superglue/ERROR' | ||
export const GRAFTING_ERROR = '@@superglue/GRAFTING_ERROR' | ||
export const GRAFTING_SUCCESS = '@@superglue/GRAFTING_SUCCESS' | ||
|
||
export const HISTORY_CHANGE = '@@superglue/HISTORY_CHANGE' | ||
export const SET_CSRF_TOKEN = '@@superglue/SET_CSRF_TOKEN' | ||
export const REMOVE_PAGE = '@@superglue/REMOVE_PAGE' | ||
export const COPY_PAGE = '@@superglue/COPY_PAGE' | ||
export const UPDATE_FRAGMENTS = '@@superglue/UPDATE_FRAGMENTS' | ||
export const saveResponse = createAction( | ||
'@@superglue/SAVE_RESPONSE', | ||
({ pageKey, page }: { pageKey: string; page: VisitResponse }) => { | ||
pageKey = urlToPageKey(pageKey) | ||
|
||
return { | ||
payload: { | ||
pageKey, | ||
page, | ||
}, | ||
} | ||
} | ||
) | ||
|
||
export const handleGraft = createAction( | ||
'@@superglue/HANDLE_GRAFT', | ||
({ pageKey, page }: { pageKey: string; page: GraftResponse }) => { | ||
pageKey = urlToPageKey(pageKey) | ||
|
||
return { | ||
payload: { | ||
page, | ||
pageKey, | ||
}, | ||
} | ||
} | ||
) | ||
|
||
export const superglueError = createAction<{ message: string }>( | ||
'@@superglue/ERROR' | ||
) | ||
|
||
export const updateFragments = createAction<{ | ||
changedFragments: Record<string, JSONValue> | ||
}>('@@superglue/UPDATE_FRAGMENTS') | ||
|
||
export const copyPage = createAction<{ from: PageKey; to: PageKey }>( | ||
'@@superglue/COPY_PAGE' | ||
) | ||
|
||
export const removePage = createAction<{ pageKey: PageKey }>( | ||
'@@superglue/REMOVE_PAGE' | ||
) | ||
|
||
export const beforeFetch = createAction<{ fetchArgs: FetchArgs }>( | ||
'@@superglue/BEFORE_FETCH' | ||
) | ||
|
||
export const beforeVisit = createAction<{ | ||
currentPageKey: PageKey | ||
fetchArgs: FetchArgs | ||
}>('@@superglue/BEFORE_VISIT') | ||
|
||
export const beforeRemote = createAction<{ | ||
currentPageKey: PageKey | ||
fetchArgs: FetchArgs | ||
}>('@@superglue/BEFORE_REMOTE') | ||
|
||
export const setCSRFToken = createAction<{ | ||
csrfToken: string | undefined | ||
}>('@@superglue/SET_CSRF_TOKEN') | ||
|
||
export const historyChange = createAction<{ | ||
pathname: string | ||
search: string | ||
hash: string | ||
}>('@@superglue/HISTORY_CHANGE') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.