From 83b077450a7da58f7355d5e0d7245b49bdac6d72 Mon Sep 17 00:00:00 2001 From: Yining Shi Date: Thu, 27 Oct 2016 19:45:09 -0400 Subject: [PATCH] Showing "Autosave enabled" toast (#173) * showing Autosave enabled toast * fixed variable names * fixed constant name typo --- client/constants.js | 3 +++ client/modules/IDE/actions/ide.js | 12 ++++++++++++ client/modules/IDE/actions/project.js | 25 ++++++++++++++++++++----- client/modules/IDE/actions/toast.js | 4 ++-- client/modules/IDE/reducers/ide.js | 7 ++++++- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/client/constants.js b/client/constants.js index 1e50f5b7..77b7e35e 100644 --- a/client/constants.js +++ b/client/constants.js @@ -98,3 +98,6 @@ export const RESET_PASSWORD_RESET = 'RESET_PASSWORD_RESET'; export const INVALID_RESET_PASSWORD_TOKEN = 'INVALID_RESET_PASSWORD_TOKEN'; // eventually, handle errors more specifically and better export const ERROR = 'ERROR'; + +export const JUST_OPENED_PROJECT = 'JUST_OPENED_PROJECT'; +export const RESET_JUST_OPENED_PROJECT = 'RESET_JUST_OPENED_PROJECT'; diff --git a/client/modules/IDE/actions/ide.js b/client/modules/IDE/actions/ide.js index 7526e72c..a31b989d 100644 --- a/client/modules/IDE/actions/ide.js +++ b/client/modules/IDE/actions/ide.js @@ -195,3 +195,15 @@ export function resetInfiniteLoops() { type: ActionTypes.RESET_INFINITE_LOOPS }; } + +export function justOpenedProject() { + return { + type: ActionTypes.JUST_OPENED_PROJECT, + }; +} + +export function resetJustOpenedProject() { + return { + type: ActionTypes.RESET_JUST_OPENED_PROJECT + }; +} diff --git a/client/modules/IDE/actions/project.js b/client/modules/IDE/actions/project.js index 00552467..b9420c7c 100644 --- a/client/modules/IDE/actions/project.js +++ b/client/modules/IDE/actions/project.js @@ -5,12 +5,13 @@ import JSZip from 'jszip'; import JSZipUtils from 'jszip-utils'; import { saveAs } from 'file-saver'; import { showToast, setToastText } from './toast'; -import { setUnsavedChanges } from './ide'; +import { setUnsavedChanges, justOpenedProject, resetJustOpenedProject } from './ide'; const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; export function getProject(id) { return (dispatch) => { + dispatch(justOpenedProject()); axios.get(`${ROOT_URL}/projects/${id}`, { withCredentials: true }) .then(response => { // browserHistory.push(`/projects/${id}`); @@ -52,8 +53,15 @@ export function saveProject(autosave = false) { type: ActionTypes.PROJECT_SAVE_SUCCESS }); if (!autosave) { - dispatch(showToast()); - dispatch(setToastText('Project saved.')); + if (state.ide.justOpenedProject && state.preferences.autosave) { + dispatch(showToast(5500)); + dispatch(setToastText('Project saved.')); + setTimeout(() => dispatch(setToastText('Autosave enabled.')), 1500); + dispatch(resetJustOpenedProject()); + } else { + dispatch(showToast(1500)); + dispatch(setToastText('Project saved.')); + } } }) .catch((response) => dispatch({ @@ -73,8 +81,15 @@ export function saveProject(autosave = false) { files: response.data.files }); if (!autosave) { - dispatch(showToast()); - dispatch(setToastText('Project saved.')); + if (state.preferences.autosave) { + dispatch(showToast(5500)); + dispatch(setToastText('Project saved.')); + setTimeout(() => dispatch(setToastText('Autosave enabled.')), 1500); + dispatch(resetJustOpenedProject()); + } else { + dispatch(showToast(1500)); + dispatch(setToastText('Project saved.')); + } } }) .catch(response => dispatch({ diff --git a/client/modules/IDE/actions/toast.js b/client/modules/IDE/actions/toast.js index d39ed79a..25f472e1 100644 --- a/client/modules/IDE/actions/toast.js +++ b/client/modules/IDE/actions/toast.js @@ -6,12 +6,12 @@ export function hideToast() { }; } -export function showToast() { +export function showToast(time) { return (dispatch) => { dispatch({ type: ActionTypes.SHOW_TOAST }); - setTimeout(() => dispatch(hideToast()), 1500); + setTimeout(() => dispatch(hideToast()), time); }; } diff --git a/client/modules/IDE/reducers/ide.js b/client/modules/IDE/reducers/ide.js index 6c8bc891..4ba6decc 100644 --- a/client/modules/IDE/reducers/ide.js +++ b/client/modules/IDE/reducers/ide.js @@ -16,7 +16,8 @@ const initialState = { unsavedChanges: false, infiniteLoop: false, previewIsRefreshing: false, - infiniteLoopMessage: '' + infiniteLoopMessage: '', + projectJustOpened: false }; const ide = (state = initialState, action) => { @@ -79,6 +80,10 @@ const ide = (state = initialState, action) => { return Object.assign({}, state, { previewIsRefreshing: true }); case ActionTypes.END_SKETCH_REFRESH: return Object.assign({}, state, { previewIsRefreshing: false }); + case ActionTypes.JUST_OPENED_PROJECT: + return Object.assign({}, state, { justOpenedProject: true }); + case ActionTypes.RESET_JUST_OPENED_PROJECT: + return Object.assign({}, state, { justOpenedProject: false }); default: return state; }