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

Add improvements to Transaction Counter UI #766

Merged
merged 1 commit into from
Sep 30, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import CircularProgress from '@mui/material/CircularProgress';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';
import dayjs from 'dayjs';
import Configurations from 'Config';
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
import { AdapterDayjs } from '@mui/x-date-pickers/AdapterDayjs';
import { DatePicker } from '@mui/x-date-pickers/DatePicker';
Expand All @@ -36,6 +37,7 @@ import {
Document,
StyleSheet,
Font,
Image,
} from '@react-pdf/renderer';
import API from '../../data/api';

Expand Down Expand Up @@ -67,88 +69,181 @@ const styles = {

Font.register({
family: 'Roboto',
src: 'https://fonts.gstatic.com/s/roboto/v16/zN7GBFwfMP4uA6AR0HCoLQ.ttf',
fonts: [
{ src: 'https://fonts.gstatic.com/s/roboto/v16/zN7GBFwfMP4uA6AR0HCoLQ.ttf' },
{ src: 'https://fonts.gstatic.com/s/roboto/v30/KFOlCnqEu92Fr1MmWUlvAw.ttf', fontWeight: 'bold' },
],
});

const colors = {
wso2Orange: '#FF7300',
wso2Black: '#282828',
wso2White: '#FFFFFF',
Orange: '#FCAB68',
lightOrange: '#FFE0B2',
lightGray: '#F5F5F5',
};

const pdfStyles = StyleSheet.create({
page: {
padding: 60,
padding: 40,
fontSize: 12,
fontFamily: 'Roboto',
color: colors.wso2Black,
},
header: {
marginBottom: 15,
fontFamily: 'Roboto',
fontWeight: 'bold',
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
alignItems: 'center',
marginBottom: 30,
borderBottom: `2px solid ${colors.lightGray}`,
paddingBottom: 10,
},
logo: {
height: 'auto',
width: 120,
},
title: {
fontSize: 20,
fontSize: 24,
fontWeight: 'bold',
color: colors.wso2Orange,
},
section: {
marginBottom: 10,
table: {
display: 'flex',
flexDirection: 'column',
marginTop: 20,
width: '100%',
border: `1px solid ${colors.lightGray}`,
borderRadius: 5,
overflow: 'hidden',
},
row: {
display: 'flex',
flexDirection: 'row',
justifyContent: 'space-between',
padding: 10,
borderBottom: `1px solid ${colors.lightGray}`,
},
labelColumn: {
width: '40%',
textAlign: 'left',
fontSize: 12,
fontWeight: 'bold', // Making label text bold
padding: '5px 10px',
color: colors.wso2Black,
},
label: {
valueColumn: {
width: '60%',
textAlign: 'left',
fontSize: 12,
marginBottom: 1,
padding: '5px 10px',
color: colors.wso2Black,
},
alternateRow: {
backgroundColor: colors.Orange,
},
alternateRow2: {
backgroundColor: colors.lightOrange,
},
alternateRow3: {
backgroundColor: colors.lightGray,
},
reportContent: {
backgroundColor: '#F7F7F7',
borderRadius: 5,
marginBottom: 10,
},
footer: {
position: 'absolute',
bottom: 20,
left: 30,
right: 30,
borderTop: `1px solid ${colors.lightGray}`,
paddingTop: 10,
fontSize: 10,
textAlign: 'center',
},
});

const logoUrl = '/site/public/images/wso2_logo.png';

const TransactionPDF = ({ startDate, endDate, totalTransactions }) => (
<Document>
<Page style={pdfStyles.page}>
<View style={pdfStyles.header}>
<Text style={pdfStyles.title}>Usage Report</Text>
<Image
alt='WSO2 Logo'
src={Configurations.app.context + logoUrl}
style={pdfStyles.logo}
/>
<View>
<Text style={pdfStyles.title}>Usage Report</Text>
</View>
</View>

<View style={pdfStyles.section}>
<Text style={pdfStyles.label}>
Report generated on :
{' '}
{new Date().toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
{' '}
{new Date().toLocaleTimeString('en-GB')}
</Text>
</View>
<View style={pdfStyles.table}>
<View style={[pdfStyles.row, pdfStyles.alternateRow2]}>
<Text style={pdfStyles.labelColumn}>Report generated on :</Text>
<Text style={pdfStyles.valueColumn}>
{new Date().toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
{' '}
{new Date().toLocaleTimeString('en-GB')}
</Text>
</View>

<View style={pdfStyles.section}>
<Text style={pdfStyles.label}>
Reporting period :
{' '}
{new Date(startDate).toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
{' '}
to
{' '}
{new Date(endDate).toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
</Text>
<Text style={pdfStyles.label}>
Total transaction count :
{' '}
{totalTransactions}
</Text>
<View style={[pdfStyles.row, pdfStyles.alternateRow3]}>
<Text style={pdfStyles.labelColumn}>Reporting period :</Text>
<Text style={pdfStyles.valueColumn}>
{new Date(startDate).toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
{' '}
to
{' '}
{new Date(endDate).toLocaleDateString('en-GB', {
day: '2-digit', month: 'long', year: 'numeric',
})}
</Text>
</View>
<View style={[pdfStyles.row, pdfStyles.alternateRow]}>
<Text style={pdfStyles.labelColumn}>Total transaction count :</Text>
<Text style={pdfStyles.valueColumn}>
{totalTransactions}
</Text>
</View>
</View>

<Text style={pdfStyles.footer}>
{new Date().getFullYear()}
{' '}
WSO2 LLC. All Rights Reserved. | Report generated on
{' '}
{new Date().toLocaleDateString('en-GB')}
</Text>
</Page>
</Document>
);

export default function UsageReport() {
const [loading, setLoading] = useState(false);
const [transactionCount, setTransactionCount] = useState(0);
const [startDate, setStartDate] = React.useState(dayjs().subtract(1, 'month'));
const [endDate, setEndDate] = React.useState(dayjs());
const [startDate] = React.useState(dayjs().subtract(1, 'month'));
const [endDate] = React.useState(dayjs());

const [selectedStartDate, setSelectedStartDate] = useState(startDate);
const [selectedEndDate, setSelectedEndDate] = useState(endDate);

const fetchTransactionData = () => {
setLoading(true);

if (startDate && endDate) {
if (selectedStartDate && selectedEndDate) {
const api = new API();
api.getTransactionCount({ startTime: startDate.unix().toString(), endTime: endDate.unix().toString() })
api.getTransactionCount({
startTime: selectedStartDate.unix().toString(),
endTime: selectedEndDate.unix().toString(),
})
.then((result) => {
setTransactionCount(result.body.count);
})
Expand Down Expand Up @@ -187,28 +282,28 @@ export default function UsageReport() {
<LocalizationProvider dateAdapter={AdapterDayjs}>
<DatePicker
label='Start Date'
value={startDate}
onChange={(newValue) => setStartDate(newValue)}
value={selectedStartDate}
onChange={(newValue) => setSelectedStartDate(newValue)}
shouldDisableDate={(date) => disableFutureDates(date) || disableStartDateAfterEnd(date)}
/>
<DatePicker
label='End Date'
value={endDate}
onChange={(newValue) => setEndDate(newValue)}
value={selectedEndDate}
onChange={(newValue) => setSelectedEndDate(newValue)}
shouldDisableDate={(date) => disableFutureDates(date) || disableEndDateBeforeStart(date)}
sx={styles.datePicker}
/>
</LocalizationProvider>
</Grid>
<Grid item>
<Box sx={styles.cardContent}>
<Box sx={{ ...styles.cardContent, marginLeft: 0 }}>
{loading ? (
<Box sx={styles.loadingIndicator}>
<CircularProgress />
</Box>
) : (
<>
<Typography variant='h6'>
<Typography variant='h6' sx={{ textAlign: 'left' }}>
<FormattedMessage
id='TransactionList.total.transactions'
defaultMessage='Total Transaction Count : '
Expand All @@ -218,28 +313,47 @@ export default function UsageReport() {
</>
)}
</Box>
<PDFDownloadLink
document={(
<TransactionPDF
startDate={startDate}
endDate={endDate}
totalTransactions={transactionCount}
/>
)}
fileName='usage_report.pdf'
style={{ textDecoration: 'none' }}
<Box
sx={{
display: 'flex',
alignItems: 'center',
marginTop: 2,
gap: 2,
}}
>
<Button
variant='contained'
color='primary'
disabled={loading}
onClick={fetchTransactionData}
>
<FormattedMessage
id='TransactionList.download.pdf'
defaultMessage='Download PDF'
id='TransactionList.view.report'
defaultMessage='View'
/>
</Button>
</PDFDownloadLink>
<PDFDownloadLink
document={(
<TransactionPDF
startDate={selectedStartDate}
endDate={selectedEndDate}
totalTransactions={transactionCount}
/>
)}
fileName='usage_report.pdf'
style={{ textDecoration: 'none' }}
>
<Button
variant='contained'
color='primary'
disabled={loading}
>
<FormattedMessage
id='TransactionList.download.report'
defaultMessage='Download Report'
/>
</Button>
</PDFDownloadLink>
</Box>
</Grid>
</Grid>
</ContentBase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ import ExpandMore from '@mui/icons-material/ExpandMore';
* @returns {JSX} Header AppBar components.
*/
function NavigatorChildren(props) {
const { isSuperTenant } = useAppContext();
const { isSuperTenant, user: { _scopes } } = useAppContext();
const isSuperAdmin = isSuperTenant && _scopes.includes('apim:admin_settings');
const [open, setOpen] = React.useState(true);
const { navChildren, navText, classes } = props;
const handleClick = () => {
Expand All @@ -48,6 +49,8 @@ function NavigatorChildren(props) {

if (!isSuperTenant) {
navigationChildren = navChildren.filter((menu) => menu.id !== 'Custom Policies');
}
if (!isSuperAdmin) {
navigationChildren = navChildren.filter((menu) => menu.id !== 'Usage Report');
}

Expand Down
Loading