Skip to content

Commit

Permalink
improve redux part
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroidoruido committed May 27, 2024
1 parent a3529b6 commit b99f819
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 18 deletions.
52 changes: 52 additions & 0 deletions docs/markdown/08-states/21-redux-toolkit.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,55 @@ store.dispatch(todosActions.add('Easier with Redux Toolkit!'));
```

<!-- .element: class="big-code" -->

##==##

<!-- .slide: class="with-code" -->

# Actions without slice?

```typescript
export const navigateTo = createAction<string>('navigateTo');
```

<!-- .element: class="big-code" -->

##==##

<!-- .slide: class="with-code" -->

# Extra reducer

```typescript [7,13|8,12|9-11|7-13]
export const todosSlice = createSlice({
name: 'todos',
initialState: { todos: [] },
reducers: {
/* ... */
},
extraReducers: (builder) => {
builder.addCase(navigateTo, (state, action) => {
if (action.payload === '/logout') {
state.todos = [];
}
});
},
});
```

<!-- .element: class="big-code" -->

##==##

<!-- .slide: class="with-code" -->

# Need a unique id?

```typescript
import { nanoid } from '@reduxjs/toolkit';

const defaultSizeId: string = nanoid();
const customSizeId: string = nanoid(10);
```

<!-- .element: class="big-code" -->
1 change: 1 addition & 0 deletions docs/markdown/08-states/25-lab-redux.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Setup the store
- Create the slice for the Carousel state
- Rewrite Carousel `useCarousel` to use Redux
- Pause and reset to the first person the carousel on navigate to another page

</small>

Expand Down
7 changes: 7 additions & 0 deletions steps/12-redux-solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ npm run 12-redux
- Edit only `useCarousel` from `src/components/Carousel.tsx`
- You should not change the API (states and functions) returned by the `useCarousel` hook, only change its implementation
- Use `useEffect` to dispatch new people array to the store

4. Pause and reset to the first person the carousel on navigate to another page

- Create in `src/store/actions.ts` an action `navigateTo`
- Dispatch this action at every router navigation
- Add an `extraReducer` to `carouselSlice` to pause and reset the carousel state when navigating outside the home page (pathname === '/')
- play when navigating to home page
51 changes: 34 additions & 17 deletions steps/12-redux-solution/src/pages/router.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
import { RouterProvider, createBrowserRouter, useParams } from 'react-router-dom';
import { Outlet, RouterProvider, createBrowserRouter, useLocation, useParams } from 'react-router-dom';
import { HomePage } from './HomePage';
import { PeopleGridPage } from './PeopleGridPage';
import { NotFoundPage } from './NotFoundPage';
import { EditPeoplePage } from './EditPersonPage';
import { useDispatch } from 'react-redux';
import { navigateTo } from '../store/actions';
import { useEffect } from 'react';

export const router = createBrowserRouter([
{
path: '/',
Component: HomePage,
},
{
path: 'people',
Component: PeopleGridPage,
},
{
path: 'people/:id',
Component: () => {
const { id } = useParams();
return <EditPeoplePage id={id ?? ''} />;
},
},
{
path: '*',
Component: NotFoundPage,
Component: ConnectRouterToRedux,
children: [
{
index: true,
Component: HomePage,
},
{
path: 'people',
Component: PeopleGridPage,
},
{
path: 'people/:id',
Component: () => {
const { id } = useParams();
return <EditPeoplePage id={id ?? ''} />;
},
},
{
path: '*',
Component: NotFoundPage,
},
],
},
]);

Expand All @@ -33,3 +42,11 @@ if (import.meta.hot) {
export function AppRouter() {
return <RouterProvider router={router} />;
}

function ConnectRouterToRedux() {
const location = useLocation();
const dispatch = useDispatch();
dispatch(navigateTo(location));

return <Outlet />;
}
4 changes: 4 additions & 0 deletions steps/12-redux-solution/src/store/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createAction } from '@reduxjs/toolkit';
import { Location } from 'react-router-dom';

export const navigateTo = createAction<Location>('navigateTo');
14 changes: 13 additions & 1 deletion steps/12-redux-solution/src/store/carousel-slice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';
import type { RootState } from './index';
import { navigateTo } from './actions';

type CarouselStatus = 'PLAY' | 'PAUSE';

Expand All @@ -10,7 +11,7 @@ export interface CarouselReducerState {
status: CarouselStatus;
}

const initialState: CarouselReducerState = { people: [], currentIndex: -1, currentPerson: undefined, status: 'PLAY' };
const initialState: CarouselReducerState = { people: [], currentIndex: -1, currentPerson: undefined, status: 'PAUSE' };

export const carouselSlice = createSlice({
name: 'carousel',
Expand All @@ -36,6 +37,17 @@ export const carouselSlice = createSlice({
state.status = 'PAUSE';
},
},
extraReducers(builder) {
builder.addCase(navigateTo, (state, action) => {
if (action.payload.pathname === '/') {
state.status = 'PLAY';
} else {
state.currentIndex = 0;
state.currentPerson = state.people[0];
state.status = 'PAUSE';
}
});
},
});

// Action creators are generated for each case reducer function
Expand Down
7 changes: 7 additions & 0 deletions steps/12-redux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ npm run 12-redux
- Edit only `useCarousel` from `src/components/Carousel.tsx`
- You should not change the API (states and functions) returned by the `useCarousel` hook, only change its implementation
- Use `useEffect` to dispatch new people array to the store

4. Pause and reset to the first person the carousel on navigate to another page

- Create in `src/store/actions.ts` an action `navigateTo`
- Dispatch this action at every router navigation
- Add an `extraReducer` to `carouselSlice` to pause and reset the carousel state when navigating outside the home page (pathname === '/')
- play when navigating to home page

0 comments on commit b99f819

Please sign in to comment.