Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #1145 #1148

Merged
merged 2 commits into from
Mar 4, 2024
Merged
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
81 changes: 58 additions & 23 deletions src/adminApp/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ const formatLang = lang =>
.map(lang => lang.name)
.join("");

const SubjectTypeSyncAttributeShow = ({ subjectType, ...props }) => (
const SubjectTypeSyncAttributeShow = ({ subjectType, syncConceptValueMap, ...props }) => (
<div style={{ marginTop: 8, padding: 10, border: "3px solid rgba(0, 0, 0, 0.05)" }}>
<Typography gutterBottom variant={"subtitle1"}>{`Sync settings for Subject Type: ${
subjectType.name
Expand All @@ -176,46 +176,42 @@ const SubjectTypeSyncAttributeShow = ({ subjectType, ...props }) => (
<ConceptSyncAttributeShow
subjectType={subjectType}
syncAttributeName={"syncAttribute1"}
syncConceptValueMap={syncConceptValueMap}
{...props}
/>
)}
{subjectType.syncAttribute2 && (
<ConceptSyncAttributeShow
subjectType={subjectType}
syncAttributeName={"syncAttribute2"}
syncConceptValueMap={syncConceptValueMap}
{...props}
/>
)}
</div>
);

const ConceptSyncAttributeShow = ({ subjectType, syncAttributeName, ...props }) => {
const ConceptSyncAttributeShow = ({
subjectType,
syncConceptValueMap,
syncAttributeName,
...props
}) => {
const syncSettings = get(props.record, ["syncSettings", subjectType.name], {});
const conceptUUID = get(syncSettings, [syncAttributeName]);
if (isEmpty(conceptUUID)) return null;

const [syncConceptValueMap, setSyncConceptValueMap] = useState(new Map());
useEffect(() => {
let isMounted = true;
const newValMap = new Map();

ConceptService.getAnswerConcepts(conceptUUID).then(content => {
content.forEach(val => newValMap.set(val.id, val.name));
if (isMounted) {
setSyncConceptValueMap(newValMap);
}
});
return () => {
isMounted = false;
};
}, [conceptUUID]);
return (
<div>
<span style={{ color: "rgba(0, 0, 0, 0.54)", fontSize: "12px", marginRight: 10 }}>
{startCase(syncAttributeName)}
</span>
{map(get(syncSettings, `${syncAttributeName}Values`, []), value => (
<Chip style={{ margin: "0.2em" }} label={syncConceptValueMap.get(value)} key={value} />
<Chip
style={{ margin: "0.2em" }}
label={syncConceptValueMap.get(value) || value}
key={value}
/>
))}
</div>
);
Expand Down Expand Up @@ -258,7 +254,11 @@ export const UserDetail = ({ user, hasEditUserPrivilege, ...props }) => {
/>
<LineBreak />
{map(syncAttributesData.subjectTypes, st => (
<SubjectTypeSyncAttributeShow subjectType={st} key={get(st, "name")} />
<SubjectTypeSyncAttributeShow
subjectType={st}
key={get(st, "name")}
syncConceptValueMap={syncAttributesData.syncConceptValueMap}
/>
))}
<FunctionField
label="Preferred Language"
Expand Down Expand Up @@ -450,21 +450,56 @@ const ConceptSyncAttribute = ({ subjectType, syncAttributeName, edit, ...props }

const initialSyncAttributes = { subjectTypes: [] };

const getSyncConceptValueMap = async sortedSubjectTypes => {
const syncConceptValueMap = new Map();
const codedConceptUUIDSet = new Set();
sortedSubjectTypes.forEach(subject => {
const syncAttribute1UUID =
subject.syncAttribute1 &&
subject.syncAttribute1.dataType === "Coded" &&
subject.syncAttribute1.id;
const syncAttribute2UUID =
subject.syncAttribute2 &&
subject.syncAttribute2.dataType === "Coded" &&
subject.syncAttribute2.id;
syncAttribute1UUID && codedConceptUUIDSet.add(syncAttribute1UUID);
syncAttribute2UUID && codedConceptUUIDSet.add(syncAttribute2UUID);
});

for (const conceptUUID of codedConceptUUIDSet) {
const content = await ConceptService.getAnswerConcepts(conceptUUID);
content.forEach(val => {
syncConceptValueMap.set(val.id, val.name);
});
}
return syncConceptValueMap;
};

function fetchSyncAttributeData(setSyncAttributesData) {
useEffect(() => {
let isMounted = true;
http.get("/subjectType/syncAttributesData").then(res => {
const {
subjectTypes,
anySubjectTypeDirectlyAssignable,
anySubjectTypeSyncByLocation
} = res.data;
const sortedSubjectTypes = sortBy(subjectTypes, "id");
setSyncAttributesData({
subjectTypes: sortedSubjectTypes,
anySubjectTypeDirectlyAssignable,
anySubjectTypeSyncByLocation

getSyncConceptValueMap(sortedSubjectTypes).then(syncConceptValueMap => {
if (isMounted) {
setSyncAttributesData({
subjectTypes: sortedSubjectTypes,
anySubjectTypeDirectlyAssignable,
anySubjectTypeSyncByLocation,
syncConceptValueMap
});
}
});
});
return () => {
isMounted = false;
};
}, []);
}

Expand Down