Skip to content

Refactor toast actions #2206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions client/modules/IDE/actions/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import browserHistory from '../../../browserHistory';
import apiClient from '../../../utils/apiClient';
import * as ActionTypes from '../../../constants';
import { startLoader, stopLoader } from './loader';
import { setToastText, showToast } from './toast';

const TOAST_DISPLAY_TIME_MS = 1500;
import { showToast } from './toast';

export function getCollections(username) {
return (dispatch) => {
Expand Down Expand Up @@ -47,8 +45,7 @@ export function createCollection(collection) {
dispatch(stopLoader());

const newCollection = response.data;
dispatch(setToastText(`Created "${newCollection.name}"`));
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
dispatch(showToast(`Created "${newCollection.name}"`));

const pathname = `/${newCollection.owner.username}/collections/${newCollection.id}`;
const location = { pathname, state: { skipSavingPath: true } };
Expand Down Expand Up @@ -80,8 +77,7 @@ export function addToCollection(collectionId, projectId) {

const collectionName = response.data.name;

dispatch(setToastText(`Added to "${collectionName}`));
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
dispatch(showToast(`Added to "${collectionName}`));

return response.data;
})
Expand Down Expand Up @@ -110,8 +106,7 @@ export function removeFromCollection(collectionId, projectId) {

const collectionName = response.data.name;

dispatch(setToastText(`Removed from "${collectionName}`));
dispatch(showToast(TOAST_DISPLAY_TIME_MS));
dispatch(showToast(`Removed from "${collectionName}`));

return response.data;
})
Expand Down
24 changes: 9 additions & 15 deletions client/modules/IDE/actions/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import browserHistory from '../../../browserHistory';
import apiClient from '../../../utils/apiClient';
import getConfig from '../../../utils/getConfig';
import * as ActionTypes from '../../../constants';
import { showToast, setToastText } from './toast';
import { showToast } from './toast';
import {
setUnsavedChanges,
justOpenedProject,
Expand Down Expand Up @@ -174,24 +174,21 @@ export function saveProject(
dispatch(projectSaveSuccess());
if (!autosave) {
if (state.ide.justOpenedProject && state.preferences.autosave) {
dispatch(showToast(5500));
dispatch(setToastText('Toast.SketchSaved'));
dispatch(showToast('Toast.SketchSaved', 5500));
setTimeout(
() => dispatch(setToastText('Toast.AutosaveEnabled')),
() => dispatch(showToast('Toast.AutosaveEnabled', 5500)),
1500
);
dispatch(resetJustOpenedProject());
} else {
dispatch(showToast(1500));
dispatch(setToastText('Toast.SketchSaved'));
dispatch(showToast('Toast.SketchSaved'));
}
}
})
.catch((error) => {
const { response } = error;
dispatch(endSavingProject());
dispatch(setToastText('Toast.SketchFailedSave'));
dispatch(showToast(1500));
dispatch(showToast('Toast.SketchFailedSave'));
if (response.status === 403) {
dispatch(showErrorModal('staleSession'));
} else if (response.status === 409) {
Expand Down Expand Up @@ -224,24 +221,21 @@ export function saveProject(
dispatch(projectSaveSuccess());
if (!autosave) {
if (state.preferences.autosave) {
dispatch(showToast(5500));
dispatch(setToastText('Toast.SketchSaved'));
dispatch(showToast('Toast.SketchSaved', 5500));
setTimeout(
() => dispatch(setToastText('Toast.AutosaveEnabled')),
() => dispatch(showToast('Toast.AutosaveEnabled')),
1500
);
dispatch(resetJustOpenedProject());
} else {
dispatch(showToast(1500));
dispatch(setToastText('Toast.SketchSaved'));
dispatch(showToast('Toast.SketchSaved'));
}
}
})
.catch((error) => {
const { response } = error;
dispatch(endSavingProject());
dispatch(setToastText('Toast.SketchFailedSave'));
dispatch(showToast(1500));
dispatch(showToast('Toast.SketchFailedSave'));
if (response.status === 403) {
dispatch(showErrorModal('staleSession'));
} else {
Expand Down
45 changes: 9 additions & 36 deletions client/modules/IDE/actions/toast.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,12 @@
import * as ActionTypes from '../../../constants';
import { setToast, hideToast } from '../reducers/toast';

export function hideToast() {
return {
type: ActionTypes.HIDE_TOAST
};
}
export { hideToast } from '../reducers/toast';

/**
* Temporary fix until #2206 is merged.
* Supports legacy two-action syntax:
* dispatch(setToastText('Toast.SketchFailedSave'));
* dispatch(showToast(1500));
* And also supports proposed single-action syntax with message and optional timeout.
* dispatch(showToast('Toast.SketchFailedSave'));
* dispatch(showToast('Toast.SketchSaved', 5500));
*/
export function showToast(textOrTime, timeout = 1500) {
return (dispatch) => {
let time = timeout;
if (typeof textOrTime === 'string') {
// eslint-disable-next-line no-use-before-define
dispatch(setToastText(textOrTime));
} else {
time = textOrTime;
}
dispatch({
type: ActionTypes.SHOW_TOAST
});
setTimeout(() => dispatch(hideToast()), time);
};
}
export const TOAST_DISPLAY_TIME_MS = 1500;

export function setToastText(text) {
return {
type: ActionTypes.SET_TOAST_TEXT,
text
};
}
export const showToast = (text, timeout = TOAST_DISPLAY_TIME_MS) => (
dispatch
) => {
dispatch(setToast(text));
setTimeout(() => dispatch(hideToast()), timeout);
};
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ AddToCollectionSketchList.propTypes = {
}).isRequired
};


export default AddToCollectionSketchList;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { find } from 'lodash';
import * as ProjectActions from '../../actions/project';
import * as ProjectsActions from '../../actions/projects';
import * as CollectionsActions from '../../actions/collections';
import * as ToastActions from '../../actions/toast';
import * as SortingActions from '../../actions/sorting';
import getSortedCollections from '../../selectors/collections';
import Loader from '../../../App/components/loader';
Expand Down Expand Up @@ -301,7 +300,6 @@ function mapDispatchToProps(dispatch) {
CollectionsActions,
ProjectsActions,
ProjectActions,
ToastActions,
SortingActions
),
dispatch
Expand Down
Loading