Skip to content

Commit e7a64b9

Browse files
ning-yEvan Sebastian
authored andcommitted
Scaffold playground page (#8)
* Add button for playground at Navbar No functionality. * Make first NavbarGroup's align left explicit * Change playground icon * Add routes to Playground * Update test snapshots * Fix bad test for Playground component Oops * Add react-ace for IDE * Try passing props into playground * Load a preset "Hello World" onto the ace editor. Changes: - Changed `Playground` component to a statefull one (extends `React.Component) - add property `playgroundCode` to IApplicationState (bad practice, change to a slice of the state instead) - add interface `IPlaygroundProps` to denote the properties passed to Playground * Remove unused onChange() * Add stored state for playground editor text Once again, this is a WIP. Text typed into the playground editor will stay in the playground (or rather, appear again) after leaving and coming back. Changes: - added of action, actionCreator (updatePlaygroundCode) - added a reducer under application's `reducer` function Possible problems: - Location of reducer - Location of playgroundCode stored (as in previous commit) * Change initialValue prop to editorValue * Made playground actions a separate file * Modify actions and dispatch call * Change redux import statement * Add IPlaygroundState to IState in index reducer * Add playground reducer * Remove playgroundCode from IApplicationState * Fix type errors Made a compromise by including an IPlaygroundState as part of IApplicationProps * Re-disable console.logs (tslint) * Fix missing imports for react-ace mode, theme * Add playground CSS * Add JSDoc style comments for playground * Fix some tests * Fix semantics of mapStateToProps for ApplicationContainer * Do trivial formatting * Make 16/05 code review changes Changes not made: - Change of `Playground` component to an `SFC` * Fix test snapshot (name of onChange for ReactAce) Due to 7ac707c. * Revert Playground as SFC, make Editor container * Make Playground stateless This is done by abstracting out the ace editor into its own component , `Editor`, and its own container to handle redux calls, `EditorContainer`. * Add test and fix export type Export of `Editor` changed to a default export, like the other components. A test has been added for the `Editor` component. Snapshot changes pending. * Fix test snapshots
1 parent cfdfd9e commit e7a64b9

22 files changed

+255
-18
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@blueprintjs/core": "^2.1.1",
2929
"normalize.css": "^8.0.0",
3030
"react": "^16.3.1",
31+
"react-ace": "^6.1.1",
3132
"react-dom": "^16.3.1",
3233
"react-redux": "^5.0.7",
3334
"react-router": "^4.2.0",

src/actions/index.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/actions/playground.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Action, ActionCreator } from 'redux'
2+
3+
/**
4+
* The `type` attribute for an `Action` which updates the `IPlaygroundState`
5+
* `editorValue`
6+
*/
7+
export const UPDATE_EDITOR_VALUE = 'UPDATE_EDITOR_VALUE'
8+
9+
/**
10+
* Represents an `Action` which updates the `editorValue` of a
11+
* `IPlaygroundState`
12+
* @property type - Unique string identifier for this `Action`
13+
* @property newEditorValue - The new string value for `editorValue`
14+
*/
15+
export interface IUpdateEditorValue extends Action {
16+
payload: string
17+
}
18+
19+
/**
20+
* An `ActionCreator` returning an `IUpdateEditorValue` `Action`
21+
* @param newEditorValue - The new string value for `editorValue`
22+
*/
23+
export const updateEditorValue: ActionCreator<IUpdateEditorValue> = (newEditorValue: string) => ({
24+
type: UPDATE_EDITOR_VALUE,
25+
payload: newEditorValue
26+
})

src/components/Application.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@ import * as React from 'react'
33
import { Redirect, Route, RouteComponentProps, Switch } from 'react-router'
44

55
import DashboardContainer from '../containers/DashboardContainer'
6-
import { IApplicationState } from '../reducers/application'
6+
77
import NavigationBar from './NavigationBar'
88
import NotFound from './NotFound'
9+
import Playground from './Playground'
910

1011
export interface IApplicationProps extends RouteComponentProps<{}> {
11-
application: IApplicationState
12+
title: string
1213
}
1314

14-
const Application: React.SFC<IApplicationProps> = ({ application }) => {
15+
const Application: React.SFC<IApplicationProps> = props => {
1516
const redirectToDashboard = () => <Redirect to="/dashboard" />
1617

1718
return (
1819
<div className="Application">
19-
<NavigationBar title={application.title} />
20+
<NavigationBar title={props.title} />
2021
<div className="Application__main">
2122
<Switch>
2223
<Route path="/dashboard" component={DashboardContainer} />
24+
<Route path="/playground" component={Playground} />
2325
<Route exact={true} path="/" component={redirectToDashboard} />
2426
<Route component={NotFound} />
2527
</Switch>

src/components/Editor.tsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from 'react'
2+
import AceEditor from 'react-ace'
3+
4+
import 'brace/mode/javascript'
5+
import 'brace/theme/github'
6+
7+
/**
8+
* @property editorValue - The string content of the react-ace editor
9+
* @property updateCode - A callback function for the react-ace editor's `onChange`
10+
*/
11+
export interface IEditorProps {
12+
editorValue: string
13+
handleEditorChange: (newCode: string) => void
14+
}
15+
16+
class Editor extends React.Component<IEditorProps, {}> {
17+
public render() {
18+
return (
19+
<AceEditor
20+
height="90%"
21+
width="90%"
22+
mode="javascript"
23+
theme="github"
24+
value={this.props.editorValue}
25+
onChange={this.props.handleEditorChange}
26+
/>
27+
)
28+
}
29+
}
30+
31+
export default Editor

src/components/NavigationBar.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export interface INavigationBarProps {
99

1010
const NavigationBar: React.SFC<INavigationBarProps> = ({ title }) => (
1111
<Navbar className="NavigationBar pt-dark">
12-
<NavbarGroup>
12+
<NavbarGroup className="pt-align-left">
1313
<NavbarHeading>{title}</NavbarHeading>
1414
<NavbarDivider />
1515
<NavLink
@@ -21,6 +21,17 @@ const NavigationBar: React.SFC<INavigationBarProps> = ({ title }) => (
2121
Dashboard
2222
</NavLink>
2323
</NavbarGroup>
24+
25+
<NavbarGroup className="pt-align-right">
26+
<NavLink
27+
to="/playground"
28+
activeClassName="pt-active"
29+
className="NavigationBar__link pt-button pt-minimal"
30+
>
31+
<Icon icon="code" />
32+
Playground
33+
</NavLink>
34+
</NavbarGroup>
2435
</Navbar>
2536
)
2637

src/components/Playground.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as React from 'react'
2+
import EditorContainer from '../containers/EditorContainer'
3+
4+
const Playground: React.SFC<{}> = ()=> {
5+
return (
6+
<div className="Playground">
7+
<h2>Playground</h2>
8+
<EditorContainer />
9+
</div>
10+
)
11+
}
12+
13+
export default Playground

src/components/__tests__/Application.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ import * as React from 'react'
33
import { shallow } from 'enzyme'
44

55
import { mockRouterProps } from '../../mocks/components'
6-
import { ApplicationEnvironment } from '../../reducers/application'
76
import Application, { IApplicationProps } from '../Application'
87

98
test('Application renders correctly', () => {
109
const props: IApplicationProps = {
1110
...mockRouterProps('/dashboard', {}),
12-
application: {
13-
title: 'Cadet',
14-
environment: ApplicationEnvironment.Development
15-
}
11+
title: 'Cadet'
1612
}
1713
const app = <Application {...props} />
1814
const tree = shallow(app)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as React from 'react'
2+
3+
import { shallow } from 'enzyme'
4+
5+
import Editor, { IEditorProps } from '../Editor'
6+
7+
test('Editor renders correctly', () => {
8+
const props: IEditorProps = {
9+
editorValue: ''
10+
handleEditorChange: (newCode) =>
11+
}
12+
const app = <Editor { props }/>
13+
const tree = shallow(app)
14+
expect(tree.debug()).toMatchSnapshot()
15+
})
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import * as React from 'react'
2+
3+
import { shallow } from 'enzyme'
4+
5+
import Playground from '../Playground'
6+
7+
test('Playground renders correctly', () => {
8+
const app = <Playground />
9+
const tree = shallow(app)
10+
expect(tree.debug()).toMatchSnapshot()
11+
})

0 commit comments

Comments
 (0)