-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from openimis/develop
v1.3
- Loading branch information
Showing
7 changed files
with
130 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "all", | ||
"printWidth": 120, | ||
"quoteProps": "preserve", | ||
"arrowParens": "always" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { graphql, formatPageQuery } from "@openimis/fe-core"; | ||
|
||
export function fetchProducts(mm, str) { | ||
let payload = formatPageQuery("productsStr", | ||
let payload = formatPageQuery( | ||
"productsStr", | ||
!!str && str.length && [`str:"${str}"`], | ||
mm.getRef("product.ProductPicker.projection") | ||
mm.getRef("product.ProductPicker.projection"), | ||
); | ||
return graphql(payload, 'PRODUCT_PRODUCT_PICKER'); | ||
} | ||
return graphql(payload, "PRODUCT_PRODUCT_PICKER"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,91 @@ | ||
import React, { Component } from "react"; | ||
import { connect } from "react-redux"; | ||
import { bindActionCreators } from "redux"; | ||
import { injectIntl } from 'react-intl'; | ||
import { injectIntl } from "react-intl"; | ||
import { formatMessage, AutoSuggestion, withModulesManager } from "@openimis/fe-core"; | ||
import { fetchProducts } from "../actions"; | ||
import _debounce from "lodash/debounce"; | ||
|
||
class ProductPicker extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.cache = props.modulesManager.getConf("fe-product", "cacheProducts", true); | ||
this.selectThreshold = props.modulesManager.getConf("fe-product", "ProductPicker.selectThreshold", 10); | ||
} | ||
|
||
constructor(props) { | ||
super(props); | ||
this.cache = props.modulesManager.getConf("fe-product", "cacheProducts", true); | ||
this.selectThreshold = props.modulesManager.getConf("fe-product", "ProductPicker.selectThreshold", 10); | ||
componentDidMount() { | ||
if (this.cache && !this.props.products) { | ||
// prevent loading multiple times the cache when component is | ||
// several times on tha page | ||
setTimeout(() => { | ||
!this.props.fetching && this.props.fetchProducts(this.props.modulesManager); | ||
}, Math.floor(Math.random() * 300)); | ||
} | ||
} | ||
|
||
componentDidMount() { | ||
if (this.cache && !this.props.products) { | ||
// prevent loading multiple times the cache when component is | ||
// several times on tha page | ||
setTimeout( | ||
() => { | ||
!this.props.fetching && this.props.fetchProducts(this.props.modulesManager); | ||
}, | ||
Math.floor(Math.random() * 300) | ||
); | ||
} | ||
} | ||
|
||
formatSuggestion = s => !!s ? `${s.code} ${s.name}` : ''; | ||
formatSuggestion = (s) => (!!s ? `${s.code} ${s.name}` : ""); | ||
|
||
getSuggestions = str => !!str && | ||
str.length >= this.props.modulesManager.getConf("fe-product", "productsMinCharLookup", 2) && | ||
this.props.fetchProducts(this.props.modulesManager, str); | ||
getSuggestions = (str) => | ||
!!str && | ||
str.length >= this.props.modulesManager.getConf("fe-product", "productsMinCharLookup", 2) && | ||
this.props.fetchProducts(this.props.modulesManager, str); | ||
|
||
debouncedGetSuggestion = _debounce( | ||
this.getSuggestions, | ||
this.props.modulesManager.getConf("fe-product", "debounceTime", 800) | ||
) | ||
debouncedGetSuggestion = _debounce( | ||
this.getSuggestions, | ||
this.props.modulesManager.getConf("fe-product", "debounceTime", 800), | ||
); | ||
|
||
onSuggestionSelected = v => this.props.onChange(v, this.formatSuggestion(v)); | ||
onSuggestionSelected = (v) => this.props.onChange(v, this.formatSuggestion(v)); | ||
|
||
render() { | ||
const { intl, products, withLabel = true, label, withPlaceholder = false, placeholder, value, reset, | ||
readOnly = false, required = false, | ||
withNull = false, nullLabel = null, | ||
filter = null | ||
} = this.props; | ||
let items = products | ||
if (!!filter) { | ||
items = this.props.filter(items) | ||
} | ||
return <AutoSuggestion | ||
items={items} | ||
label={!!withLabel && (label || formatMessage(intl, "product", "Product"))} | ||
placeholder={!!withPlaceholder ? placeholder || formatMessage(intl, "product", "ProductPicker.placehoder") : null} | ||
getSuggestions={this.cache ? null : this.debouncedGetSuggestion} | ||
getSuggestionValue={this.formatSuggestion} | ||
onSuggestionSelected={this.onSuggestionSelected} | ||
value={value} | ||
reset={reset} | ||
readOnly={readOnly} | ||
required={required} | ||
selectThreshold={this.selectThreshold} | ||
withNull={withNull} | ||
nullLabel={nullLabel || formatMessage(intl, "product", "product.ProductPicker.null")} | ||
/> | ||
render() { | ||
const { | ||
intl, | ||
products, | ||
withLabel = true, | ||
label, | ||
withPlaceholder = false, | ||
placeholder, | ||
value, | ||
reset, | ||
readOnly = false, | ||
required = false, | ||
withNull = false, | ||
nullLabel = null, | ||
filter = null, | ||
} = this.props; | ||
let items = products; | ||
if (!!filter) { | ||
items = this.props.filter(items); | ||
} | ||
return ( | ||
<AutoSuggestion | ||
items={items} | ||
label={!!withLabel && (label || formatMessage(intl, "product", "Product"))} | ||
placeholder={ | ||
!!withPlaceholder ? placeholder || formatMessage(intl, "product", "ProductPicker.placehoder") : null | ||
} | ||
getSuggestions={this.cache ? null : this.debouncedGetSuggestion} | ||
getSuggestionValue={this.formatSuggestion} | ||
onSuggestionSelected={this.onSuggestionSelected} | ||
value={value} | ||
reset={reset} | ||
readOnly={readOnly} | ||
required={required} | ||
selectThreshold={this.selectThreshold} | ||
withNull={withNull} | ||
nullLabel={nullLabel || formatMessage(intl, "product", "product.ProductPicker.null")} | ||
/> | ||
); | ||
} | ||
} | ||
|
||
const mapStateToProps = state => ({ | ||
products: state.product.products, | ||
fetching: state.product.fetchingProducts, | ||
const mapStateToProps = (state) => ({ | ||
products: state.product.products, | ||
fetching: state.product.fetchingProducts, | ||
}); | ||
|
||
const mapDispatchToProps = dispatch => { | ||
return bindActionCreators({ fetchProducts }, dispatch); | ||
const mapDispatchToProps = (dispatch) => { | ||
return bindActionCreators({ fetchProducts }, dispatch); | ||
}; | ||
|
||
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)( | ||
withModulesManager(ProductPicker))); | ||
export default injectIntl(connect(mapStateToProps, mapDispatchToProps)(withModulesManager(ProductPicker))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,40 @@ | ||
import { parseData, formatServerError, formatGraphQLError } from '@openimis/fe-core'; | ||
import { parseData, formatServerError, formatGraphQLError } from "@openimis/fe-core"; | ||
|
||
function reducer( | ||
state = { | ||
fetchingProducts: false, | ||
state = { | ||
fetchingProducts: false, | ||
fetchedProducts: false, | ||
errorProducts: null, | ||
products: null, | ||
}, | ||
action, | ||
) { | ||
switch (action.type) { | ||
case "PRODUCT_PRODUCT_PICKER_REQ": | ||
return { | ||
...state, | ||
fetchingProducts: true, | ||
fetchedProducts: false, | ||
products: null, | ||
errorProducts: null, | ||
products: null, | ||
}, | ||
action, | ||
) { | ||
switch (action.type) { | ||
case 'PRODUCT_PRODUCT_PICKER_REQ': | ||
return { | ||
...state, | ||
fetchingProducts: true, | ||
fetchedProducts: false, | ||
products: null, | ||
errorProducts: null, | ||
}; | ||
case 'PRODUCT_PRODUCT_PICKER_RESP': | ||
return { | ||
...state, | ||
fetchingProducts: false, | ||
fetchedProducts: true, | ||
products: parseData(action.payload.data.productsStr), | ||
errorProducts: formatGraphQLError(action.payload) | ||
}; | ||
case 'PRODUCT_PRODUCT_PICKER_ERR': | ||
return { | ||
...state, | ||
fetchingProducts: false, | ||
errorProducts: formatServerError(action.payload) | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; | ||
case "PRODUCT_PRODUCT_PICKER_RESP": | ||
return { | ||
...state, | ||
fetchingProducts: false, | ||
fetchedProducts: true, | ||
products: parseData(action.payload.data.productsStr), | ||
errorProducts: formatGraphQLError(action.payload), | ||
}; | ||
case "PRODUCT_PRODUCT_PICKER_ERR": | ||
return { | ||
...state, | ||
fetchingProducts: false, | ||
errorProducts: formatServerError(action.payload), | ||
}; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
export default reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"product.Product": "Product", | ||
"product.ProductPicker.placehoder": "Search Product…", | ||
"product.ProductPicker.null": "Any", | ||
"product.Product.none": "None" | ||
} | ||
"product.Product": "Product", | ||
"product.ProductPicker.placehoder": "Search Product…", | ||
"product.ProductPicker.null": "Any", | ||
"product.Product.none": "None" | ||
} |