diff --git a/src/actions.js b/src/actions.js index 31bbec7..71aa104 100644 --- a/src/actions.js +++ b/src/actions.js @@ -265,9 +265,8 @@ export function fetchInsureeMutation(mm, clientMutationId) { ); return graphql(payload, "INSUREE_INSUREE"); } - -export function fetchInsureeOfficers(mm) { - const payload = formatPageQuery("insureeOfficers", null, mm.getRef("insuree.InsureeOfficerPicker.projection")); +export function fetchInsureeOfficers(mm, filters) { + const payload = formatPageQuery("insureeOfficers", filters, mm.getRef("insuree.InsureeOfficerPicker.projection")); return graphql(payload, "INSUREE_INSUREE_OFFICERS"); } diff --git a/src/components/InsureeAddress.js b/src/components/InsureeAddress.js index 40fe17f..25a42cc 100644 --- a/src/components/InsureeAddress.js +++ b/src/components/InsureeAddress.js @@ -22,6 +22,7 @@ const useStyles = makeStyles((theme) => ({ const InsureeAddress = ({ onChangeLocation, onChangeAddress, + onChangeSameLocationCheckbox, readOnly, value, }) => { @@ -32,6 +33,13 @@ const InsureeAddress = ({ const [location, setLocation] = useState(true); const [address, setAddress] = useState(true); + const handleLocationChange = (e) => { + const checked = e.target.checked; + onChangeSameLocationCheckbox(checked) + setLocation(checked); + + }; + return ( @@ -41,7 +49,7 @@ const InsureeAddress = ({ color="primary" checked={location} disabled={readOnly} - onChange={(e) => setLocation((prevState) => !prevState)} + onChange={(e) =>handleLocationChange(e)} /> } label={formatMessage("Insuree.currentVillage.sameAsFamily")} diff --git a/src/components/InsureeAvatar.js b/src/components/InsureeAvatar.js index e35df4d..74f3d59 100644 --- a/src/components/InsureeAvatar.js +++ b/src/components/InsureeAvatar.js @@ -17,7 +17,7 @@ const styles = (theme) => ({ }); const InsureeAvatar = (props) => { - const { photo, classes, className, withMeta = false, readOnly, onChange } = props; + const { photo, classes, className, withMeta = false, readOnly, onChange, locationId } = props; const modulesManager = useModulesManager(); const { formatMessage } = useTranslations("insuree", modulesManager); @@ -79,6 +79,7 @@ const InsureeAvatar = (props) => { readOnly={readOnly} required={isRequired} onChange={(v) => onChange({ ...photo, officerId: v?.id })} + locationId = {locationId} /> diff --git a/src/components/InsureeMasterPanel.js b/src/components/InsureeMasterPanel.js index 705890f..484fbb9 100644 --- a/src/components/InsureeMasterPanel.js +++ b/src/components/InsureeMasterPanel.js @@ -77,6 +77,21 @@ class InsureeMasterPanel extends FormPanel { ); + getLocationId = (insuree, family) => { + const { sameLocation } = this.props.edited || {}; + const { edited }= this.props || {} + if (sameLocation == false) { + if (insuree?.currentVillage?.id) return insuree.currentVillage.id; + if (family?.headInsuree?.currentVillage?.id) return family.headInsuree.currentVillage.id; + if (edited?.currentVillage?.id ) return edited.currentVillage.id; + } + + if (family?.location?.id) return family.location.id; + if (insuree?.family?.location?.id) return insuree.family.location.id; + + return ""; +}; + render() { const { intl, @@ -89,8 +104,9 @@ class InsureeMasterPanel extends FormPanel { edited_id, isSubFamily, insuree, + family } = this.props; - + const locationId = this.getLocationId(insuree, family); return ( @@ -226,6 +242,7 @@ class InsureeMasterPanel extends FormPanel { readOnly={readOnly} onChangeLocation={(v) => this.updateAttribute("currentVillage", v)} onChangeAddress={(v) => this.updateAttribute("currentAddress", v)} + onChangeSameLocationCheckbox={(v) =>this.updateAttribute("sameLocation", v)} /> @@ -360,6 +377,7 @@ class InsureeMasterPanel extends FormPanel { readOnly={readOnly} withMeta={true} onChange={(v) => this.updateAttribute("photo", !!v ? v : null)} + locationId={locationId} /> { - !this.props.fetchingInsureeOfficers && this.props.fetchInsureeOfficers(this.props.modulesManager); + !this.props.fetchingInsureeOfficers && this.props.fetchInsureeOfficers(this.props.modulesManager, filters); }, Math.floor(Math.random() * 300)); } } + componentDidUpdate(prevProps) { + // Recharger les données si locationId change + if (this.props.locationId !== prevProps.locationId) { + const { locationId } = this.props + const filters = []; + if (locationId != undefined && locationId != "" ) { + filters.push(`locationId:"${decodeId(locationId)}"`) + } + this.props.fetchInsureeOfficers(this.props.modulesManager, filters); + } + + if (this.isCurrentAdminEnrollmentOfficerActive == true && + this.props.insureeOfficers !== prevProps.insureeOfficers && + this.props.insureeOfficers && + this.props.insureeOfficers.length > 0 && this.isEnrollmentAdminOfficer(this.props.user, this.props.insureeOfficers)) { + this.props.onChange( + this.props.insureeOfficers[0], + this.formatSuggestion(this.props.insureeOfficers[0]) + ); + } + } + + formatSuggestion = (a) => { if (!a) return ""; @@ -44,6 +70,12 @@ class InsureeOfficer extends Component { return `${a.code} ${fullName}`.trim(); }; + isEnrollmentAdminOfficer = (user, insureeOfficers) => { + if (!insureeOfficers || !user) return false; + if (user.username.trim() === insureeOfficers[0].code.trim()) return true; + else return false + } + onSuggestionSelected = (v) => this.props.onChange(v, this.formatSuggestion(v)); render() { @@ -61,8 +93,9 @@ class InsureeOfficer extends Component { required = false, withNull = false, nullLabel = null, + user } = this.props; - let v = insureeOfficers ? insureeOfficers.filter((o) => parseInt(decodeId(o.id)) === value) : []; + let v = (insureeOfficers ? insureeOfficers.filter((o) => parseInt(decodeId(o.id)) === value) : []); v = v.length ? v[0] : null; return ( @@ -75,9 +108,9 @@ class InsureeOfficer extends Component { getSuggestions={this.insureeOfficers} getSuggestionValue={this.formatSuggestion} onSuggestionSelected={this.onSuggestionSelected} - value={v} + value={this.isCurrentAdminEnrollmentOfficerActive == true && this.isEnrollmentAdminOfficer(user, insureeOfficers) ? insureeOfficers[0] : v} reset={reset} - readOnly={readOnly} + readOnly={this.isCurrentAdminEnrollmentOfficerActive == true && this.isEnrollmentAdminOfficer(user, insureeOfficers) ? true : readOnly} required={required} selectThreshold={this.selectThreshold} withNull={withNull} @@ -94,6 +127,7 @@ const mapStateToProps = (state) => ({ fetchingInsureeOfficers: state.insuree.fetchingInsureeOfficers, fetchedInsureeOfficers: state.insuree.fetchedInsureeOfficers, errorInsureeOfficers: state.insuree.errorInsureeOfficers, + user: state.core.user }); const mapDispatchToProps = (dispatch) => { @@ -102,4 +136,4 @@ const mapDispatchToProps = (dispatch) => { export default withModulesManager( connect(mapStateToProps, mapDispatchToProps)(injectIntl(withTheme(withStyles(styles)(InsureeOfficer)))), -); +); \ No newline at end of file