diff --git a/app/javascript/gabsocial/actions/toasts.js b/app/javascript/gabsocial/actions/toasts.js new file mode 100644 index 00000000..4616c610 --- /dev/null +++ b/app/javascript/gabsocial/actions/toasts.js @@ -0,0 +1,38 @@ +import { + TOAST_TYPE_ERROR, + TOAST_TYPE_SUCCESS, +} from '../constants' + +export const TOAST_SHOW = 'TOAST_SHOW' +export const TOAST_DISMISS = 'TOAST_DISMISS' +export const TOAST_CLEAR = 'TOAST_CLEAR' + +export function dismissToast(alert) { + return { + type: TOAST_DISMISS, + alert, + } +} + +export function clearToast() { + return { + type: TOAST_CLEAR, + } +} + +function showToast(type, message) { + return { + type: TOAST_SHOW, + toastType: type, + message, + } +} + +export const showToastError = (message) => { + return showToast(TOAST_TYPE_ERROR, message) +} + +export const showToastSucess = (message) => { + console.log("showToastSucess:", message) + return showToast(TOAST_TYPE_SUCCESS, message) +} \ No newline at end of file diff --git a/app/javascript/gabsocial/reducers/index.js b/app/javascript/gabsocial/reducers/index.js index 11c97ff6..03e4c4de 100644 --- a/app/javascript/gabsocial/reducers/index.js +++ b/app/javascript/gabsocial/reducers/index.js @@ -39,6 +39,7 @@ import status_revisions from './status_revisions' import suggestions from './suggestions' import tenor from './tenor' import timelines from './timelines' +import toasts from './toasts' import user from './user' import user_lists from './user_lists' @@ -83,6 +84,7 @@ const reducers = { suggestions, tenor, timelines, + toasts, user, user_lists, } diff --git a/app/javascript/gabsocial/reducers/toasts.js b/app/javascript/gabsocial/reducers/toasts.js new file mode 100644 index 00000000..3d9c9560 --- /dev/null +++ b/app/javascript/gabsocial/reducers/toasts.js @@ -0,0 +1,25 @@ +import { + TOAST_SHOW, + TOAST_DISMISS, + TOAST_CLEAR, +} from '../actions/toasts' +import { Map as ImmutableMap, List as ImmutableList } from 'immutable' + +const initialState = ImmutableList([]) + +export default function toasts(state = initialState, action) { + switch(action.type) { + case TOAST_SHOW: + return state.push(ImmutableMap({ + key: state.size > 0 ? state.last().get('key') + 1 : 0, + message: action.message, + type: action.toastType, + })) + case TOAST_DISMISS: + return state.filterNot(item => item.get('key') === action.toast.key) + case TOAST_CLEAR: + return state.clear() + default: + return state + } +}