Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2368 from studentinsights/feature/reading-persist…
…ence Reading: store grouping state locally, snapshot grouping state and post to server
- Loading branch information
Showing
16 changed files
with
549 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
// Wraps lifecycle events, so that a caller can | ||
// describe imperative side-effecting actions based on when something | ||
// is rendered, without having to put that inside the | ||
// component definition. | ||
// | ||
// Example: | ||
// <Lifecycle componentWillMount={this.prefetchScreenTwoData}> | ||
// <ScreenOne /> | ||
// </Lifecycle> | ||
export default class Lifecycle extends React.Component { | ||
componentWillMount(nextProps, nextState) { | ||
const {componentWillMount} = this.props; | ||
if (componentWillMount) componentWillMount(nextProps, nextState); | ||
} | ||
|
||
render() { | ||
const {children} = this.props; | ||
return children; | ||
} | ||
} | ||
|
||
Lifecycle.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
componentWillMount: PropTypes.func | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Lifecycle from './Lifecycle.js'; | ||
|
||
|
||
it('renders without crashing', () => { | ||
const div = document.createElement('div'); | ||
ReactDOM.render(<Lifecycle><div>hello</div></Lifecycle>, div); | ||
}); | ||
|
||
|
||
it('calls componentWillMount', () => { | ||
const div = document.createElement('div'); | ||
const props = { | ||
componentWillMount: jest.fn() | ||
}; | ||
ReactDOM.render(<Lifecycle {...props}><div>hello</div></Lifecycle>, div); | ||
expect(props.componentWillMount).toHaveBeenCalled(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import _ from 'lodash'; | ||
|
||
// Autosaves on a throttled interval, calling `doSaveFn` whenever `readSnapshotFn` | ||
// has changed (using _.isEqual). | ||
export default class Autosaver extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
lastSavedSnapshot: null | ||
}; | ||
|
||
this.doAutoSaveChanges = _.throttle(this.doAutoSaveChanges, props.autoSaveIntervalMs); | ||
this.onPostDone = this.onPostDone.bind(this); | ||
this.onPostError = this.onPostError.bind(this); | ||
} | ||
|
||
componentDidUpdate() { | ||
this.doAutoSaveChanges(); | ||
} | ||
|
||
componentWillUnmount() { | ||
if (this.doAutoSaveChanges.flush) this.doAutoSaveChanges.flush(); // flush any queued changes | ||
} | ||
|
||
isDirty() { | ||
const {readSnapshotFn} = this.props; | ||
const {lastSavedSnapshot} = this.state; | ||
const snapshot = readSnapshotFn(); | ||
if (snapshot === undefined || snapshot === null) return false; | ||
return !_.isEqual(lastSavedSnapshot, snapshot); | ||
} | ||
|
||
// This method is throttled. | ||
doAutoSaveChanges() { | ||
const {doSaveFn} = this.props; | ||
if (!this.isDirty()) return; | ||
|
||
doSaveFn() | ||
.then(this.onPostDone) | ||
.catch(this.onPostError); | ||
} | ||
|
||
onPostDone(snapshotForSaving) { | ||
this.setState({lastSavedSnapshot: snapshotForSaving}); | ||
} | ||
|
||
onPostError(error) { | ||
window.Rollbar.error && window.Rollbar.error('Autosaver#onPostError', error); | ||
} | ||
|
||
render() { | ||
const {children} = this.props; | ||
return children; | ||
} | ||
} | ||
Autosaver.propTypes = { | ||
readSnapshotFn: PropTypes.func.isRequired, | ||
doSaveFn: PropTypes.func.isRequired, | ||
children: PropTypes.node.isRequired, | ||
autoSaveIntervalMs: PropTypes.number.isRequired | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import Autosaver from './Autosaver'; | ||
|
||
|
||
export function testProps(props) { | ||
return { | ||
readSnapshotFn() { | ||
return { foo: 'bar' }; | ||
}, | ||
doSaveFn() { | ||
return Promise.resolve({ foo: 'bazzzzz-updated'}); | ||
}, | ||
autoSaveIntervalMs: 300, | ||
...props | ||
}; | ||
} | ||
|
||
|
||
it('renders without crashing', () => { | ||
const el = document.createElement('div'); | ||
const props = testProps(); | ||
ReactDOM.render( | ||
<Autosaver {...props}> | ||
<div>hello!</div> | ||
</Autosaver> | ||
, el); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,58 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {testProps, testEl} from './CreateGroups.test'; | ||
|
||
|
||
function storyProps(props = {}) { | ||
return { | ||
...testProps(), | ||
onStudentIdsByRoomChanged: action('onStudentIdsByRoomChanged'), | ||
...props | ||
}; | ||
} | ||
|
||
storiesOf('reading/CreateGroups', module) // eslint-disable-line no-undef | ||
.add('mock photo', () => testEl(testProps())) | ||
.add('fallback photo', () => testEl(testProps({useMockPhoto: true}))); | ||
|
||
.add('with state container', () => ( | ||
<StateContainer defaultStudentIdsByRoom={storyProps().studentIdsByRoom}> | ||
{({studentIdsByRoom, onStudentIdsByRoomChanged}) => ( | ||
testEl(storyProps({ | ||
studentIdsByRoom, | ||
onStudentIdsByRoomChanged, | ||
useMockPhoto: true | ||
})) | ||
)} | ||
</StateContainer> | ||
)) | ||
.add('mock photo', () => testEl(storyProps())) | ||
.add('fallback photo', () => testEl(storyProps({useMockPhoto: true}))); | ||
|
||
|
||
class StateContainer extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
studentIdsByRoom: props.defaultStudentIdsByRoom | ||
}; | ||
|
||
this.onStudentIdsByRoomChanged = this.onStudentIdsByRoomChanged.bind(this); | ||
} | ||
|
||
onStudentIdsByRoomChanged({studentIdsByRoom}) { | ||
this.setState({studentIdsByRoom}); | ||
} | ||
|
||
render() { | ||
const {children} = this.props; | ||
const {studentIdsByRoom} = this.state; | ||
return children({ | ||
studentIdsByRoom, | ||
onStudentIdsByRoomChanged: this.onStudentIdsByRoomChanged | ||
}); | ||
} | ||
} | ||
StateContainer.propTypes = { | ||
defaultStudentIdsByRoom: PropTypes.any, | ||
children: PropTypes.func.isRequired | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.