Giter Site home page Giter Site logo

learn-rtk-saga's Introduction

Mini Project - Student Management

react-router-dom @types/react-router-dom

/login: /admin: layout

/admin/* feature: /admin/dashboard feature: /admin/students

auth / authentication

  • login
  • sign up / register
  • forget password

CLICK LOGIN

  • Call API to login
  • Success --> redirect ADMIN
  • FAILED --> show ERROR

LOGIN LOGOUT

authSaga

LOOP

  • if logged in, watch LOGOUT
  • else watch LOGIN

LOGIN

  • call login API to get token + user info
  • set token to local storage
  • redirect to admin page

LOGOUT

  • clear token from local storage
  • redirect to login page

authSlice authSaga


Different ways to handle navigation in Redux Saga

  1. Watch redux store and make redirect on component
const function App() {
  const loginSuccess = useAppSelector(state => state.auth.loginSuccess)
  
  useEffect(() => {
    if (loginSuccess) {
      // redirect to admin page
    }
  }, [loginSuccess])

  // ...
}

--> Flow is fragmented, hard to control when you have more and more state.

  1. Using callbacks
  • This approach using non-serializable (callback) in action and dispatch to redux store which is NOT RECOMMENDED BY Redux Toolkit.
const function App() {
  const dispatch = useAppDispatch();
  
  const handleLoginSubmit = (values) => {
    dispatch(authActions.login({
      ...values,
      onSuccess: () => history.push('/admin'),
      onError: () => console.log('Notify error to user'),
    }))
  }

  // ...
}
  1. Using connected-react-router
  • Sync routings to redux.
  • Navigate by dispatching an action to redux store.
  • One thing to make sure, when route changes, it doesn't cause re-render our components.

--> We'll go with this solution for now.

Lib: connected-react-router + custom history

Handle loading / error in redux saga

  • RTK + Thunk: provide a way to await an async action right on component --> Handle loading/error on component easily

  • RTK + Saga: doesn't have a way to do so --> what to do?

imho, my suggestions:

  • LOADING: can based on redux store
  • ERROR: eliminate the usage as much as you can.

Considerations:

  • Trigger error toast from saga.
  • Consider to call API directly on component instead of going through saga.

Students

ROUTINGS

  • /admin/students: listing
  • /admin/students/add: add new student
  • /admin/students/:studentId: update a student

LISTING

  • Search by name
  • Filter by city
  • Sort by name, mark
  • Pagination

student slice state:

  • loading
  • list
  • pagination
  • filter { page: 1, limit: 10, ... }

ADD/EDIT

  • React Hook Form v7
  • Yup

ROUTINGS

  • /admin/students/add: add new student
  • /admin/students/:studentId: update a student

Student Form

  • Mode: Add/Edit
  • Initial values
  • Values
    • name: Text Input
    • age: Number Input
    • gender: Radio options
    • city: Select
    • mark: Number Input
  • Validations: all required
    • name: at least two words
    • age: >= 18
    • gender: male / female
    • city: required
    • mark: 0 -> 10
  • Submission: redirect to student list page after submitting successfully

learn-rtk-saga's People

Contributors

paulnguyen-mn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.