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

Refactor queries to ensure order direction in ListAgents, ListComponents, and ListServices #33

Merged
merged 9 commits into from
Jun 15, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ const getAllAndMyAgentsQuery = (currentPage, ownerAddress = null) => {
return gql`
{
units(
first: ${TOTAL_VIEW_COUNT},
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)},
first: ${TOTAL_VIEW_COUNT}
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)}
where: {
packageType: agent
${ownerAddress ? `owner_contains_nocase: "${ownerAddress}"` : ''}
},
}
orderBy: tokenId
orderDirection: desc
) ${UNIT_FIELDS}
}
`;
Expand All @@ -43,6 +44,7 @@ const getAgentsBySearchQuery = (searchValue, ownerAddress = null) => {
]
}
orderBy: tokenId
orderDirection: desc
) ${UNIT_FIELDS}
}
`;
Expand Down
4 changes: 2 additions & 2 deletions apps/autonolas-registry/components/ListAgents/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ export const getAgents = async (total, nextPage) => {
const allAgentsPromises = [];
const { first, last } = getFirstAndLastIndex(total, nextPage);
for (let i = first; i <= last; i += 1) {
allAgentsPromises.push(contract.methods.getUnit(`${i}`).call());
allAgentsPromises.push(contract.methods.getUnit(`${total - (i + first - 1)}`).call());
}

const agents = await Promise.allSettled(allAgentsPromises);
const results = await Promise.all(
agents.map(async (info, i) => {
const owner = await getAgentOwner(`${first + i}`);
const owner = await getAgentOwner(`${total - (i + first - 1)}`);
return { ...info.value, owner };
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ const getAllAndMyComponentsQuery = (currentPage, ownerAddress = null) => {
return gql`
{
units(
first: ${TOTAL_VIEW_COUNT},
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)},
first: ${TOTAL_VIEW_COUNT}
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)}
where: {
${componentPackageType}
${ownerAddress ? `owner_contains_nocase: "${ownerAddress}"` : ''}
},
}
orderBy: tokenId
orderDirection: desc
) ${UNIT_FIELDS}
}
`;
Expand All @@ -46,6 +47,7 @@ const getComponentsBySearchQuery = (searchValue, ownerAddress = null) => {
]
}
orderBy: tokenId
orderDirection: desc
) ${UNIT_FIELDS}
}
`;
Expand Down
4 changes: 2 additions & 2 deletions apps/autonolas-registry/components/ListComponents/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export const getComponents = async (total, nextPage) => {
const allComponentsPromises = [];
const { first, last } = getFirstAndLastIndex(total, nextPage);
for (let i = first; i <= last; i += 1) {
allComponentsPromises.push(contract.methods.getUnit(`${i}`).call());
allComponentsPromises.push(contract.methods.getUnit(`${total - (i + first - 1)}`).call());
}

const components = await Promise.allSettled(allComponentsPromises);
const results = await Promise.all(
components.map(async (info, i) => {
const owner = await getComponentOwner(`${first + i}`);
const owner = await getComponentOwner(`${total - (i + first - 1)}`);
return { ...info.value, owner };
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const getAllAndMyServicesQuery = (currentPage, ownerAddress = null) => {
return gql`
{
services(
first: ${TOTAL_VIEW_COUNT},
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)},
first: ${TOTAL_VIEW_COUNT}
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)}
orderBy: serviceId
orderDirection: desc
${
ownerAddress
? `where: { owner_contains_nocase: "${ownerAddress}" }`
Expand Down Expand Up @@ -67,8 +68,8 @@ const getServicesBySearchQuery = (
return gql`
{
services (
first: ${TOTAL_VIEW_COUNT},
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)},
first: ${TOTAL_VIEW_COUNT}
skip: ${TOTAL_VIEW_COUNT * (currentPage - 1)}
where: {
and: [
${
Expand All @@ -78,6 +79,7 @@ const getServicesBySearchQuery = (
]
}
orderBy: serviceId
orderDirection: desc
) ${SERVICE_FIELDS}
}
`;
Expand Down
7 changes: 5 additions & 2 deletions apps/autonolas-registry/components/ListServices/utils.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,14 @@ export const getServices = async (total, nextPage, fetchAll = false) => {
existsPromises.push(result);
}

existsPromises.reverse();

const existsResult = await Promise.allSettled(existsPromises);
// filter services which don't exists (deleted or destroyed)
const validTokenIds = [];

existsResult.forEach((item, index) => {
const serviceId = `${first + index}`;
const serviceId = `${total - (index + first - 1)}`;
if (item.status === 'fulfilled' && !!item.value) {
validTokenIds.push(serviceId);
}
Expand All @@ -70,7 +73,7 @@ export const getServices = async (total, nextPage, fetchAll = false) => {
}),
);

return results;
return results.sort((a, b) => b.id - a.id);
};

export const getFilteredServices = async (searchValue, account) => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26785,4 +26785,4 @@ zustand@^4.3.1:
resolved "https://registry.yarnpkg.com/zustand/-/zustand-4.5.2.tgz#fddbe7cac1e71d45413b3682cdb47b48034c3848"
integrity sha512-2cN1tPkDVkwCy5ickKrI7vijSjPksFRfqS6237NzT0vqSsztTNnQdHw9mmN7uBdk3gceVXU0a+21jFzFzAc9+g==
dependencies:
use-sync-external-store "1.2.0"
use-sync-external-store "1.2.0"
Loading