-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from no-chris/add-router
Add basic front-end routing with universal-router
- Loading branch information
Showing
18 changed files
with
388 additions
and
54 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
Binary file not shown.
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,26 +1,42 @@ | ||
<!doctype html> | ||
<html class="no-js" lang="en_EN"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge"> | ||
<title><%= htmlWebpackPlugin.options.title %></title> | ||
<meta name="description" content="Build chord charts easily for all musicians. The official editor of ChordMark."> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | ||
<link href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Roboto+Mono:400,700" rel="stylesheet"> | ||
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> | ||
<link rel=“icon” href=”favicon.ico” type=“image/x-icon”> | ||
<link rel="manifest" href="manifest.json" /> | ||
<!-- Global site tag (gtag.js) - Google Analytics --> | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-EGKBT2J600"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="x-ua-compatible" content="ie=edge" /> | ||
<title><%= htmlWebpackPlugin.options.title %></title> | ||
<meta | ||
name="description" | ||
content="Build chord charts easily for all musicians. The official editor of ChordMark." | ||
/> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, initial-scale=1, shrink-to-fit=no" | ||
/> | ||
<link | ||
href="https://fonts.googleapis.com/css?family=Open+Sans:400,700|Roboto+Mono:400,700" | ||
rel="stylesheet" | ||
/> | ||
<link | ||
href="https://fonts.googleapis.com/icon?family=Material+Icons" | ||
rel="stylesheet" | ||
/> | ||
<link rel="manifest" href="/manifest.json" /> | ||
<!-- Global site tag (gtag.js) - Google Analytics --> | ||
<script | ||
async | ||
src="https://www.googletagmanager.com/gtag/js?id=G-EGKBT2J600" | ||
></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag() { | ||
dataLayer.push(arguments); | ||
} | ||
gtag('js', new Date()); | ||
|
||
gtag('config', 'G-EGKBT2J600'); | ||
</script> | ||
</head> | ||
<body> | ||
<div id="app" class="theme-dark"></div> | ||
</body> | ||
gtag('config', 'G-EGKBT2J600'); | ||
</script> | ||
</head> | ||
<body> | ||
<div id="app" class="theme-dark"></div> | ||
</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
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,71 @@ | ||
import UniversalRouter from 'universal-router'; | ||
import generateUrls from 'universal-router/generateUrls'; | ||
import qs from 'qs'; | ||
|
||
import renderController from '../renderController'; | ||
|
||
let router; | ||
let getUrl; | ||
|
||
export default { | ||
init(allRoutes) { | ||
const allRoutesWithWrappedActions = allRoutes.map((route) => { | ||
return { | ||
...route, | ||
action: (context) => ({ | ||
Controller: route.action, | ||
params: context.params, | ||
}), | ||
}; | ||
}); | ||
router = new UniversalRouter(allRoutesWithWrappedActions, { | ||
errorHandler(error, context) { | ||
console.error( | ||
`Error: Cannot find route for path: ${context.pathname}` | ||
); | ||
}, | ||
}); | ||
getUrl = generateUrls(router, { | ||
stringifyQueryParams: qs.stringify, | ||
}); | ||
}, | ||
}; | ||
|
||
export function navigateTo(url, shouldPushState = true) { | ||
const parsedUrl = new URL(url, window.location.origin); | ||
|
||
return router | ||
.resolve(parsedUrl.pathname) | ||
.then(({ Controller, params } = {}) => { | ||
if (Controller) { | ||
if (shouldPushState) { | ||
pushState(url); | ||
} | ||
const queryParams = qs.parse(parsedUrl.search, { | ||
ignoreQueryPrefix: true, | ||
}); | ||
renderController(Controller, { | ||
...params, | ||
...queryParams, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export function getLink(routeName, params) { | ||
try { | ||
return getUrl(routeName, params); | ||
} catch (e) { | ||
console.error(e.toString()); | ||
} | ||
} | ||
|
||
function pushState(url) { | ||
window.history.pushState({ url }, null, url); | ||
} | ||
|
||
/* istanbul ignore next */ | ||
window.addEventListener('popstate', () => { | ||
const path = window.location.pathname + window.location.search; | ||
navigateTo(path, false); | ||
}); |
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,13 @@ | ||
import libraryRoutes from './library/routes'; | ||
import songViewRoutes from './songView/routes'; | ||
import Editor from '../controllers/Editor'; | ||
|
||
export default [ | ||
{ | ||
name: 'home', | ||
path: '/', | ||
action: Editor, | ||
}, | ||
...libraryRoutes, | ||
...songViewRoutes, | ||
]; |
34 changes: 34 additions & 0 deletions
34
packages/chord-chart-studio/src/modules/library/controllers/Library.js
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,34 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { getAllTitles } from '../../../db/files/selectors'; | ||
import { navigateTo, getLink } from '../../../core/router'; | ||
|
||
export default function Library() { | ||
const allTitles = useSelector(getAllTitles); | ||
|
||
const allrenderedTitles = allTitles.map((song) => ( | ||
<SongEntry key={song.id} song={song} /> | ||
)); | ||
|
||
return ( | ||
<div> | ||
Full Library | ||
<ul>{allrenderedTitles}</ul> | ||
</div> | ||
); | ||
} | ||
|
||
const SongEntry = ({ song }) => { | ||
const handleClick = (e) => { | ||
e.preventDefault(); | ||
navigateTo(getLink('songView', { songId: song.id })); | ||
}; | ||
return ( | ||
<li> | ||
<a href={`/song/${song.id}`} onClick={handleClick}> | ||
{song.title} | ||
</a> | ||
</li> | ||
); | ||
}; |
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,9 @@ | ||
import Library from './controllers/Library'; | ||
|
||
export default [ | ||
{ | ||
name: 'library', | ||
path: '/library', | ||
action: Library, | ||
}, | ||
]; |
25 changes: 25 additions & 0 deletions
25
packages/chord-chart-studio/src/modules/songView/controllers/SongView.js
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,25 @@ | ||
import React from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
|
||
import { navigateTo, getLink } from '../../../core/router'; | ||
import { getOne } from '../../../db/files/selectors'; | ||
|
||
export default function SongView({ songId }) { | ||
const song = useSelector((state) => getOne(state, songId)); | ||
|
||
const handleClick = (e) => { | ||
e.preventDefault(); | ||
navigateTo(getLink('library')); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<a href="" onClick={handleClick}> | ||
Go back to list | ||
</a> | ||
<br /> | ||
<b>{song.title}</b> | ||
<p>{song.content}</p> | ||
</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,9 @@ | ||
import SongView from './controllers/SongView'; | ||
|
||
export default [ | ||
{ | ||
name: 'songView', | ||
path: '/songView/:songId', | ||
action: SongView, | ||
}, | ||
]; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.