diff options
-rw-r--r-- | packages/gitbook-core/package.json | 1 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/TYPES.js | 6 | ||||
-rw-r--r-- | packages/gitbook-core/src/actions/api.js | 73 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/createContext.js | 3 | ||||
-rw-r--r-- | packages/gitbook-core/src/lib/renderWithContext.js | 3 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/StateApi.js | 26 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/StateSummary.js | 31 | ||||
-rw-r--r-- | packages/gitbook-core/src/models/User.js | 15 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/api.js | 15 | ||||
-rw-r--r-- | packages/gitbook-core/src/reducers/summary.js | 26 |
10 files changed, 172 insertions, 27 deletions
diff --git a/packages/gitbook-core/package.json b/packages/gitbook-core/package.json index 1397b44..9078679 100644 --- a/packages/gitbook-core/package.json +++ b/packages/gitbook-core/package.json @@ -7,6 +7,7 @@ "bluebird": "^3.4.6", "classnames": "^2.2.5", "entities": "^1.1.1", + "gitbook-api": "^3.0.2", "history": "^4.3.0", "html-tags": "^1.1.1", "immutable": "^3.8.1", diff --git a/packages/gitbook-core/src/actions/TYPES.js b/packages/gitbook-core/src/actions/TYPES.js index 9876057..613aacf 100644 --- a/packages/gitbook-core/src/actions/TYPES.js +++ b/packages/gitbook-core/src/actions/TYPES.js @@ -11,6 +11,8 @@ module.exports = { PAGE_FETCH_START: 'history/fetch:start', PAGE_FETCH_END: 'history/fetch:end', PAGE_FETCH_ERROR: 'history/fetch:error', - // i18n - I18N_REGISTER_LOCALE: 'i18n/register:locale' + // I18n + I18N_REGISTER_LOCALE: 'i18n/register:locale', + // Api + API_USER_FETCHED: 'api/user_fetched' }; diff --git a/packages/gitbook-core/src/actions/api.js b/packages/gitbook-core/src/actions/api.js new file mode 100644 index 0000000..7a0910f --- /dev/null +++ b/packages/gitbook-core/src/actions/api.js @@ -0,0 +1,73 @@ +const GitBookAPI = require('gitbook-api'); +const ACTION_TYPES = require('./TYPES'); + +const User = require('../models/User'); + +/** + * Execute an HTTP API request. + * + * @param {String} method + * @param {String} url + * @param {Object} params + * @param {Object} data + * @return {Action} + */ +function request(method, url, params, data) { + return (dispatch, getState) => { + const client = new GitBookAPI({}, { + host: '<todo>' + }); + + return client.request(url, { + method, + data, + params + }); + }; +} + +const get = (...args) => request('GET', ...args); +const post = (...args) => request('POST', ...args); +const del = (...args) => request('DELETE', ...args); + +/** + * Fetch infos about current user. + * @return {Action} + */ +function fetchCurrentUser() { + return (dispatch) => { + return dispatch(get('reader')) + .then((user) => { + dispatch({ + type: ACTION_TYPES.API_USER_FETCHED, + user: new User(user) + }); + }); + }; +} + +/** + * Activate the API: + * - Fetch the current user. + * @return {Action} + */ +function activate() { + return (dispatch) => { + return dispatch(fetchCurrentUser()); + }; +} + +/** + * Deactivate the AP + * @return {Action} + */ +function deactivate() { + return (dispatch) => { + + }; +} + +module.exports = { + get, post, del, + activate, deactivate +}; diff --git a/packages/gitbook-core/src/lib/createContext.js b/packages/gitbook-core/src/lib/createContext.js index ba0c7e1..d4e1fca 100644 --- a/packages/gitbook-core/src/lib/createContext.js +++ b/packages/gitbook-core/src/lib/createContext.js @@ -8,6 +8,7 @@ const coreReducers = require('../reducers'); const composeReducer = require('./composeReducer'); const Components = require('../actions/components'); +const Api = require('../actions/api'); const I18n = require('../actions/i18n'); const History = require('../actions/history'); @@ -21,7 +22,7 @@ const isBrowser = (typeof window !== 'undefined'); const corePlugin = new Plugin({ reduce: coreReducers, actions: { - Components, I18n, History + Components, I18n, History, Api } }); diff --git a/packages/gitbook-core/src/lib/renderWithContext.js b/packages/gitbook-core/src/lib/renderWithContext.js index 70fba5c..480cfef 100644 --- a/packages/gitbook-core/src/lib/renderWithContext.js +++ b/packages/gitbook-core/src/lib/renderWithContext.js @@ -5,6 +5,7 @@ const PJAXWrapper = require('../components/PJAXWrapper'); const I18nProvider = require('../components/I18nProvider'); const ContextProvider = require('../components/ContextProvider'); const History = require('../actions/history'); +const Api = require('../actions/api'); const contextShape = require('../propTypes/Context'); const GitBookApplication = React.createClass({ @@ -16,11 +17,13 @@ const GitBookApplication = React.createClass({ componentDidMount() { const { context } = this.props; context.dispatch(History.activate()); + context.dispatch(Api.activate()); }, componentWillUnmount() { const { context } = this.props; context.dispatch(History.deactivate()); + context.dispatch(Api.deactivate()); }, render() { diff --git a/packages/gitbook-core/src/models/StateApi.js b/packages/gitbook-core/src/models/StateApi.js new file mode 100644 index 0000000..430833b --- /dev/null +++ b/packages/gitbook-core/src/models/StateApi.js @@ -0,0 +1,26 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + currentUser: null +}; + +/** + * State for the API, it stores informations about the logged in user, etc. + * @type {Record} + */ +class StateApi extends Record(DEFAULTS) { + static create(state) { + return state instanceof StateApi ? + state : new StateApi(state); + } + + /** + * Check if reader is an user is loggedin. + * @return {Boolean} + */ + get isLoggedIn() { + return Boolean(this.currentUser); + } +} + +module.exports = StateApi; diff --git a/packages/gitbook-core/src/models/StateSummary.js b/packages/gitbook-core/src/models/StateSummary.js new file mode 100644 index 0000000..f268f34 --- /dev/null +++ b/packages/gitbook-core/src/models/StateSummary.js @@ -0,0 +1,31 @@ +const { Record, List } = require('immutable'); + +const File = require('../models/File'); +const SummaryPart = require('../models/SummaryPart'); + +const DEFAULTS = { + file: new File(), + parts: List() +}; + +/** + * State for the summary. + * @type {Record} + */ +class StateSummary extends Record(DEFAULTS) { + constructor(state = {}) { + super({ + ...state, + file: new File(state.file), + parts: (new List(state.parts)) + .map(article => new SummaryPart(article)) + }); + } + + static create(state) { + return state instanceof StateSummary ? + state : new StateSummary(state); + } +} + +module.exports = StateSummary; diff --git a/packages/gitbook-core/src/models/User.js b/packages/gitbook-core/src/models/User.js new file mode 100644 index 0000000..6653dc4 --- /dev/null +++ b/packages/gitbook-core/src/models/User.js @@ -0,0 +1,15 @@ +const { Record } = require('immutable'); + +const DEFAULTS = { + username: '' +}; + +/** + * An instance of a GitBook.com user (from API) + * @type {String} + */ +class User extends Record(DEFAULTS) { + +} + +module.exports = User; diff --git a/packages/gitbook-core/src/reducers/api.js b/packages/gitbook-core/src/reducers/api.js new file mode 100644 index 0000000..72f5f33 --- /dev/null +++ b/packages/gitbook-core/src/reducers/api.js @@ -0,0 +1,15 @@ +const StateApi = require('../models/StateApi'); +const ACTION_TYPES = require('../actions/TYPES'); + +module.exports = (state, action) => { + state = StateApi.create(state); + + switch (action.type) { + case ACTION_TYPES.API_USER_FETCHED: + return state.merge({ + currentUser: action.user + }); + default: + return state; + } +}; diff --git a/packages/gitbook-core/src/reducers/summary.js b/packages/gitbook-core/src/reducers/summary.js index 60568ef..c20d27a 100644 --- a/packages/gitbook-core/src/reducers/summary.js +++ b/packages/gitbook-core/src/reducers/summary.js @@ -1,28 +1,6 @@ -const { Record, List } = require('immutable'); +const StateSummary = require('../models/StateSummary'); -const File = require('../models/File'); -const SummaryPart = require('../models/SummaryPart'); - - -class SummaryState extends Record({ - file: new File(), - parts: List() -}) { - constructor(state = {}) { - super({ - ...state, - file: new File(state.file), - parts: (new List(state.parts)) - .map(article => new SummaryPart(article)) - }); - } - - static create(state) { - return state instanceof SummaryState ? - state : new SummaryState(state); - } -} module.exports = (state, action) => { - return SummaryState.create(state); + return StateSummary.create(state); }; |