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