-
Notifications
You must be signed in to change notification settings - Fork 27
/
state-machines-list.js
114 lines (107 loc) 路 4.21 KB
/
state-machines-list.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import React from 'react';
import PropTypes from 'prop-types';
import { FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import { useApplicationContext } from '@commercetools-frontend/application-shell-connectors';
import { DotIcon } from '@commercetools-uikit/icons';
import LoadingSpinner from '@commercetools-uikit/loading-spinner';
import { Table } from '@commercetools-uikit/table';
import Spacings from '@commercetools-uikit/spacings';
import Text from '@commercetools-uikit/text';
import Constraints from '@commercetools-uikit/constraints';
import { NO_VALUE_FALLBACK } from '@commercetools-frontend/constants';
import PageBottomSpacer from '../page-bottom-spacer';
import { selectStateMachinesFromCache } from '../../reducers/cache';
import StateMachinesListConnector from '../state-machines-list-connector';
import messages from './messages';
import styles from './state-machines-list.mod.css';
export const columnsDefinition = [
{
key: 'key',
label: <FormattedMessage {...messages.columnStateMachineKey} />,
flexGrow: 1,
},
{
key: 'name',
label: <FormattedMessage {...messages.columnStateMachineName} />,
flexGrow: 1,
},
];
const getErrorMessage = error =>
error.stack || error.message || error.toString();
const StateMachinesList = props => {
const cachedStateMachine = useSelector(selectStateMachinesFromCache);
const cachedStateMachineObjectsCount = cachedStateMachine
? Object.keys(cachedStateMachine).length
: null;
const dataLocale = useApplicationContext(context => context.dataLocale);
return (
<Spacings.Inset scale="m">
<Spacings.Stack scale="m">
<Text.Headline as="h2" intlMessage={messages.title} />
<StateMachinesListConnector>
{({ isLoading, result, error, hasNoResults /* , refresh */ }) => {
if (isLoading) return <LoadingSpinner />;
if (error) return <div>{getErrorMessage(error)}</div>;
if (hasNoResults) {
return (
<div className={styles['empty-results']}>
<Text.Body intlMessage={messages.noResultsText} />
</div>
);
}
return (
<Spacings.Stack scale="m">
{cachedStateMachineObjectsCount !== null && (
<Spacings.Inline alignItems="center">
<DotIcon size="small" color="primary" />
<Text.Detail isItalic={true}>
<FormattedMessage
{...messages.objectsInCache}
values={{
count: cachedStateMachineObjectsCount,
}}
/>
</Text.Detail>
</Spacings.Inline>
)}
<Table
columns={columnsDefinition}
itemRenderer={item => {
const value = result.results[item.rowIndex][item.columnKey];
switch (item.columnKey) {
case 'name':
return value ? (
<Constraints.Horizontal constraint="m">
<Text.Wrap>
{value[dataLocale] || NO_VALUE_FALLBACK}
</Text.Wrap>
</Constraints.Horizontal>
) : null;
default:
return value;
}
}}
rowCount={result.count}
onRowClick={(_, rowIndex) => {
props.goToStateMachineDetail(result.results[rowIndex].id);
}}
shouldFillRemainingVerticalSpace={true}
items={result.results}
>
{/* TODO: add <Pagination> component */}
<PageBottomSpacer />
</Table>
</Spacings.Stack>
);
}}
</StateMachinesListConnector>
</Spacings.Stack>
</Spacings.Inset>
);
};
StateMachinesList.displayName = 'StateMachinesList';
StateMachinesList.propTypes = {
goToStateMachineDetail: PropTypes.func.isRequired,
};
export default StateMachinesList;