superglue icon indicating copy to clipboard operation
superglue copied to clipboard

Add a helpful clear flash action for the flash slice

Open jho406 opened this issue 1 year ago • 0 comments

The default flash slice generated by superglue is very basic, and I find myself making edits to it soon after.

Mine looks like this:

import { createSlice } from '@reduxjs/toolkit';
import { saveResponse, beforeVisit } from '../actions';

export const flashSlice = createSlice({
  name: 'flash',
  initialState: {},
  reducers: {
    clearFlash() {
      return {};
    },
    flash(state, { payload }) {
      return payload;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(beforeVisit, (state, action) => {
      return {};
    });
    builder.addCase(saveResponse, (state, action) => {
      const { page } = action.payload;

      return {
        ...state,
        ...page.slices.flash,
      };
    });
  },
});

export const { clearFlash, flash } = flashSlice.actions;

It comes with two action creators, one clearFlash to clear the flash away, for example clicking on an X on a flash message, or creating a new one client side using flash. This seems like handy things to have out-of-the-box. Lets take what we have in that snippet and replace the generator's version.

jho406 avatar May 06 '24 15:05 jho406