Skip to content

Commit

Permalink
Merge pull request #38472 from code-dot-org/fix-save-time
Browse files Browse the repository at this point in the history
show client time when script and lesson edit pages are saved
  • Loading branch information
davidsbailey committed Jan 12, 2021
2 parents cfb82d0 + 80d9965 commit 9e64972
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion apps/src/lib/levelbuilder/SaveBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const styles = {

export default class SaveBar extends Component {
static propTypes = {
lastSaved: PropTypes.string,
lastSaved: PropTypes.number,
error: PropTypes.string,
handleSave: PropTypes.func.isRequired,
isSaving: PropTypes.bool
Expand Down
6 changes: 1 addition & 5 deletions apps/src/lib/levelbuilder/lesson-editor/LessonEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ class LessonEditor extends Component {

this.setState({isSaving: true, lastSaved: null, error: null});

// Remove updatedAt before sending information to server as updateAt time
// was not consistent between server version and client
delete this.state.originalLessonData.updatedAt;

$.ajax({
url: `/lessons/${this.state.originalLessonData.id}`,
method: 'PUT',
Expand Down Expand Up @@ -126,7 +122,7 @@ class LessonEditor extends Component {

this.props.initActivities(activities);
this.setState({
lastSaved: data.updatedAt,
lastSaved: Date.now(),
isSaving: false,
originalLessonData: data
});
Expand Down
2 changes: 1 addition & 1 deletion apps/src/lib/levelbuilder/script-editor/ScriptEditor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ class ScriptEditor extends React.Component {

this.props.init(lessonGroups, this.props.levelKeyList);
this.setState({
lastSaved: data.updatedAt,
lastSaved: Date.now(),
isSaving: false,
oldScriptText: data.lessonLevelData
});
Expand Down
4 changes: 1 addition & 3 deletions apps/test/unit/lib/levelbuilder/SaveBarTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ describe('SaveBar', () => {
});

it('shows lastSaved when there is no error', () => {
const wrapper = mount(
<SaveBar {...defaultProps} lastSaved={'2020-11-06T21:33:32.000Z'} />
);
const wrapper = mount(<SaveBar {...defaultProps} lastSaved={Date.now()} />);

expect(wrapper.find('.lastSavedMessage').text()).to.include(
'Last saved at:'
Expand Down
16 changes: 12 additions & 4 deletions apps/test/unit/lib/levelbuilder/lesson-editor/LessonEditorTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import sinon from 'sinon';
import * as utils from '@cdo/apps/utils';

describe('LessonEditor', () => {
let defaultProps, store;
let defaultProps, store, clock;
beforeEach(() => {
sinon.stub(utils, 'navigateToHref');
stubRedux();
Expand Down Expand Up @@ -54,6 +54,10 @@ describe('LessonEditor', () => {
afterEach(() => {
restoreRedux();
utils.navigateToHref.restore();
if (clock) {
clock.restore();
clock = undefined;
}
});

const createWrapper = overrideProps => {
Expand Down Expand Up @@ -120,7 +124,7 @@ describe('LessonEditor', () => {
const wrapper = createWrapper({});
const lessonEditor = wrapper.find('LessonEditor');

let returnData = {updatedAt: '2020-11-06T21:33:32.000Z', activities: []};
let returnData = {activities: []};
let server = sinon.fakeServer.create();
server.respondWith('PUT', `/lessons/1`, [
200,
Expand All @@ -139,11 +143,15 @@ describe('LessonEditor', () => {
expect(wrapper.find('.saveBar').find('FontAwesome').length).to.equal(1);
expect(lessonEditor.state().isSaving).to.equal(true);

clock = sinon.useFakeTimers(new Date('2020-12-01'));
const expectedLastSaved = Date.now();
server.respond();
clock.tick(50);

lessonEditor.update();
expect(utils.navigateToHref).to.not.have.been.called;
expect(lessonEditor.state().isSaving).to.equal(false);
expect(lessonEditor.state().lastSaved).to.equal('2020-11-06T21:33:32.000Z');
expect(lessonEditor.state().lastSaved).to.equal(expectedLastSaved);
expect(wrapper.find('.saveBar').find('FontAwesome').length).to.equal(0);
//check that last saved message is showing
expect(wrapper.find('.lastSavedMessage').length).to.equal(1);
Expand Down Expand Up @@ -189,7 +197,7 @@ describe('LessonEditor', () => {
const wrapper = createWrapper({});
const lessonEditor = wrapper.find('LessonEditor');

let returnData = {updatedAt: '2020-11-06T21:33:32.000Z', activities: []};
let returnData = {activities: []};
let server = sinon.fakeServer.create();
server.respondWith('PUT', `/lessons/1`, [
200,
Expand Down
19 changes: 14 additions & 5 deletions apps/test/unit/lib/levelbuilder/script-editor/ScriptEditorTest.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,20 @@ describe('ScriptEditor', () => {
});

describe('Saving Script Editor', () => {
let clock;

afterEach(() => {
if (clock) {
clock.restore();
clock = undefined;
}
});

it('can save and keep editing', () => {
const wrapper = createWrapper({});
const scriptEditor = wrapper.find('ScriptEditor');

let returnData = {
updatedAt: '2020-11-06T21:33:32.000Z',
scriptPath: '/s/test-script'
};
let server = sinon.fakeServer.create();
Expand All @@ -228,13 +236,15 @@ describe('ScriptEditor', () => {
expect(wrapper.find('.saveBar').find('FontAwesome').length).to.equal(1);
expect(scriptEditor.state().isSaving).to.equal(true);

clock = sinon.useFakeTimers(new Date('2020-12-01'));
const expectedLastSaved = Date.now();
server.respond();
clock.tick(50);

scriptEditor.update();
expect(utils.navigateToHref).to.not.have.been.called;
expect(scriptEditor.state().isSaving).to.equal(false);
expect(scriptEditor.state().lastSaved).to.equal(
'2020-11-06T21:33:32.000Z'
);
expect(scriptEditor.state().lastSaved).to.equal(expectedLastSaved);
expect(wrapper.find('.saveBar').find('FontAwesome').length).to.equal(0);
//check that last saved message is showing
expect(wrapper.find('.lastSavedMessage').length).to.equal(1);
Expand Down Expand Up @@ -281,7 +291,6 @@ describe('ScriptEditor', () => {
const scriptEditor = wrapper.find('ScriptEditor');

let returnData = {
updatedAt: '2020-11-06T21:33:32.000Z',
scriptPath: '/s/test-script'
};
let server = sinon.fakeServer.create();
Expand Down
2 changes: 1 addition & 1 deletion dashboard/app/controllers/lessons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def edit
# PATCH/PUT /lessons/1
def update
if params[:originalLessonData]
current_lesson_data = JSON.generate(@lesson.summarize_for_lesson_edit.except(:updatedAt))
current_lesson_data = JSON.generate(@lesson.summarize_for_lesson_edit)
old_lesson_data = params[:originalLessonData]
if old_lesson_data != current_lesson_data
msg = "Could not update the lesson because the contents of the lesson has changed outside of this editor. Reload the page and try saving again."
Expand Down
3 changes: 1 addition & 2 deletions dashboard/app/models/lesson.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ def summarize_for_lesson_edit
activities: lesson_activities.map(&:summarize_for_lesson_edit),
resources: resources.map(&:summarize_for_lesson_edit),
objectives: objectives.map(&:summarize_for_edit),
courseVersionId: lesson_group.script.get_course_version&.id,
updatedAt: updated_at
courseVersionId: lesson_group.script.get_course_version&.id
}
end

Expand Down
1 change: 0 additions & 1 deletion dashboard/app/models/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,6 @@ def summarize(include_lessons = true, user = nil, include_bonus_levels = false)
is_course: is_course?,
background: background,
is_migrated: is_migrated?,
updatedAt: updated_at,
scriptPath: script_path(self),
showCalendar: show_calendar
}
Expand Down

0 comments on commit 9e64972

Please sign in to comment.