diff --git a/package.json b/package.json index 8520cb82..b6f2bec3 100644 --- a/package.json +++ b/package.json @@ -76,6 +76,7 @@ "js-base64": "^2.1.9", "json-loader": "^0.5.4", "localforage": "^1.4.2", - "lodash": "^4.13.1" + "lodash": "^4.13.1", + "pluralize": "^3.0.0" } } diff --git a/src/actions/findbar.js b/src/actions/findbar.js index 77053ada..68db0399 100644 --- a/src/actions/findbar.js +++ b/src/actions/findbar.js @@ -2,8 +2,8 @@ import history from '../routing/history'; import { SEARCH } from '../containers/FindBar'; export const RUN_COMMAND = 'RUN_COMMAND'; -export const LIST_POSTS = 'LIST_POSTS'; -export const LIST_FAQ = 'LIST_FAQ'; +export const SHOW_COLLECTION = 'SHOW_COLLECTION'; +export const CREATE_COLLECTION = 'CREATE_COLLECTION'; export const HELP = 'HELP'; export function run(commandName, payload) { @@ -13,11 +13,11 @@ export function run(commandName, payload) { export function runCommand(commandName, payload) { return (dispatch, getState) => { switch (commandName) { - case LIST_POSTS: - history.push('/collections/posts'); + case SHOW_COLLECTION: + history.push(`/collections/${payload.collectionName}`); break; - case LIST_FAQ: - history.push('/collections/faq'); + case CREATE_COLLECTION: + window.alert(`Create a new ${payload.collectionName} - not supported yet`); break; case HELP: window.alert('Find Bar Help (PLACEHOLDER)\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit.'); diff --git a/src/containers/App.js b/src/containers/App.js index 2e0a00ef..052af2c1 100644 --- a/src/containers/App.js +++ b/src/containers/App.js @@ -3,9 +3,10 @@ import { connect } from 'react-redux'; import { loadConfig } from '../actions/config'; import { loginUser } from '../actions/auth'; import { currentBackend } from '../backends/backend'; -import { LIST_POSTS, LIST_FAQ, HELP, MORE_COMMANDS } from '../actions/findbar'; +import { SHOW_COLLECTION, CREATE_COLLECTION, HELP } from '../actions/findbar'; import FindBar from './FindBar'; import styles from './App.css'; +import pluralize from 'pluralize'; class App extends React.Component { componentDidMount() { @@ -50,6 +51,37 @@ class App extends React.Component { ; } + generateFindBarCommands() { + // Generate command list + const commands = []; + const defaultCommands = []; + + this.props.collections.forEach(collection => { + commands.push({ + id: `show_${collection.get('name')}`, + pattern: `Show ${pluralize(collection.get('label'))}`, + type: SHOW_COLLECTION, + payload: { collectionName:collection.get('name') } + }); + + if (defaultCommands.length < 5) defaultCommands.push(`show_${collection.get('name')}`); + + if (collection.get('create') === true) { + commands.push({ + id: `create_${collection.get('name')}`, + pattern: `Create new ${pluralize(collection.get('label'), 1)}(:itemName as ${pluralize(collection.get('label'), 1)} Name)`, + type: CREATE_COLLECTION, + payload: { collectionName:collection.get('name') } + }); + } + }); + + commands.push({ id: HELP, type: HELP, pattern: 'Help' }); + defaultCommands.push(HELP); + + return { commands, defaultCommands }; + } + render() { const { user, config, children } = this.props; @@ -69,15 +101,16 @@ class App extends React.Component { return this.authenticating(); } + const { commands, defaultCommands } = this.generateFindBarCommands(); + return (
- +
@@ -89,10 +122,10 @@ class App extends React.Component { } function mapStateToProps(state) { - const { auth, config } = state; + const { auth, config, collections } = state; const user = auth && auth.get('user'); - return { auth, config, user }; + return { auth, config, collections, user }; } export default connect(mapStateToProps)(App); diff --git a/src/containers/FindBar.js b/src/containers/FindBar.js index 829e9d5f..ce13254d 100644 --- a/src/containers/FindBar.js +++ b/src/containers/FindBar.js @@ -124,8 +124,11 @@ class FindBar extends Component { }, () => { this._input.blur(); }); - const payload = paramName ? { [paramName]: enteredParamValue } : null; - this.props.dispatch(runCommand(command.id, payload)); + const payload = command.payload || {}; + if (paramName) { + payload[paramName] = enteredParamValue; + } + this.props.dispatch(runCommand(command.type, payload)); } } @@ -358,6 +361,7 @@ class FindBar extends Component { FindBar.propTypes = { commands: PropTypes.arrayOf(PropTypes.shape({ id: PropTypes.string.isRequired, + type: PropTypes.string.isRequired, pattern: PropTypes.string.isRequired })).isRequired, defaultCommands: PropTypes.arrayOf(PropTypes.string),