Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alte0 committed Jul 26, 2020
1 parent d234e93 commit 82d51ca
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 42 deletions.
10 changes: 0 additions & 10 deletions spa-react/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ import 'normalize.css';
import './Common.scss';

export class App extends Component {
constructor(props) {
super(props);

this.initialState = {
textSearch: '',
};

this.state = this.initialState;
}

componentDidMount() {
if (checkLoggedUser()) {
const userInfo = getCookie("userInfo").split(";");
Expand Down
32 changes: 15 additions & 17 deletions spa-react/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function setCookie(name, value, options = {}) {
document.cookie = updatedCookie;
}
/**
* Уддаление Cookie по её имени.
* Удаление Cookie по её имени.
* @param {String} name
*/
export const deleteCookie = (name) => {
Expand Down Expand Up @@ -102,8 +102,8 @@ export const getActiveTitleTasks = (url, textSearch="") => {
export const deleteTaskFromArrTasks = (tasks, idTask) => {
let copyTasks = [...tasks];
const indexTask = copyTasks.findIndex((task) => Number(task.task_id) === Number(idTask));

if (indexTask !== -1) {
// copyTasks[indexTask]["task_status"] = "1";
copyTasks.splice(indexTask, 1);
}

Expand Down Expand Up @@ -155,20 +155,18 @@ export const getActiveMenuLinks = (url) => {
}
}
/**
* Декадирование параметра поиска
* @param {String} urlOrigin
* Получаем текст для поиска из параметра q
* @param location
* @returns {string}
*/
export const decodeParamsSearchUrl = (urlOrigin) => {
if (!urlOrigin) {
return '';
}
const url = new URL(urlOrigin);
if (url.searchParams.has('q')) {
const searchTextEncode = url.searchParams.get('q');
return decodeURIComponent(searchTextEncode);
}
export const getTextInSearchParams = (location) => {
const { search } = location;
const searchParams = new URLSearchParams(search);
const searchTextEncode = searchParams.get("q") || '';
return decodeURIComponent(searchTextEncode);
}
/*
* Проверяет залогинился ли пользователь
* */
export const checkLoggedUser = ()=> (getCookie("userInfo") && getCookie("PHPSESSID"));
/**
* Проверяет залогинился ли пользователь
* @returns { Boolean }
*/
export const checkLoggedUser = () => (getCookie("userInfo") && getCookie("PHPSESSID"));
36 changes: 21 additions & 15 deletions spa-react/src/pages/page-tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,27 @@ import SearchByTasks from "../components/search-by-tasks/search-by-tasks";
import UserMenu from "../components/user-menu/user-menu";
import Tasks from "../components/tasks/tasks";
import Pagination from "../components/pagination/pagination";
import { getActiveTitleTasks, deleteTaskFromArrTasks, decodeParamsSearchUrl, checkLoggedUser } from "../helpers/helpers";
import {
getActiveTitleTasks,
deleteTaskFromArrTasks,
checkLoggedUser,
getTextInSearchParams
} from "../helpers/helpers";
import { Redirect } from "react-router-dom";
import { getMyTasks, getMyTasksDone, getDesignatedTasks, getDesignatedTasksDone, executeTask, getResultSearchText } from "../data/data";
import {
getMyTasks,
getMyTasksDone,
getDesignatedTasks,
getDesignatedTasksDone,
executeTask,
getResultSearchText
} from "../data/data";
import { TypeMessage, showMessage } from '../plugins/show-message';
import LoadingData from '../components/loading-data/loading-data';
import { connect } from "react-redux";
import { allTasks } from "../actions";
import { withRouter } from "react-router-dom";

const getTextInSearchParams = (location) => {
const { search } = location;
const searchParams = new URLSearchParams(search);
return searchParams.get("q");
}

class PageTasks extends Component {
constructor(props){
super(props);
Expand Down Expand Up @@ -45,7 +51,6 @@ class PageTasks extends Component {
}
}

//TODO - уточнить про пропсы.
shouldComponentUpdate(nextProps, nextState) {
const { url: currUrl } = this.props.match;
const { url: nextUrl } = nextProps.match;
Expand All @@ -67,7 +72,8 @@ class PageTasks extends Component {
return true;
}

return (nextState.loading !== this.state.loading) || (nextState.pageCurrentPagination !== this.state.pageCurrentPagination);
return (nextState.loading !== this.state.loading) ||
(nextState.pageCurrentPagination !== this.state.pageCurrentPagination);
}

render() {
Expand All @@ -83,18 +89,18 @@ class PageTasks extends Component {
} = this.props;

const { url } = match;

const { userId } = user;
const urlOrigin=`${window.location.origin}${location.search}`;

const textSearch = decodeParamsSearchUrl(urlOrigin);
const textSearch = getTextInSearchParams(location);

const {
pageCurrentPagination,
itemsTasks,
pagesCount
} = this.state;
const visibleTasks = tasks.length ? tasks.slice((pageCurrentPagination - 1) * itemsTasks, pageCurrentPagination * itemsTasks) : tasks;

const visibleTasks = tasks.length ?
tasks.slice((pageCurrentPagination - 1) * itemsTasks, pageCurrentPagination * itemsTasks) :
tasks;

return (
<React.Fragment>
Expand Down

0 comments on commit 82d51ca

Please sign in to comment.