Skip to content

Commit

Permalink
Fixes #36469 - Fix Ansible Roles and Variables sub-tabs pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
ronlavi2412 committed Jan 1, 2024
1 parent e1a7b13 commit 4f39f4f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { translate as __ } from 'foremanReact/common/I18n';

Expand All @@ -16,22 +16,27 @@ import './AnsibleVariableOverrides.scss';
const AnsibleVariableOverrides = ({ hostId, hostAttrs, history }) => {
const hostGlobalId = encodeId('Host', hostId);
const pagination = useCurrentPagination(history);
const [totalItems, setTotalItems] = useState(0);

const useFetchFn = () =>
useQuery(variableOverrides, {
variables: {
id: hostGlobalId,
match: `fqdn=${hostAttrs.name}`,
...useParamsToVars(history),
...useParamsToVars(history, totalItems),
},
fetchPolicy: 'network-only',
nextFetchPolicy: 'cache-first',
});

const renameData = data => ({
variables: data.host.ansibleVariablesWithOverrides.nodes,
totalCount: data.host.ansibleVariablesWithOverrides.totalCount,
});
const renameData = data => {
const totalCount = data.host.ansibleVariablesWithOverrides.totalCount;
if (totalItems === 0) setTotalItems(totalCount);
return ({
variables: data.host.ansibleVariablesWithOverrides.nodes,
totalCount,
})
};

return (
<AnsibleVariableOverridesTable
Expand Down
16 changes: 10 additions & 6 deletions webpack/helpers/pageParamsHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ export const useCurrentPagination = (

export const pageToVars = (
pagination,
keys = { page: 'page', perPage: 'per_page' }
) => ({
first: pagination[keys.page] * pagination[keys.perPage],
last: pagination[keys.perPage],
});
totalCount = 0,
keys = { page: 'page', perPage: 'per_page' },
) => {
return ({
first: pagination[keys.page] * pagination[keys.perPage],
last: pagination[keys.page] > 1 & totalCount > 0 ? totalCount - pagination[keys.perPage] : pagination[keys.perPage],
})
};

export const useParamsToVars = (
history,
totalCount,
keys = { page: 'page', perPage: 'per_page' }
) => pageToVars(useCurrentPagination(history, keys), keys);
) => pageToVars(useCurrentPagination(history, keys), totalCount, keys);

0 comments on commit 4f39f4f

Please sign in to comment.