Compare commits
4 Commits
developmen
...
backend-co
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1d6837e004 | ||
|
|
60de10633b | ||
|
|
902d339c6e | ||
|
|
86867c4e04 |
@@ -0,0 +1,7 @@
|
||||
.wiaas-contacts-table th{
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.wiaas-contacts-table tr.odd{
|
||||
background-color: #f9f9f9;
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
jQuery(document).ready(function($) {
|
||||
$('#wiaas_contacts_table').DataTable({
|
||||
"pageLength": 20,
|
||||
"lengthChange": false,
|
||||
"info": false,
|
||||
"filter": true,
|
||||
"order": [[ 0, "desc" ]]
|
||||
});
|
||||
} );
|
||||
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Wiaas_Admin_CL_Contacts
|
||||
*/
|
||||
class Wiaas_Admin_CL_Contacts {
|
||||
|
||||
/**
|
||||
* Displays table list of customers that are linked to commercial lead and brokers
|
||||
*/
|
||||
|
||||
public static function init() {
|
||||
add_action( 'admin_menu', array( __CLASS__, 'add_contacts_page' ), 9 );
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
|
||||
}
|
||||
|
||||
public static function enqueue_scripts() {
|
||||
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
|
||||
|
||||
wp_enqueue_style( 'wiaas_admin_cl_contacts', $plugin_url . '/assets/css/wiaas-admin-cl-contacts.css' );
|
||||
wp_enqueue_script( 'wiaas_admin_cl_contacts', $plugin_url . '/assets/js/wiaas-admin-cl-contacts.js' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Add customer menu page for commercial lead
|
||||
*/
|
||||
public static function add_contacts_page() {
|
||||
add_menu_page(
|
||||
__( 'Contacts', 'wiaas' ),
|
||||
__( 'Contacts', 'wiaas' ),
|
||||
'manage_wiaas_cl_customers',
|
||||
'wiaas-cl-contacts',
|
||||
array(__CLASS__, 'output_contacts'),
|
||||
'dashicons-list-view',
|
||||
'66.0' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render content for contacts menu page
|
||||
*/
|
||||
public static function output_contacts() {
|
||||
$organization_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
$related_customer_organizations = Wiaas_Shop::get_shop_customers($organization_id);
|
||||
$ids_of_user_organizations_to_show = [];
|
||||
|
||||
//add customer organizations related to CL
|
||||
foreach( $related_customer_organizations as $customer_organization){
|
||||
$ids_of_user_organizations_to_show[] = $customer_organization['customer_id'];
|
||||
}
|
||||
|
||||
//add brokers
|
||||
$brokers = wiaas_get_brokers();
|
||||
|
||||
foreach ($brokers as $broker_id => $broker_name) {
|
||||
$ids_of_user_organizations_to_show[] = $broker_id;
|
||||
}
|
||||
|
||||
|
||||
$args = array(
|
||||
'meta_key' => '_wiaas_organization_id',
|
||||
'meta_value' => $ids_of_user_organizations_to_show,
|
||||
'compare' => 'IN'
|
||||
);
|
||||
|
||||
$query = new WP_User_Query($args);
|
||||
|
||||
$list_of_users = $query->get_results();
|
||||
$contacts = [];
|
||||
|
||||
foreach ($list_of_users as $user){
|
||||
$roles_array = wiaas_get_organization_roles($user->_wiaas_organization_id);
|
||||
|
||||
$roles = '';
|
||||
foreach($roles_array as $role){
|
||||
$roles .= translate_user_role( wp_roles()->role_names[ $role ]) . ', ';
|
||||
}
|
||||
|
||||
$roles = substr($roles, 0, -2);
|
||||
|
||||
$contacts[] = array(
|
||||
'name' => $user->user_nicename,
|
||||
'email' => $user->user_email,
|
||||
'phone' => get_the_author_meta('phone', $user->ID),
|
||||
'roles' => $roles
|
||||
);
|
||||
}
|
||||
|
||||
require 'views/html-admin-cl-contacts-page.php';
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_CL_Contacts::init();
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
<div>
|
||||
<h1><?php esc_html_e( 'Contacts', 'wiaas' ); ?> </h1>
|
||||
|
||||
<br class="clear" />
|
||||
<div >
|
||||
<table id="wiaas_contacts_table" class="wiaas-contacts-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th><?php esc_html_e( 'Name', 'wiaas' ); ?></th>
|
||||
<th><?php esc_html_e( 'Email', 'wiaas' ); ?></th>
|
||||
<th><?php esc_html_e( 'Phone', 'wiaas' ); ?></th>
|
||||
<th><?php esc_html_e( 'Roles', 'wiaas' ); ?></th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
||||
<tbody id="wiaas-contacts-tbody">
|
||||
|
||||
<?php
|
||||
foreach ($contacts as $contact) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?php esc_html_e($contact['name'] ?: '-', 'wiaas'); ?></td>
|
||||
<td><a href="mailto:<?php esc_html_e($contact['email'] ?: '-', 'wiaas'); ?>"><?php esc_html_e($contact['email'] ?: '-', 'wiaas'); ?></a></td>
|
||||
<td><?php esc_html_e($contact['phone'] ?: '-', 'wiaas'); ?></td>
|
||||
<td><?php esc_html_e($contact['roles'] ?: '-', 'wiaas'); ?></td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
</div>
|
||||
@@ -24,6 +24,8 @@ class Wiaas_Admin_CL {
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-customers.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-contacts.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/class-wiaas-admin-cl-orders.php';
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-cl/wiaas-admin-cl-packages-ajax.php';
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_Contacts {
|
||||
|
||||
public static function init() {
|
||||
add_filter( 'manage_users_columns', array(__CLASS__, 'modify_users_screen_table_columns'), 999, 1 );
|
||||
|
||||
add_filter( 'manage_users_custom_column', array(__CLASS__, 'fill_custom_columns'), 10, 3);
|
||||
}
|
||||
|
||||
public static function modify_users_screen_table_columns( $columns ) {
|
||||
|
||||
$show_columns = array();
|
||||
|
||||
$show_columns['cb'] = $columns['cb'];
|
||||
$show_columns['username'] = $columns['username'];
|
||||
$show_columns['name'] = $columns['name'];
|
||||
$show_columns['email'] = $columns['email'];
|
||||
$show_columns['phone'] = __( 'Phone', 'wiaas' );
|
||||
$show_columns['role'] = $columns['role'];
|
||||
$show_columns['wiaas-user-organization'] = $columns['wiaas-user-organization'];
|
||||
|
||||
return $show_columns;
|
||||
|
||||
}
|
||||
|
||||
public static function fill_custom_columns ($empty, $column, $user_id){
|
||||
if ($column == 'phone'){
|
||||
return get_the_author_meta('phone', $user_id) ?: '-';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Wiaas_Admin_Contacts::init();
|
||||
@@ -113,6 +113,7 @@ class Wiaas_Admin_Organization {
|
||||
|
||||
if (! empty($organization_role)) {
|
||||
unset($role_list['none']);
|
||||
unset($role_list['user']);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -141,7 +142,7 @@ class Wiaas_Admin_Organization {
|
||||
* @param $admin_bar
|
||||
*/
|
||||
public static function add_role_switcher_menu($admin_bar) {
|
||||
if (get_current_user_id() === Wiaas_Authentication::SUPER_ADMIN_USER_ID) {
|
||||
if (is_super_admin()) {
|
||||
$roles = array( 'administrator' );
|
||||
} else {
|
||||
$organization_id = wiaas_get_current_user_organization_id();
|
||||
|
||||
@@ -36,6 +36,8 @@ class Wiaas_Admin {
|
||||
|
||||
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-dashboard.php';
|
||||
|
||||
require_once dirname( __FILE__) . '/admin/class-wiaas-admin-contacts.php';
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ class Wiaas_Package_Pricing {
|
||||
*/
|
||||
private static $pay_types = array(
|
||||
'purchase' => array(
|
||||
'title' => 'Purchase and monthly agreements',
|
||||
'title' => 'Purchase',
|
||||
'package_pay_period' => 0,
|
||||
'services_contract_period' => 0,
|
||||
'max_contract_period' => 36,
|
||||
@@ -23,14 +23,14 @@ class Wiaas_Package_Pricing {
|
||||
'labe'
|
||||
),
|
||||
'purchase_24' => array(
|
||||
'title' => 'Purchase and 36 month agreements',
|
||||
'title' => 'Purchase with 24M commitment',
|
||||
'package_pay_period' => 0,
|
||||
'services_contract_period' => 24,
|
||||
'max_contract_period' => 36,
|
||||
'period_unit' => 'month'
|
||||
),
|
||||
'managed_36' => array(
|
||||
'title' => '36 months financed rent and agreements',
|
||||
'title' => 'Managed service 36M rent',
|
||||
'package_pay_period' => 36,
|
||||
'services_contract_period'=> 36,
|
||||
'max_contract_period' => 36,
|
||||
|
||||
@@ -56,6 +56,15 @@ function wiaas_get_organizations_with_role($role) {
|
||||
return is_array($terms) ? $terms : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves list of broker organizations in [ 'id' => 'name' ] format
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_get_brokers() {
|
||||
return wiaas_get_organizations_with_role('administrator');
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves list of commercial lead organizations in [ 'id' => 'name' ] format
|
||||
*
|
||||
@@ -134,7 +143,7 @@ function wiaas_get_organization_info($organization_id) {
|
||||
'description' => $organization->description,
|
||||
'vat_code' => get_term_meta($organization_id, '_wiaas_organization_vat', true),
|
||||
'phone' => get_term_meta($organization_id, '_wiaas_organization_phone', true),
|
||||
'email' => $email
|
||||
'email' => $email
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -5,12 +5,9 @@ import HtmlClient from '../../helpers/HtmlClient';
|
||||
import {
|
||||
REQUEST_SHOP_PACKAGES,
|
||||
RECIEVE_SHOP_PACKAGES,
|
||||
SEARCH_SHOP_PACKAGES_REQUEST,
|
||||
SEARCH_SHOP_PACKAGES_RESULT,
|
||||
REQUEST_SHOPS,
|
||||
RECEIVE_SHOPS,
|
||||
SELECT_SHOP,
|
||||
SHOP_PAGE_SIZE
|
||||
SELECT_SHOP
|
||||
} from '../../constants/coMarketConstants';
|
||||
import { fromWCPackage } from '../../helpers/PackageHelper';
|
||||
|
||||
@@ -20,60 +17,30 @@ const requestShopPackages = () => ({
|
||||
type: REQUEST_SHOP_PACKAGES,
|
||||
isLoading: true
|
||||
});
|
||||
const recieveShopPackages = (packages, page = 1) => ({
|
||||
const recieveShopPackages = (json) => ({
|
||||
type: RECIEVE_SHOP_PACKAGES,
|
||||
isLoading: false,
|
||||
shopPackages: packages,
|
||||
page: page,
|
||||
shopPackages: json
|
||||
});
|
||||
|
||||
export const fetchShopPackages = (shop, page = 1) => {
|
||||
export const fetchShopPackages = (shop, search) => {
|
||||
return dispatch => {
|
||||
dispatch(requestShopPackages());
|
||||
let searchParam = search ? '?search=' +search : ''
|
||||
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}&page=${page}&per_page=${SHOP_PAGE_SIZE + 1}`,
|
||||
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}` + searchParam,
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data) {
|
||||
const packages = response.data.map(wcPackage => fromWCPackage(wcPackage));
|
||||
dispatch(recieveShopPackages(packages, page))
|
||||
dispatch(recieveShopPackages(response.data.map(wcPackage => fromWCPackage(wcPackage))))
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const searchShopPackagesRequest = () => ({
|
||||
type: SEARCH_SHOP_PACKAGES_REQUEST,
|
||||
isLoading: true
|
||||
});
|
||||
const searchShopPackagesResult = (packages) => ({
|
||||
type: SEARCH_SHOP_PACKAGES_RESULT,
|
||||
isLoading: false,
|
||||
shopPackages: packages,
|
||||
});
|
||||
|
||||
export const searchShopPackages = (shop, search) => {
|
||||
return dispatch => {
|
||||
dispatch(searchShopPackagesRequest());
|
||||
|
||||
return client.fetch({
|
||||
url: `${API_SERVER}/wp-json/wc/v2/products?shop_id=${shop.id}&search=${search}`,
|
||||
})
|
||||
.then(response => {
|
||||
if (response.data) {
|
||||
const packages = response.data.map(wcPackage => fromWCPackage(wcPackage));
|
||||
dispatch(searchShopPackagesResult(packages))
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
client.onError(error, dispatch);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const requestShops = () => ({
|
||||
type: REQUEST_SHOPS
|
||||
|
||||
@@ -2,9 +2,6 @@ const MODULE = 'CO_MARKET_';
|
||||
export const REQUEST_SHOP_PACKAGES = MODULE + 'REQUEST_SHOP_PACKAGES';
|
||||
export const RECIEVE_SHOP_PACKAGES = MODULE + 'RECIEVE_SHOP_PACKAGES';
|
||||
|
||||
export const SEARCH_SHOP_PACKAGES_REQUEST = MODULE + 'SEARCH_SHOP_PACKAGES_REQUEST';
|
||||
export const SEARCH_SHOP_PACKAGES_RESULT = MODULE + 'SEARCH_SHOP_PACKAGES_RESULT';
|
||||
|
||||
export const REQUEST_SHOPS = MODULE + 'REQUEST_SHOPS';
|
||||
export const RECEIVE_SHOPS = MODULE + 'RECEIVE_SHOPS';
|
||||
export const SELECT_SHOP = MODULE + 'SELECT_SHOP';
|
||||
@@ -108,9 +105,6 @@ export const coMarketTexts = {
|
||||
},
|
||||
buttons: {
|
||||
ADD_TO_CART: 'Add to cart',
|
||||
DETAILS: 'Details',
|
||||
LOAD_MORE: 'Load more'
|
||||
DETAILS: 'Details'
|
||||
}
|
||||
}
|
||||
|
||||
export const SHOP_PAGE_SIZE = 12;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import React, {Component} from 'react';
|
||||
import {connect} from 'react-redux';
|
||||
import {Row, Col, Input} from 'reactstrap';
|
||||
import {searchShopPackages, fetchShopPackages} from '../../actions/coMarket/coMarketPackagesActions';
|
||||
import {fetchShopPackages} from '../../actions/coMarket/coMarketPackagesActions';
|
||||
import {coMarketTexts} from '../../constants/coMarketConstants';
|
||||
|
||||
class CoMarketNavContainer extends Component {
|
||||
@@ -15,12 +15,9 @@ class CoMarketNavContainer extends Component {
|
||||
}
|
||||
|
||||
handleSearchChange(event) {
|
||||
const s = event.target.value;
|
||||
this.setState({searchValue: s});
|
||||
if (this.props.selectedShop && s) {
|
||||
this.props.dispatch(searchShopPackages(this.props.selectedShop, s));
|
||||
} else if (this.props.selectedShop) {
|
||||
this.props.dispatch(fetchShopPackages(this.props.selectedShop));
|
||||
this.setState({searchValue: event.target.value});
|
||||
if (this.props.selectedShop) {
|
||||
this.props.dispatch(fetchShopPackages(this.props.selectedShop, event.target.value));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,8 +42,7 @@ class CoMarketNavContainer extends Component {
|
||||
}
|
||||
|
||||
const mapStateToProps = (state) => ({
|
||||
selectedShop: state.coMarketPackagesReducer.selectedShop,
|
||||
shopPage: state.coMarketPackagesReducer.shopPage,
|
||||
selectedShop: state.coMarketPackagesReducer.selectedShop
|
||||
});
|
||||
|
||||
export default connect(mapStateToProps)(CoMarketNavContainer);
|
||||
|
||||
@@ -3,14 +3,12 @@ import {connect} from 'react-redux';
|
||||
import {
|
||||
Navbar,
|
||||
Row,
|
||||
Col,
|
||||
Button
|
||||
Col
|
||||
} from 'reactstrap';
|
||||
import ShopItem from './components/ShopItem.jsx';
|
||||
import WiaasBox from '../../mainComponents/box/WiaasBox.jsx';
|
||||
import CoMarketNavContainer from './CoMarketNavContainer.jsx';
|
||||
import {fetchShopPackages} from '../../actions/coMarket/coMarketPackagesActions';
|
||||
import {coMarketTexts} from "../../constants/coMarketConstants";
|
||||
|
||||
class CoMarketPackagesContainer extends Component {
|
||||
componentDidMount() {
|
||||
@@ -19,14 +17,8 @@ class CoMarketPackagesContainer extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
onLoadMore() {
|
||||
if (this.props.selectedShop) {
|
||||
this.props.dispatch(fetchShopPackages(this.props.selectedShop, this.props.shopPage + 1));
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {shopPackages, selectedShop, isLoading, shopHasMorePackages, shopSearch} = this.props;
|
||||
const {shopPackages, selectedShop, isLoading} = this.props;
|
||||
|
||||
return (
|
||||
<div id="co-market-shop">
|
||||
@@ -49,34 +41,22 @@ class CoMarketPackagesContainer extends Component {
|
||||
<Col xl="8" lg="12" md="12">
|
||||
<WiaasBox id="co-market-packages" customHeader={CoMarketNavContainer}>
|
||||
<Row>
|
||||
{
|
||||
(shopPackages) &&
|
||||
shopPackages.map((shopPackage, mapKey) => <ShopItem key={shopPackage.id}
|
||||
shopId={selectedShop.id}
|
||||
shopPackage={shopPackage}/>)
|
||||
}
|
||||
{
|
||||
isLoading &&
|
||||
<Col xl="12" className="loader">
|
||||
<i className="fa fa-spinner fa-spin fa-3x" aria-hidden="true"></i>
|
||||
</Col>
|
||||
}
|
||||
{
|
||||
(shopPackages && !isLoading) &&
|
||||
shopPackages.map((shopPackage, mapKey) => <ShopItem key={shopPackage.id}
|
||||
shopId={selectedShop.id}
|
||||
shopPackage={shopPackage}/>)
|
||||
}
|
||||
</Row>
|
||||
</WiaasBox>
|
||||
</Col>
|
||||
</Row>
|
||||
{
|
||||
shopHasMorePackages && !isLoading && !shopSearch && (
|
||||
<Row>
|
||||
<Col xl="8" lg="12" md="12">
|
||||
<Button
|
||||
size="lg" block outline
|
||||
className="shop-package-load-more-btn"
|
||||
onClick={() => {this.onLoadMore()}}
|
||||
>{coMarketTexts.buttons.LOAD_MORE}</Button>
|
||||
</Col>
|
||||
</Row>)
|
||||
}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -84,9 +64,6 @@ class CoMarketPackagesContainer extends Component {
|
||||
|
||||
const mapStateToProps = (state) => ({
|
||||
shopPackages: state.coMarketPackagesReducer.shopPackages,
|
||||
shopHasMorePackages: state.coMarketPackagesReducer.shopHasMorePackages,
|
||||
shopPage: state.coMarketPackagesReducer.shopPage,
|
||||
shopSearch: state.coMarketPackagesReducer.shopSearch,
|
||||
selectedShop: state.coMarketPackagesReducer.selectedShop,
|
||||
isLoading: state.coMarketPackagesReducer.isLoading
|
||||
});
|
||||
|
||||
@@ -9,6 +9,7 @@ class CoMarketCatalogSelect extends Component {
|
||||
super(props);
|
||||
|
||||
this.handleShopChange = this.handleShopChange.bind(this);
|
||||
this.handleSearchChange = this.handleSearchChange.bind(this);
|
||||
this.state = {
|
||||
searchValue : ''
|
||||
};
|
||||
@@ -40,6 +41,14 @@ class CoMarketCatalogSelect extends Component {
|
||||
this.props.dispatch(fetchShopPackages(shop));
|
||||
}
|
||||
|
||||
handleSearchChange(event) {
|
||||
this.setState({searchValue: event.target.value});
|
||||
|
||||
if (this.props.selectedShop) {
|
||||
this.props.dispatch(fetchShopPackages(this.props.selectedShop, event.target.value));
|
||||
}
|
||||
}
|
||||
|
||||
render() {
|
||||
const {shops, selectedShop, idPackage, activeSubmodule} = this.props;
|
||||
const isDisabled = (idPackage || this.props.activeModule === 'cart') ? true : false;
|
||||
|
||||
@@ -89,18 +89,6 @@
|
||||
font-weight: $font-weight;
|
||||
}
|
||||
|
||||
.shop-package-load-more-btn {
|
||||
border-radius: 0;
|
||||
color: #e25c56;
|
||||
font-size: 1rem;
|
||||
border: 1px solid rgba(0, 0, 0, 0.125);
|
||||
|
||||
&:hover, &:active, &:focus {
|
||||
background: $hoverColor;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
.search-layer{
|
||||
display:flex;
|
||||
align-items:center;
|
||||
|
||||
@@ -2,74 +2,20 @@ import {
|
||||
RECIEVE_SHOP_PACKAGES,
|
||||
RECEIVE_SHOPS,
|
||||
SELECT_SHOP,
|
||||
REQUEST_SHOP_PACKAGES,
|
||||
SHOP_PAGE_SIZE,
|
||||
SEARCH_SHOP_PACKAGES_REQUEST,
|
||||
SEARCH_SHOP_PACKAGES_RESULT,
|
||||
REQUEST_SHOP_PACKAGES
|
||||
} from '../../constants/coMarketConstants';
|
||||
|
||||
const moduleReducers = {};
|
||||
|
||||
moduleReducers[REQUEST_SHOP_PACKAGES] = (state, action) => {
|
||||
return Object.assign({}, state, {
|
||||
isLoading: action.isLoading,
|
||||
shopSearch: false,
|
||||
});
|
||||
};
|
||||
|
||||
moduleReducers[RECIEVE_SHOP_PACKAGES] = (state = {}, action) => {
|
||||
|
||||
// implement paging
|
||||
// paging is implemented in a way that requested number of packages is {page_size + 1}
|
||||
// this way if retrieved number of packages is greater than page size there may be more packages
|
||||
|
||||
const shopPage = action.page || 1;
|
||||
let shopPackages = [];
|
||||
let shopPackagesDiff = [];
|
||||
let retrievedShopPackages = action.shopPackages || [];
|
||||
|
||||
// append newly retrieved packages to existing ones if more packages are loaded
|
||||
if (state.shopPage && shopPage > state.shopPage) {
|
||||
shopPackages = state.loadedShopPackages || [];
|
||||
// get ignored packages from previous request
|
||||
const oldShopPackagesDiff = state.shopPackagesDiff || [];
|
||||
// append packages ignored previous time to the beginning
|
||||
retrievedShopPackages = oldShopPackagesDiff.concat(retrievedShopPackages);
|
||||
}
|
||||
|
||||
// if number of packages is greater than page size there may be more of them to retrieve
|
||||
const hasMorePages = retrievedShopPackages.length > SHOP_PAGE_SIZE;
|
||||
|
||||
// ignore all packages over limit of page size (they will be displayed at the beginning of the next request)
|
||||
while (retrievedShopPackages.length > SHOP_PAGE_SIZE) {
|
||||
shopPackagesDiff.push(retrievedShopPackages.pop());
|
||||
}
|
||||
|
||||
// append packages from this page to existing ones
|
||||
shopPackages = shopPackages.concat(retrievedShopPackages);
|
||||
|
||||
return Object.assign({}, state, {
|
||||
shopPackages: shopPackages,
|
||||
loadedShopPackages: shopPackages,
|
||||
shopPackagesDiff: shopPackagesDiff,
|
||||
shopPage: shopPage,
|
||||
shopSearch: false,
|
||||
shopHasMorePackages: hasMorePages,
|
||||
isLoading: action.isLoading
|
||||
});
|
||||
};
|
||||
|
||||
moduleReducers[SEARCH_SHOP_PACKAGES_REQUEST] = (state, action) => {
|
||||
moduleReducers[RECIEVE_SHOP_PACKAGES] = (state, action) => {
|
||||
return Object.assign({}, state, {
|
||||
shopSearch: true,
|
||||
isLoading: action.isLoading
|
||||
});
|
||||
};
|
||||
|
||||
moduleReducers[SEARCH_SHOP_PACKAGES_RESULT] = (state, action) => {
|
||||
return Object.assign({}, state, {
|
||||
shopPackages: action.shopPackages || [],
|
||||
shopSearch: true,
|
||||
shopPackages: action.shopPackages,
|
||||
isLoading: action.isLoading
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user