Commit 7fb56c62 authored by Ermolaev Timur's avatar Ermolaev Timur

#28 Создал редюсер и экшен на получение задач

parent 0e93993f
import { Container } from '@mui/material'; import { Container } from '@mui/material';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarBody'; import MonthCalendarBody from '../../components/MonthCalendarBody/MonthCalendarBody';
import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader'; import MonthCalendarHeader from '../../components/MonthCalendarHeader/MonthCalendarHeader';
import { fetchTasks } from '../../store/actions/tasksActions';
function MonthCalendar() { function MonthCalendar() {
const dispatch = useDispatch();
const { tasks } = useSelector(state => state.tasks);
const [month, setMonth] = useState('') const [month, setMonth] = useState('')
const [year, setYear] = useState('') const [year, setYear] = useState('')
const [worker, setWorker] = useState(''); const [worker, setWorker] = useState('');
...@@ -13,7 +18,9 @@ function MonthCalendar() { ...@@ -13,7 +18,9 @@ function MonthCalendar() {
useEffect(()=>{ useEffect(()=>{
setMonth(new Date().getMonth()) setMonth(new Date().getMonth())
setYear(new Date().getFullYear()) setYear(new Date().getFullYear())
},[]) dispatch(fetchTasks())
}, [dispatch])
console.log(tasks)
const onChangeWorkerHandler = (event) => { const onChangeWorkerHandler = (event) => {
setWorker(event.target.value); setWorker(event.target.value);
}; };
......
...@@ -5,6 +5,7 @@ import App from './App'; ...@@ -5,6 +5,7 @@ import App from './App';
import { configureStore } from '@reduxjs/toolkit'; import { configureStore } from '@reduxjs/toolkit';
import { Provider } from 'react-redux'; import { Provider } from 'react-redux';
import usersReducer from './store/reducers/usersReducer'; import usersReducer from './store/reducers/usersReducer';
import tasksReducer from './store/reducers/tasksReducer';
import axios from 'axios'; import axios from 'axios';
const localStorageMiddleware = ({getState}) => (next) => (action) => { const localStorageMiddleware = ({getState}) => (next) => (action) => {
...@@ -31,6 +32,7 @@ axios.interceptors.request.use(config=>{ ...@@ -31,6 +32,7 @@ axios.interceptors.request.use(config=>{
const store = configureStore({ const store = configureStore({
reducer: { reducer: {
users: usersReducer, users: usersReducer,
tasks: tasksReducer
}, },
preloadedState: loadFromLocalStorage(), preloadedState: loadFromLocalStorage(),
middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware) middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(localStorageMiddleware)
......
export const FETCH_TASKS_REQUEST = "FETCH_TASKS_REQUEST";
export const FETCH_TASKS_SUCCESS = "FETCH_TASKS_SUCCESS";
export const FETCH_TASKS_FAILURE = "FETCH_TASKS_FAILURE";
\ No newline at end of file
import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS } from "../actionTypes/tasksTypes";
import axios from '../../axiosPlanner'
const fetchTasksRequest = () => {
return {type: FETCH_TASKS_REQUEST}
};
const fetchTasksSuccess = (tasks) => {
return {type: FETCH_TASKS_SUCCESS, tasks}
};
const fetchTasksFailure = (error) => {
return {type: FETCH_TASKS_FAILURE, error}
};
export const fetchTasks = () => {
return async (dispatch) => {
dispatch(fetchTasksRequest());
try {
const response = await axios.get("/tasks");
console.log(response.data)
dispatch(fetchTasksSuccess(response.data))
} catch (error) {
dispatch(fetchTasksFailure(error.response.data));
}
}
}
import { FETCH_TASKS_FAILURE, FETCH_TASKS_REQUEST, FETCH_TASKS_SUCCESS } from "../actionTypes/tasksTypes";
const initialState = {
tasks: [],
loading: false,
error: null
};
const tasksReduсer = (state = initialState, action) => {
switch(action.type) {
case FETCH_TASKS_REQUEST:
return {...state, loading: true};
case FETCH_TASKS_SUCCESS:
return {...state, loading: false, tasks: action.tasks};
case FETCH_TASKS_FAILURE:
return {...state, loading: false, error: action.error};
default:
return state;
}
};
export default tasksReduсer;
\ No newline at end of file
...@@ -5,14 +5,14 @@ const initialState = { ...@@ -5,14 +5,14 @@ const initialState = {
name: 'Ivan', name: 'Ivan',
surname: 'Petrov', surname: 'Petrov',
email: 'test@gmail.com', email: 'test@gmail.com',
role: 'user' role: 'superuser'
}, },
registerError: null, registerError: null,
loginError: null, loginError: null,
loading: false loading: false
}; };
const usersReduser = (state = initialState, action) => { const usersReducer = (state = initialState, action) => {
switch(action.type) { switch(action.type) {
case REGISTER_USER_REQUEST: case REGISTER_USER_REQUEST:
return {...state, loading: true}; return {...state, loading: true};
...@@ -31,4 +31,4 @@ const usersReduser = (state = initialState, action) => { ...@@ -31,4 +31,4 @@ const usersReduser = (state = initialState, action) => {
} }
}; };
export default usersReduser; export default usersReducer;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment