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

Use tags from API instead of mocked ones #278

Merged
merged 3 commits into from
Nov 22, 2019
Merged
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
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@patternfly/react-core": "3.115.2",
"@patternfly/react-icons": "^3.14.8",
"@patternfly/react-table": "2.23.8",
"@redhat-cloud-services/host-inventory-client": "1.0.18",
"@redhat-cloud-services/host-inventory-client": "1.0.39",
"abortcontroller-polyfill": "^1.2.1",
"apollo-boost": "^0.1.23",
"axios": "^0.19.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/inventory-general-info/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"@redhat-cloud-services/frontend-components": "*",
"@redhat-cloud-services/frontend-components-utilities": ">=0.0.7",
"@redhat-cloud-services/host-inventory-client": "1.0.18",
"@redhat-cloud-services/host-inventory-client": "1.0.39",
"@redhat-cloud-services/frontend-components-inventory": ">=0.0.13"
}
}
2 changes: 1 addition & 1 deletion packages/inventory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"dependencies": {
"@redhat-cloud-services/frontend-components": "*",
"@redhat-cloud-services/frontend-components-utilities": ">=0.0.7",
"@redhat-cloud-services/host-inventory-client": "1.0.19",
"@redhat-cloud-services/host-inventory-client": "1.0.39",
"axios": "^0.19.0"
}
}
8 changes: 3 additions & 5 deletions packages/inventory/src/EntityDetail.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,9 @@ class EntityDetails extends Component {
{ this.generateTop() }
{ this.generateFacts() }
{
localStorage.getItem('rhcs-tags') === 'true' && (
loaded ?
<TagWithDialog count={ entity.tagCount } systemId={ entity.id } /> :
<Skeleton size={SkeletonSize.sm}>&nbsp;</Skeleton>
)
loaded ?
<TagWithDialog count={ entity.tags.length } systemId={ entity.id } /> :
<Skeleton size={ SkeletonSize.sm }>&nbsp;</Skeleton>
}
<TagsModal />
</Fragment>
Expand Down
2 changes: 1 addition & 1 deletion packages/inventory/src/EntityTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class EntityTable extends React.Component {
return ([
...transforms || [],
...props && props.width ? [ cellWidth(props.width) ] : [],
...hasItems || rows.length <= 0 ? [] : [ sortable ]
...hasItems || rows.length <= 0 || (props && props.isStatic) ? [] : [ sortable ]
]);
}

Expand Down
23 changes: 7 additions & 16 deletions packages/inventory/src/TagsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,23 @@ import PropTypes from 'prop-types';
import { toggleTagModal } from './redux/actions';
import { TagModal, Skeleton, SkeletonSize } from '@redhat-cloud-services/frontend-components';

const TagsModal = ({ showTagDialog, tagsLoaded, tags, activeSystemTag, tagCount, onToggleTagModal }) => {
const TagsModal = ({ showTagDialog, tags, activeSystemTag, tagCount, onToggleTagModal }) => {
return (
<TagModal
width="auto"
isOpen={ showTagDialog }
toggleModal={() => onToggleTagModal()}
systemName={ `${activeSystemTag.display_name} (${tagCount})` }
rows={
tagsLoaded ?
tags.map(({ tagName, tagValue }) => ([ tagName, tagValue ])) :
[ ...Array(tagCount) ].map(() => ([
{ title: <Skeleton size={ SkeletonSize.md }>&nbsp;</Skeleton> },
{ title: <Skeleton size={SkeletonSize.md}>&nbsp;</Skeleton> }
]))
}
rows={ tags.map(({ value, namespace }) => ([ value, namespace ])) }
/>
);
};

TagsModal.propTypes = {
showTagDialog: PropTypes.bool,
tagsLoaded: PropTypes.bool,
tags: PropTypes.arrayOf(PropTypes.shape({
tagName: PropTypes.node,
tagValue: PropTypes.node
value: PropTypes.node,
namespace: PropTypes.node
})),
tagCount: PropTypes.number,
activeSystemTag: PropTypes.shape({
Expand All @@ -48,13 +40,12 @@ TagsModal.defaultProps = {
};

export default connect(({ entities, entityDetails }) => {
const { showTagDialog, tagsLoaded, tags, activeSystemTag, tagCount } = entities || entityDetails || {};
const { showTagDialog, activeSystemTag = { tags: [] } } = entities || entityDetails || {};
return ({
showTagDialog,
tagsLoaded,
tags,
tags: activeSystemTag.tags,
activeSystemTag,
tagCount
tagCount: activeSystemTag.tags.length
});
}, (dispatch) => ({
onToggleTagModal: () => dispatch(toggleTagModal(false))
Expand Down
36 changes: 25 additions & 11 deletions packages/inventory/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,28 @@ export const mapData = ({ facts = {}, ...oneResult }) => ({
.reduce(
(acc, curr) => ({ ...acc, ...(typeof curr !== 'string') ? curr : {} }), {}
)
},
...localStorage.getItem('rhcs-tags') === 'true' ? {
tagCount: Math.floor(Math.random() * Math.floor(11))
} : {}
}
});

export const mapTags = (data = { results: [] }, { orderBy, orderDirection } = {}) => {
if (data.results.length > 0) {
return hosts.apiHostGetHostTags(data.results.map(({ id }) => id), data.per_page, 1, orderBy, orderDirection)
.then(({ results: tags }) => ({
...data,
results: data.results.map(row => ({ ...row, tags: tags[row.id] || [] }))
}))
.catch(() => ({
...data,
results: data.results.map(row => ({
...row,
tags: []
}))
}));
}

return data;
};

export function getEntities(items, {
controller,
hasItems,
Expand All @@ -43,6 +59,7 @@ export function getEntities(items, {

if (hasItems && items.length > 0) {
return hosts.apiHostGetHostById(items, undefined, perPage, page, undefined, undefined, { cancelToken: controller && controller.token })
.then(mapTags)
.then(({ results = [], ...data } = {}) => ({
...data,
filters,
Expand All @@ -58,12 +75,14 @@ export function getEntities(items, {
hostnameOrId && hostnameOrId.filter,
undefined,
undefined,
undefined,
perPage,
page,
orderBy,
orderDirection,
{ cancelToken: controller && controller.token }
)
.then((data) => mapTags(data, { orderBy, orderDirection }))
.then(({ results = [], ...data } = {}) => ({
...data,
filters,
Expand All @@ -85,13 +104,8 @@ export function getEntities(items, {

export const getEntitySystemProfile = (item) => hosts.apiHostGetHostSystemProfileById([ item ]);

export function getTags(systemId, count) {
return new Promise(res => setTimeout(() => res({
results: [ ...Array(count) ].map(() => ({
tagName: 'Tag name',
tagValue: `Some tag=${systemId}`
}))
}), 1000));
export function getTags(systemId) {
return hosts.apiHostGetHostTags(systemId).then(({ results }) => ({ results: Object.values(results)[0] }));
}

export function getAllTags() {
Expand Down
8 changes: 2 additions & 6 deletions packages/inventory/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,9 @@ export const editAnsibleHost = (id, value) => ({
}
});

export const loadTags = (systemId, count) => ({
export const loadTags = (systemId) => ({
type: ACTION_TYPES.LOAD_TAGS,
payload: getTags(systemId, count),
meta: {
systemId,
count
}
payload: systemId
});

export const toggleTagModal = (isOpen) => ({
Expand Down
37 changes: 12 additions & 25 deletions packages/inventory/src/redux/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ export const defaultState = { loaded: false, tagsLoaded: false, allTagsLoaded: f

const defaultColumns = [
{ key: 'display_name', title: 'Name', composed: [ 'facts.os_release', 'display_name' ] },
...localStorage.getItem('rhcs-tags') === 'true' ? [
{
key: 'tagCount',
title: 'Tags',
props: { width: 25 },
// eslint-disable-next-line react/display-name
renderFunc: (value, systemId) => <TagWithDialog count={value} systemId={ systemId }/>
}
] : [],
{
key: 'tags',
title: 'Tags',
props: { width: 25, isStatic: true },
// eslint-disable-next-line react/display-name
renderFunc: (value, systemId) => <TagWithDialog count={value.length} systemId={systemId} />
},
{ key: 'updated', title: 'Last sync', isTime: true, props: { width: 25 } }
];

Expand Down Expand Up @@ -115,23 +113,13 @@ function selectFilter(state, { payload: { item: { items, ...item }, selected } }
};
}

export function tagsLoading(state, { meta: { systemId, count } }) {
const activeSystemTag = state.rows ? state.rows.find(({ id }) => systemId === id) : state.entity;
export function showTags(state, { payload }) {
const activeSystemTag = state.rows ? state.rows.find(({ id }) => payload === id) : state.entity;
return {
...state,
tags: [],
showTagDialog: true,
tagsLoaded: false,
activeSystemTag,
tagCount: count
};
}

export function tagsFulfilled(state, { payload: { results } }) {
return {
...state,
tags: results,
tagsLoaded: true
tagsLoaded: true,
showTagDialog: true
};
}

Expand All @@ -155,8 +143,7 @@ export default {
[ACTION_TYPES.ALL_TAGS_PENDING]: (state) => ({ ...state, allTagsLoaded: false }),
[ACTION_TYPES.LOAD_ENTITIES_PENDING]: entitiesPending,
[ACTION_TYPES.LOAD_ENTITIES_FULFILLED]: entitiesLoaded,
[ACTION_TYPES.LOAD_TAGS_PENDING]: tagsLoading,
[ACTION_TYPES.LOAD_TAGS_FULFILLED]: tagsFulfilled,
[ACTION_TYPES.LOAD_TAGS]: showTags,
[UPDATE_ENTITIES]: entitiesLoaded,
[SHOW_ENTITIES]: (state, action) => entitiesLoaded(state, {
payload: {
Expand Down
5 changes: 2 additions & 3 deletions packages/inventory/src/redux/entityDetails.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ACTION_TYPES, APPLICATION_SELECTED, TOGGLE_TAG_MODAL } from './action-types';
import { tagsLoading, tagsFulfilled, toggleTagModal } from './entities';
import { showTags, toggleTagModal } from './entities';
export const defaultState = { loaded: false };

function entityDetailPending(state) {
Expand Down Expand Up @@ -29,7 +29,6 @@ export default {
[ACTION_TYPES.LOAD_ENTITY_PENDING]: entityDetailPending,
[ACTION_TYPES.LOAD_ENTITY_FULFILLED]: entityDetailLoaded,
[APPLICATION_SELECTED]: onApplicationSelected,
[ACTION_TYPES.LOAD_TAGS_PENDING]: tagsLoading,
[ACTION_TYPES.LOAD_TAGS_FULFILLED]: tagsFulfilled,
[ACTION_TYPES.LOAD_TAGS]: showTags,
[TOGGLE_TAG_MODAL]: toggleTagModal
};