-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce plugins and extract resources as a plugin (#192)
- Loading branch information
Showing
36 changed files
with
873 additions
and
115 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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React from 'react'; | ||
|
||
import { createResource, useResource } from 'react-resource-router'; | ||
|
||
export const homeResource = createResource({ | ||
type: 'home', | ||
getKey: () => 'breedList', | ||
maxAge: 10000, | ||
getData: async () => { | ||
const response = await fetch('https://dog.ceo/api/breeds/image/random'); | ||
const result: { message: string } = await response.json(); | ||
|
||
return result; | ||
}, | ||
}); | ||
|
||
export const Home = () => { | ||
// eslint-disable-next-line | ||
const { data, loading, error } = useResource(homeResource); | ||
|
||
return ( | ||
<div> | ||
<h1>Random Dog</h1> | ||
<section> | ||
{data?.message && <img src={data.message} style={{ width: '400px' }} />} | ||
</section> | ||
</div> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>Example - Hydration</title> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
<script src="bundle.js" type="text/javascript"></script> | ||
</body> | ||
</html> |
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { createMemoryHistory } from 'history'; | ||
import React from 'react'; | ||
import { render } from 'react-dom'; | ||
import { defaultRegistry } from 'react-sweet-state'; | ||
|
||
import { createResourcesPlugin } from '../../src/resources'; | ||
|
||
import { homeRoute } from './routes'; | ||
|
||
import { | ||
Router, | ||
RouteComponent, | ||
createBrowserHistory, | ||
invokePluginLoad, | ||
} from 'react-resource-router'; | ||
|
||
const myHistory = createBrowserHistory(); | ||
|
||
const appRoutes = [homeRoute]; | ||
|
||
const getStateFromServer = async () => { | ||
const resourcesPlugin = createResourcesPlugin({ | ||
resourceData: null, | ||
}); | ||
|
||
invokePluginLoad([resourcesPlugin], { | ||
history: createMemoryHistory({ initialEntries: [location] }), | ||
routes: appRoutes, | ||
basePath: '/hydration-with-plugins', | ||
}); | ||
|
||
const resourceData = await resourcesPlugin.getSerializedResources(); | ||
|
||
// clearing the store | ||
defaultRegistry.stores.clear(); | ||
|
||
return resourceData; | ||
}; | ||
|
||
const main = async () => { | ||
const data = await getStateFromServer(); | ||
const resourcesPlugin = createResourcesPlugin({ | ||
resourceData: data, | ||
}); | ||
|
||
const App = () => { | ||
return ( | ||
<Router | ||
basePath="/hydration-with-plugins" | ||
history={myHistory} | ||
plugins={[resourcesPlugin]} | ||
routes={appRoutes} | ||
> | ||
<RouteComponent /> | ||
</Router> | ||
); | ||
}; | ||
|
||
render(<App />, document.getElementById('root')); | ||
}; | ||
|
||
main(); |
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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Home, homeResource } from './home'; | ||
|
||
export const homeRoute = { | ||
name: 'home', | ||
path: '/', | ||
exact: true, | ||
component: Home, | ||
navigation: null, | ||
resources: [homeResource], | ||
}; |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"main": "../build/cjs/resources.js", | ||
"module": "../build/esm/resources.js", | ||
"types": "../build/esm/resources.d.ts" | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { History, Plugin, Routes } from '../../common/types'; | ||
import { findRouterContext } from '../../common/utils'; | ||
|
||
export const invokePluginLoad = ( | ||
plugins: Plugin[], | ||
{ | ||
routes, | ||
history, | ||
basePath, | ||
}: { | ||
history: History; | ||
routes: Routes; | ||
basePath?: string; | ||
} | ||
) => { | ||
const context = findRouterContext(routes, { | ||
location: history.location, | ||
basePath, | ||
}); | ||
|
||
plugins.forEach(p => { | ||
p.routeLoad?.({ context }); | ||
}); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createMemoryHistory } from 'history'; | ||
|
||
import { invokePluginLoad } from './index'; | ||
|
||
describe('invokePluginLoad', () => { | ||
it('calls each plugin load method', () => { | ||
// (findRouterContext as any).mockReturnValue(firstContextMock); | ||
const pluginOne = { | ||
routeLoad: jest.fn(), | ||
}; | ||
const pluginTwo = { | ||
routeLoad: jest.fn(), | ||
}; | ||
|
||
const pluginThree = { | ||
routeLoad: jest.fn(), | ||
}; | ||
|
||
invokePluginLoad([pluginOne, pluginTwo, pluginThree], { | ||
history: createMemoryHistory(), | ||
routes: [], | ||
}); | ||
|
||
expect(pluginOne.routeLoad).toBeCalled(); | ||
expect(pluginTwo.routeLoad).toBeCalled(); | ||
expect(pluginThree.routeLoad).toBeCalled(); | ||
}); | ||
}); |
Oops, something went wrong.