Skip to content

Commit

Permalink
redux all tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
alte0 committed Jul 20, 2020
1 parent 5df27ae commit 3d1a646
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
4 changes: 3 additions & 1 deletion spa-react/src/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { getUserInfo } from './user';
import { fetchTask, toPerformTask } from './task';
import { allUsers } from "./users";
import { allTasks } from "./tasks";


export {
getUserInfo,
fetchTask,
toPerformTask,
allUsers
allUsers,
allTasks
};
1 change: 1 addition & 0 deletions spa-react/src/actions/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const allTasks = (payload) => ({ type: 'GET_TASKS', payload });
45 changes: 25 additions & 20 deletions spa-react/src/pages/page-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ import { getMyTasks, getMyTasksDone, getDesignatedTasks, getDesignatedTasksDone,
import { TypeMessage, showMessage } from '../plugins/show-message';
import LoadingData from '../components/loading-data/loading-data';
import { connect } from "react-redux";

import { allTasks } from "../actions";

class PageTasks extends Component {
constructor(props){
super(props);
this.initialState = {
tasks: [],
itemsTasks: 9,
pagesCount: 0,
pageCurrentPagination: 1,
Expand Down Expand Up @@ -57,6 +56,7 @@ class PageTasks extends Component {
}

const {
tasks,
url,
urlOrigin,
user
Expand All @@ -67,7 +67,6 @@ class PageTasks extends Component {
const textSearch = decodeParamsSearchUrl(urlOrigin);

const {
tasks,
pageCurrentPagination,
itemsTasks,
pagesCount
Expand Down Expand Up @@ -111,29 +110,27 @@ class PageTasks extends Component {
_getData(fn) {
this.setState({
loading: true,
tasks: this.initialState.tasks,
pageCurrentPagination: this.initialState.pageCurrentPagination
});

fn()
.then(tasks => {
if (tasks.msgsType === 'error') {
this.setState({
tasks: []
})
this.props.getTasksDispatch([])
return true
}

this.props.getTasksDispatch(tasks);

const lengthTasks = tasks.length;

this.setState((state) => {
const { itemsTasks } = state;

return {
tasks: tasks,
pagesCount: Math.ceil((lengthTasks / itemsTasks))
}
})
});
})
.catch(e => {
console.error(e);
Expand Down Expand Up @@ -168,14 +165,9 @@ class PageTasks extends Component {
.then(result => {
showMessage(result.msgsType, '', result.textMsgs);
if (result.msgsType === 'success') {
this.setState((state) => {
const newTasks = deleteTaskFromArrTasks(this.props.tasks, idTask);

return {
tasks: deleteTaskFromArrTasks(state.tasks, idTask),
}
})

return true
this.props.getTasksDispatch(newTasks);
}
})
.catch(e => {
Expand All @@ -194,23 +186,25 @@ class PageTasks extends Component {

_getSearchData() {
const textSearch = decodeParamsSearchUrl(this.props.urlOrigin) || '';

getResultSearchText(textSearch)
.then(tasks => {
if (tasks.msgsType === 'warning') {
this.props.getTasksDispatch([]);
this.setState({
tasks: this.initialState.tasks,
pageCurrentPagination: this.initialState.pageCurrentPagination
})
return true
}

this.props.getTasksDispatch(tasks);

const lengthTasks = tasks.length;

this.setState((state) => {
const { itemsTasks } = state;

return {
tasks: tasks,
pagesCount: Math.ceil((lengthTasks / itemsTasks))
}
})
Expand All @@ -225,6 +219,17 @@ class PageTasks extends Component {
}
}

const mapStateToProps = (state) => ({ user: state.user })
const mapStateToProps = (state) => ({
user: state.user,
tasks: state.tasks
})

const mapDispatchToProps = (dispatch) => {
return {
getTasksDispatch: (tasks) => {
dispatch(allTasks(tasks));
}
}
}

export default connect(mapStateToProps)(PageTasks);
export default connect(mapStateToProps, mapDispatchToProps)(PageTasks);
4 changes: 3 additions & 1 deletion spa-react/src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { combineReducers } from 'redux';
import user from "./user";
import task from "./task";
import users from "./users";
import tasks from "./tasks";



export default combineReducers({
user,
task,
users
users,
tasks
})
10 changes: 10 additions & 0 deletions spa-react/src/reducers/tasks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const reducerTasks = (state = [], action ) => {
switch (action.type) {
case 'GET_TASKS':
return action.payload;
default:
return state;
}
}

export default reducerTasks;

0 comments on commit 3d1a646

Please sign in to comment.