Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
10 changes: 9 additions & 1 deletion src/components/InsureeAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const useStyles = makeStyles((theme) => ({
const InsureeAddress = ({
onChangeLocation,
onChangeAddress,
onChangeSameLocationCheckbox,
readOnly,
value,
}) => {
Expand All @@ -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 (
<Grid container>
<Grid item xs={6} className={classes.item}>
Expand All @@ -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")}
Expand Down
3 changes: 2 additions & 1 deletion src/components/InsureeAvatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -79,6 +79,7 @@ const InsureeAvatar = (props) => {
readOnly={readOnly}
required={isRequired}
onChange={(v) => onChange({ ...photo, officerId: v?.id })}
locationId = {locationId}
/>
</Grid>
</Grid>
Expand Down
20 changes: 19 additions & 1 deletion src/components/InsureeMasterPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ class InsureeMasterPanel extends FormPanel {
</Grid>
);

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,
Expand All @@ -89,8 +104,9 @@ class InsureeMasterPanel extends FormPanel {
edited_id,
isSubFamily,
insuree,
family
} = this.props;

const locationId = this.getLocationId(insuree, family);
return (
<Grid container>
<Grid item xs={12}>
Expand Down Expand Up @@ -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)}
/>
</Grid>

Expand Down Expand Up @@ -360,6 +377,7 @@ class InsureeMasterPanel extends FormPanel {
readOnly={readOnly}
withMeta={true}
onChange={(v) => this.updateAttribute("photo", !!v ? v : null)}
locationId={locationId}
/>
</Grid>
<Contributions
Expand Down
50 changes: 42 additions & 8 deletions src/pickers/InsureeOfficerPicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,44 @@ class InsureeOfficer extends Component {
"renderLastNameFirst",
DEFAULT.RENDER_LAST_NAME_FIRST,
);
this.isCurrentAdminEnrollmentOfficerActive = props.modulesManager.getConf("fe-insuree", "isCurrentAdminEnrollmentOfficerActive", false);
}

componentDidMount() {
if (!this.props.fetchedInsureeOfficers) {
// prevent loading multiple times the cache when component is
// several times on tha page
if (!this.props.fetchedInsureeOfficers || !this.isCurrentAdminEnrollmentOfficerActive == false) {
const filters = [];
if(!!this.props.locationId && this.props.locationId != ""){
filters.push(`locationId:"${decodeId(this.props.locationId)}"`)
}
setTimeout(() => {
!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 "";

Expand All @@ -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() {
Expand All @@ -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 (
<Fragment>
Expand All @@ -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}
Expand All @@ -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) => {
Expand All @@ -102,4 +136,4 @@ const mapDispatchToProps = (dispatch) => {

export default withModulesManager(
connect(mapStateToProps, mapDispatchToProps)(injectIntl(withTheme(withStyles(styles)(InsureeOfficer)))),
);
);