提交 99a6a60d 编写于 作者: J Jason Park 提交者: Jason

Rename api 'directory' to 'hierarchy'

上级 e8b6b0fe
[submodule "src/backend/public/algorithms"]
path = src/backend/public/algorithms
url = git@github.com:algorithm-visualizer/algorithms.git
...@@ -8,6 +8,10 @@ const { ...@@ -8,6 +8,10 @@ const {
GITHUB_CLIENT_ID, GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET, GITHUB_CLIENT_SECRET,
GITHUB_BOT_USERNAME,
GITHUB_BOT_PASSWORD,
GITHUB_ORG = 'algorithm-visualizer',
GITHUB_REPO_ALGORITHMS = 'algorithms',
} = process.env; } = process.env;
const __PROD__ = NODE_ENV === 'production'; const __PROD__ = NODE_ENV === 'production';
...@@ -18,6 +22,14 @@ const proxyPort = parseInt(PROXY_PORT); ...@@ -18,6 +22,14 @@ const proxyPort = parseInt(PROXY_PORT);
const githubClientId = GITHUB_CLIENT_ID; const githubClientId = GITHUB_CLIENT_ID;
const githubClientSecret = GITHUB_CLIENT_SECRET; const githubClientSecret = GITHUB_CLIENT_SECRET;
const githubBotAuth = {
username: GITHUB_BOT_USERNAME,
password: GITHUB_BOT_PASSWORD,
};
const githubOrg = GITHUB_ORG;
const githubRepos = {
algorithms: GITHUB_REPO_ALGORITHMS
};
const builtPath = path.resolve(__dirname, 'built'); const builtPath = path.resolve(__dirname, 'built');
const frontendBuiltPath = path.resolve(builtPath, 'frontend'); const frontendBuiltPath = path.resolve(builtPath, 'frontend');
...@@ -36,6 +48,9 @@ module.exports = { ...@@ -36,6 +48,9 @@ module.exports = {
proxyPort, proxyPort,
githubClientId, githubClientId,
githubClientSecret, githubClientSecret,
githubBotAuth,
githubOrg,
githubRepos,
frontendBuiltPath, frontendBuiltPath,
backendBuiltPath, backendBuiltPath,
frontendSrcPath, frontendSrcPath,
......
...@@ -21,7 +21,7 @@ const response = (req, res, next) => { ...@@ -21,7 +21,7 @@ const response = (req, res, next) => {
const { access_token } = response.data; const { access_token } = response.data;
res.cookie('access_token', access_token); res.cookie('access_token', access_token);
res.redirect('/'); res.redirect('/');
}); }).catch(next);
}; };
const destroy = (req, res, next) => { const destroy = (req, res, next) => {
......
...@@ -5,11 +5,11 @@ import { NotFoundError } from '/common/error'; ...@@ -5,11 +5,11 @@ import { NotFoundError } from '/common/error';
const router = express.Router(); const router = express.Router();
const getPath = (...args) => path.resolve(__dirname, '..', '..', '..', 'algorithm', ...args); const getPath = (...args) => path.resolve(__dirname, '..', 'public', 'algorithms', ...args);
const createKey = name => name.toLowerCase().replace(/ /g, '-');
const list = dirPath => fs.readdirSync(dirPath).filter(filename => !filename.startsWith('.'));
const readCategories = () => { const cacheHierarchy = () => {
const createKey = name => name.toLowerCase().replace(/ /g, '-');
const list = dirPath => fs.readdirSync(dirPath).filter(filename => !filename.startsWith('.'));
const getCategory = categoryName => { const getCategory = categoryName => {
const categoryKey = createKey(categoryName); const categoryKey = createKey(categoryName);
const categoryPath = getPath(categoryName); const categoryPath = getPath(categoryName);
...@@ -33,16 +33,16 @@ const readCategories = () => { ...@@ -33,16 +33,16 @@ const readCategories = () => {
return list(getPath()).map(getCategory); return list(getPath()).map(getCategory);
}; };
const categories = readCategories(); const hierarchy = cacheHierarchy();
const getCategories = (req, res, next) => { const getHierarchy = (req, res, next) => {
res.json({ categories }); res.json({ hierarchy });
}; };
const getFile = (req, res, next) => { const getFile = (req, res, next) => {
const { categoryKey, algorithmKey, fileName } = req.params; const { categoryKey, algorithmKey, fileName } = req.params;
const category = categories.find(category => category.key === categoryKey); const category = hierarchy.find(category => category.key === categoryKey);
if (!category) return next(new NotFoundError()); if (!category) return next(new NotFoundError());
const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey); const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey);
if (!algorithm) return next(new NotFoundError()); if (!algorithm) return next(new NotFoundError());
...@@ -53,7 +53,7 @@ const getFile = (req, res, next) => { ...@@ -53,7 +53,7 @@ const getFile = (req, res, next) => {
}; };
router.route('/') router.route('/')
.get(getCategories); .get(getHierarchy);
router.route('/:categoryKey/:algorithmKey/:fileName') router.route('/:categoryKey/:algorithmKey/:fileName')
.get(getFile); .get(getFile);
......
import express from 'express'; import express from 'express';
import { AuthorizationError, NotFoundError, PermissionError } from '/common/error'; import { AuthorizationError, NotFoundError, PermissionError } from '/common/error';
import auth from './auth'; import auth from './auth';
import directory from './directory'; import hierarchy from './hierarchy';
import wiki from './wiki'; import wiki from './wiki';
const router = new express.Router(); const router = new express.Router();
router.use('/auth', auth); router.use('/auth', auth);
router.use('/directory', directory); router.use('/hierarchy', hierarchy);
router.use('/wiki', wiki); router.use('/wiki', wiki);
router.use((req, res, next) => next(new NotFoundError())); router.use((req, res, next) => next(new NotFoundError()));
router.use((err, req, res, next) => { router.use((err, req, res, next) => {
......
Subproject commit 417e8c9ffe9bf8411f0099ea01098c0919468a07
...@@ -2,7 +2,7 @@ import Promise from 'bluebird'; ...@@ -2,7 +2,7 @@ import Promise from 'bluebird';
import axios from 'axios'; import axios from 'axios';
import GitHub from 'github-api'; import GitHub from 'github-api';
let gitHub = new GitHub(); let gh = new GitHub();
axios.interceptors.response.use(response => { axios.interceptors.response.use(response => {
return response.data; return response.data;
...@@ -49,9 +49,9 @@ const PUT = URL => { ...@@ -49,9 +49,9 @@ const PUT = URL => {
}); });
}; };
const DirectoryApi = { const HierarchyApi = {
getCategories: GET('/directory'), getHierarchy: GET('/hierarchy'),
getFile: GET('/directory/:categoryKey/:algorithmKey/:fileName'), getFile: GET('/hierarchy/:categoryKey/:algorithmKey/:fileName'),
}; };
const WikiApi = { const WikiApi = {
...@@ -60,12 +60,12 @@ const WikiApi = { ...@@ -60,12 +60,12 @@ const WikiApi = {
}; };
const GitHubApi = { const GitHubApi = {
auth: token => gitHub = new GitHub({ token }), auth: token => gh = new GitHub({ token }),
getProfile: () => gitHub.getUser().getProfile(), getProfile: () => gh.getUser().getProfile(),
}; };
export { export {
DirectoryApi, HierarchyApi,
WikiApi, WikiApi,
GitHubApi, GitHubApi,
}; };
\ No newline at end of file
...@@ -6,7 +6,7 @@ import { Workspace, WSSectionContainer, WSTabContainer } from '/workspace/compon ...@@ -6,7 +6,7 @@ import { Workspace, WSSectionContainer, WSTabContainer } from '/workspace/compon
import { Section } from '/workspace/core'; import { Section } from '/workspace/core';
import { actions as toastActions } from '/reducers/toast'; import { actions as toastActions } from '/reducers/toast';
import { actions as envActions } from '/reducers/env'; import { actions as envActions } from '/reducers/env';
import { DirectoryApi, GitHubApi } from '/apis'; import { HierarchyApi, GitHubApi } from '/apis';
import { tracerManager } from '/core'; import { tracerManager } from '/core';
import styles from './stylesheet.scss'; import styles from './stylesheet.scss';
import 'axios-progress-bar/dist/nprogress.css' import 'axios-progress-bar/dist/nprogress.css'
...@@ -33,11 +33,11 @@ class App extends React.Component { ...@@ -33,11 +33,11 @@ class App extends React.Component {
componentDidMount() { componentDidMount() {
this.updateDirectory(this.props.match.params); this.updateDirectory(this.props.match.params);
DirectoryApi.getCategories() HierarchyApi.getHierarchy()
.then(({ categories }) => { .then(({ hierarchy }) => {
this.props.setCategories(categories); this.props.setHierarchy(hierarchy);
const { categoryKey, algorithmKey } = this.props.env; const { categoryKey, algorithmKey } = this.props.env;
const category = categories.find(category => category.key === categoryKey) || categories[0]; const category = hierarchy.find(category => category.key === categoryKey) || hierarchy[0];
const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey) || category.algorithms[0]; const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey) || category.algorithms[0];
this.props.history.push(`/${category.key}/${algorithm.key}`); this.props.history.push(`/${category.key}/${algorithm.key}`);
}); });
...@@ -63,7 +63,7 @@ class App extends React.Component { ...@@ -63,7 +63,7 @@ class App extends React.Component {
updateDirectory({ categoryKey = null, algorithmKey = null }) { updateDirectory({ categoryKey = null, algorithmKey = null }) {
if (categoryKey && algorithmKey) { if (categoryKey && algorithmKey) {
this.props.setDirectory(categoryKey, algorithmKey); this.props.setDirectory(categoryKey, algorithmKey);
DirectoryApi.getFile(categoryKey, algorithmKey, 'code.js').then(code => tracerManager.setCode(code)); HierarchyApi.getFile(categoryKey, algorithmKey, 'code.js').then(code => tracerManager.setCode(code));
} }
} }
...@@ -88,11 +88,11 @@ class App extends React.Component { ...@@ -88,11 +88,11 @@ class App extends React.Component {
} }
render() { render() {
const { categories, categoryKey, algorithmKey } = this.props.env; const { hierarchy, categoryKey, algorithmKey } = this.props.env;
const navigatorOpened = true; const navigatorOpened = true;
return categories && categoryKey && algorithmKey && ( return hierarchy && categoryKey && algorithmKey && (
<div className={styles.app}> <div className={styles.app}>
<Workspace className={styles.workspace} wsProps={{ horizontal: false }}> <Workspace className={styles.workspace} wsProps={{ horizontal: false }}>
<Header wsProps={{ <Header wsProps={{
......
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { actions as envActions } from '/reducers/env'; import { actions as envActions } from '/reducers/env';
import { DirectoryApi } from '/apis/index'; import { HierarchyApi } from '/apis/index';
import { MarkdownViewer } from '/components'; import { MarkdownViewer } from '/components';
@connect( @connect(
...@@ -37,7 +37,7 @@ class DescriptionViewer extends React.Component { ...@@ -37,7 +37,7 @@ class DescriptionViewer extends React.Component {
loadMarkdown(href) { loadMarkdown(href) {
const [, , categoryKey, algorithmKey] = href.split('/'); const [, , categoryKey, algorithmKey] = href.split('/');
DirectoryApi.getFile(categoryKey, algorithmKey, 'desc.md') HierarchyApi.getFile(categoryKey, algorithmKey, 'desc.md')
.then(source => this.setState({ source })) .then(source => this.setState({ source }))
.catch(() => this.setState({ source: null })); .catch(() => this.setState({ source: null }));
} }
......
...@@ -70,9 +70,9 @@ class Header extends React.Component { ...@@ -70,9 +70,9 @@ class Header extends React.Component {
render() { render() {
const { interval, paused, started, profile } = this.state; const { interval, paused, started, profile } = this.state;
const { className, onClickTitleBar, navigatorOpened } = this.props; const { className, onClickTitleBar, navigatorOpened } = this.props;
const { categories, categoryKey, algorithmKey, signedIn } = this.props.env; const { hierarchy, categoryKey, algorithmKey, signedIn } = this.props.env;
const category = categories.find(category => category.key === categoryKey); const category = hierarchy.find(category => category.key === categoryKey);
const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey); const algorithm = category.algorithms.find(algorithm => algorithm.key === algorithmKey);
return ( return (
......
...@@ -40,10 +40,10 @@ class Navigator extends React.Component { ...@@ -40,10 +40,10 @@ class Navigator extends React.Component {
} }
handleChangeQuery(e) { handleChangeQuery(e) {
const { categories } = this.props.env; const { hierarchy } = this.props.env;
const categoriesOpened = {}; const categoriesOpened = {};
const query = e.target.value; const query = e.target.value;
categories.forEach(category => { hierarchy.forEach(category => {
if (this.testQuery(name) || category.algorithms.find(algorithm => this.testQuery(algorithm.name))) { if (this.testQuery(name) || category.algorithms.find(algorithm => this.testQuery(algorithm.name))) {
categoriesOpened[category.key] = true; categoriesOpened[category.key] = true;
} }
...@@ -60,7 +60,7 @@ class Navigator extends React.Component { ...@@ -60,7 +60,7 @@ class Navigator extends React.Component {
render() { render() {
const { categoriesOpened, query } = this.state; const { categoriesOpened, query } = this.state;
const { className, style } = this.props; const { className, style } = this.props;
const { categoryKey: selectedCategoryKey, algorithmKey: selectedAlgorithmKey, categories } = this.props.env; const { hierarchy, categoryKey: selectedCategoryKey, algorithmKey: selectedAlgorithmKey } = this.props.env;
return ( return (
<nav className={classes(styles.navigator, className)} style={style}> <nav className={classes(styles.navigator, className)} style={style}>
...@@ -71,7 +71,7 @@ class Navigator extends React.Component { ...@@ -71,7 +71,7 @@ class Navigator extends React.Component {
</div> </div>
<div className={styles.algorithm_list}> <div className={styles.algorithm_list}>
{ {
categories.map(category => { hierarchy.map(category => {
const categoryOpened = categoriesOpened[category.key]; const categoryOpened = categoriesOpened[category.key];
let algorithms = category.algorithms; let algorithms = category.algorithms;
if (!this.testQuery(category.name)) { if (!this.testQuery(category.name)) {
......
...@@ -3,20 +3,20 @@ import { combineActions, createAction, handleActions } from 'redux-actions'; ...@@ -3,20 +3,20 @@ import { combineActions, createAction, handleActions } from 'redux-actions';
const prefix = 'ENV'; const prefix = 'ENV';
const setCategories = createAction(`${prefix}/SET_CATEGORIES`, categories => ({ categories })); const setHierarchy = createAction(`${prefix}/SET_HIERARCHY`, hierarchy => ({ hierarchy }));
const setDirectory = createAction(`${prefix}/SET_DIRECTORY`, (categoryKey, algorithmKey) => ({ const setDirectory = createAction(`${prefix}/SET_DIRECTORY`, (categoryKey, algorithmKey) => ({
categoryKey, categoryKey,
algorithmKey, algorithmKey,
})); }));
export const actions = { export const actions = {
setCategories, setHierarchy,
setDirectory, setDirectory,
}; };
const accessToken = Cookies.get('access_token'); const accessToken = Cookies.get('access_token');
const defaultState = { const defaultState = {
categories: null, hierarchy: null,
categoryKey: null, categoryKey: null,
algorithmKey: null, algorithmKey: null,
accessToken, accessToken,
...@@ -25,7 +25,7 @@ const defaultState = { ...@@ -25,7 +25,7 @@ const defaultState = {
export default handleActions({ export default handleActions({
[combineActions( [combineActions(
setCategories, setHierarchy,
setDirectory, setDirectory,
)]: (state, { payload }) => ({ )]: (state, { payload }) => ({
...state, ...state,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册