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

update hpt-pharmaceuticals andnon- hpt-pharmaceuticals #152

Merged
merged 2 commits into from
Nov 13, 2024
Merged
Changes from 1 commit
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
111 changes: 80 additions & 31 deletions pages/orgs/[org]/domains/[domain]/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ function OrgDomainsList() {
const [subdomainMenuAnchor, setSubdomainMenuAnchor] = useState(null);
const subdomainMenuOpen = Boolean(subdomainMenuAnchor);

const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL;

const fetchConcepts = (subdomain) => {
setIsLoadingConcepts(true);
setIsLoading(true);
Expand Down Expand Up @@ -108,39 +110,86 @@ function OrgDomainsList() {
};

const fetchDomainData = (page = 1) => {
fetch("/api/domains/" + domain + "?includeConcepts=true&page=" + page)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
if (data) {
setDomainData(data);
setSubDomainData(data.data?.subdomains);
let filteredConcepts = data?.data?.concepts?.filter(
(concept) => concept.type === "Concept"
);
if (data?.data?.subdomains && data?.data?.subdomains.length > 0) {
setSelectedSubdomain(data?.data?.subdomains[0]?.url);
fetchConcepts(data?.data?.subdomains[0]?.url);
// Check if the domain ID is `hpt-non-pharmaceuticals` or `hpt-pharmaceuticals`
let cascadeUrl = null;

if (domain === "hpt-non-pharmaceuticals") {
// Use cascade query URL for HPT-non-pharmaceuticals
cascadeUrl = `${API_BASE_URL}/orgs/MOH-KENYA/sources/PPB/concepts/10-00000-02/$cascade/?cascadeLevels=1&method=sourceToConcepts&view=hierarchy&reverse=true&includeRetired=false`;
} else if (domain === "hpt-pharmaceuticals") {
// Use cascade query URL for HPT-pharmaceuticals
cascadeUrl = `${API_BASE_URL}/orgs/MOH-KENYA/sources/PPB/concepts/10-00000-01/$cascade/?cascadeLevels=1&method=sourceToConcepts&view=hierarchy&reverse=true&includeRetired=false`;
Comment on lines +118 to +121
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucyjemutai - Can we extract this to the domains json and reference them here as an attribute of a domain instead of directly referencing them this way? like domain.cascadeUrl

}

if (cascadeUrl) {
fetch(cascadeUrl)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
if (data && data.entry) {
// Set subdomains from API response
const dynamicSubdomains = data.entry.entries.map((entry) => ({
id: entry.id,
display_name: entry.display_name,
url: entry.url,
}));

setSubDomainData(dynamicSubdomains);

if (dynamicSubdomains.length > 0) {
setSelectedSubdomain(dynamicSubdomains[0].url);
fetchConcepts(dynamicSubdomains[0].url);
}
} else {
setConcepts(filteredConcepts);
setCurrentConcepts(filteredConcepts);
setIsLoadingConcepts(false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These seem removed from the new if (cascadeUrl) condition. We need to keep the if block uniform with the else as pertains these setters

setSubDomainData([]);
}
setTotalPages(data?.data?.conceptsMeta?.pagecount ?? 1);
setRowsPerPage(data?.data?.conceptsMeta?.pagesize ?? 20);
setPage(data?.data?.conceptsMeta?.currentpage ?? 1);
}
})
.catch((err) => {
console.error("error::", err);
})
.finally(() => {
setIsLoading(false);
});
})
.catch((err) => {
console.error("error::", err);
})
.finally(() => {
setIsLoading(false);
});
} else {
// Default data fetching for other domains
fetch("/api/domains/" + domain + "?includeConcepts=true&page=" + page)
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.json();
})
.then((data) => {
if (data) {
setDomainData(data);
setSubDomainData(data.data?.subdomains); // Set default subdomains
let filteredConcepts = data?.data?.concepts?.filter(
(concept) => concept.type === "Concept"
);
if (data?.data?.subdomains && data?.data?.subdomains.length > 0) {
setSelectedSubdomain(data?.data?.subdomains[0]?.url);
fetchConcepts(data?.data?.subdomains[0]?.url);
} else {
setConcepts(filteredConcepts);
setCurrentConcepts(filteredConcepts);
setIsLoadingConcepts(false);
}
setTotalPages(data?.data?.conceptsMeta?.pagecount ?? 1);
setRowsPerPage(data?.data?.conceptsMeta?.pagesize ?? 20);
setPage(data?.data?.conceptsMeta?.currentpage ?? 1);
}
})
.catch((err) => {
console.error("error::", err);
})
.finally(() => {
setIsLoading(false);
});
}
};

useEffect(() => {
Expand Down