Skip to content

Commit

Permalink
Merge pull request #407 from dadosjusbr/agency-extra-info
Browse files Browse the repository at this point in the history
Accordion nos títulos das páginas de informações
  • Loading branch information
danielfireman authored Jul 19, 2023
2 parents 031a4f2 + 860654e commit d8511f1
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 17 deletions.
22 changes: 19 additions & 3 deletions src/components/AgencyWithNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import light from '../styles/theme-light';
import { formatAgency } from '../functions/format';
import Drawer from './Drawer';
import IndexTabGraph from './TransparencyChart/IndexTabChart';
import MoreInfoAccordion from './MoreInfoAccordion';

export interface AgencyPageWithNavigationProps {
id: string;
year: number;
mi: SummaryzedMI[];
agency: Agency;
title: string;
setYear: (y: number) => void;
Expand All @@ -50,6 +52,7 @@ export interface AgencyPageWithNavigationProps {

const AgencyPageWithNavigation: React.FC<AgencyPageWithNavigationProps> = ({
id,
mi,
title,
year,
agency,
Expand Down Expand Up @@ -84,9 +87,22 @@ const AgencyPageWithNavigation: React.FC<AgencyPageWithNavigationProps> = ({
return (
<Container fixed>
<Box>
<Typography variant="h2" textAlign="center">
{title} ({formatAgency(id.toLocaleUpperCase('pt'))})
</Typography>
<MoreInfoAccordion
ombudsman={agency?.ouvidoria}
twitterHandle={agency?.twitter_handle}
timestamp={data?.map(d => d.timestamp.seconds).at(-1)}
repository={mi[0]?.dados_coleta.repositorio_coletor}
>
<Typography
variant="h2"
title="Mais informações sobre o órgão"
textAlign="center"
width="100%"
pb={0}
>
{title} ({formatAgency(id.toLocaleUpperCase('pt'))})
</Typography>
</MoreInfoAccordion>
{agency && agency.coletando && !data ? (
<></>
) : (
Expand Down
27 changes: 22 additions & 5 deletions src/components/AgencyWithoutNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import ShareModal from './ShareModal';
import light from '../styles/theme-light';
import { formatAgency } from '../functions/format';
import Drawer from './Drawer';
import MoreInfoAccordion from './MoreInfoAccordion';

const AnnualRemunerationGraph = dynamic(
() => import('./AnnualRemunerationGraph'),
Expand All @@ -43,6 +44,7 @@ const IndexTabGraph = dynamic(

export interface AgencyPageWithoutNavigationProps {
id: string;
agencyTotals: v2AgencyTotalsYear;
year: number;
agency: Agency;
title: string;
Expand All @@ -53,17 +55,32 @@ export interface AgencyPageWithoutNavigationProps {

const AgencyPageWithoutNavigation: React.FC<
AgencyPageWithoutNavigationProps
> = ({ id, title, year, agency, data, dataLoading, plotData }) => {
> = ({
id,
agencyTotals,
title,
year,
agency,
data,
dataLoading,
plotData,
}) => {
const [modalIsOpen, setModalIsOpen] = useState(false);
const matches = useMediaQuery('(max-width:900px)');
const router = useRouter();

return (
<Container fixed sx={{ mb: 12 }}>
<Box>
<Typography variant="h2" textAlign="center">
{title} ({formatAgency(id.toLocaleUpperCase('pt'))})
</Typography>
<MoreInfoAccordion
ombudsman={agency?.ouvidoria}
twitterHandle={agency?.twitter_handle}
timestamp={agencyTotals?.meses?.at(-1).timestamp.seconds}
repository=""
>
<Typography variant="h2" textAlign="center" width="100%">
{title} ({formatAgency(id.toLocaleUpperCase('pt'))})
</Typography>
</MoreInfoAccordion>
{agency && agency.coletando && !agency.possui_dados ? (
<></>
) : (
Expand Down
102 changes: 102 additions & 0 deletions src/components/MoreInfoAccordion/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
Accordion,
AccordionDetails,
AccordionSummary,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
Typography,
} from '@mui/material';
import { ExpandMore, Info } from '@mui/icons-material';
import PublicIcon from '@mui/icons-material/Public';
import TwitterIcon from '@mui/icons-material/Twitter';
import AccessTimeIcon from '@mui/icons-material/AccessTime';
import { useState } from 'react';

type MoreInfoAccordionProps = {
children: React.ReactNode;
ombudsman: string;
twitterHandle: string;
timestamp: number;
repository: string;
};

const MoreInfoAccordion = ({
children,
ombudsman,
repository,
timestamp,
twitterHandle,
}: MoreInfoAccordionProps) => {
const [open, setOpen] = useState(false);

const handleClick = () => {
setOpen(!open);
};
return (
<Accordion elevation={0}>
<AccordionSummary
onClick={handleClick}
expandIcon={open ? <ExpandMore /> : <Info />}
aria-controls="panel1a-content"
id="panel1a-header"
>
{children}
</AccordionSummary>
<AccordionDetails sx={{ pt: 0 }}>
<Typography variant="h6" textAlign="center">
- Mais informações sobre o órgão -
</Typography>
<List dense sx={{ display: 'flex', justifyContent: 'center' }}>
<ListItem disablePadding sx={{ width: 'fit-content' }}>
<ListItemButton
href={ombudsman && new URL(ombudsman).origin}
target="_blank"
>
<ListItemIcon>
<PublicIcon />
</ListItemIcon>
<ListItemText
primary="Portal de transparência"
secondary={ombudsman && new URL(ombudsman).hostname}
/>
</ListItemButton>
</ListItem>
<ListItem disablePadding sx={{ width: 'fit-content' }}>
<ListItemButton
href={twitterHandle ? `https://twitter.com/${twitterHandle}` : ''}
target="_blank"
>
<ListItemIcon>
<TwitterIcon />
</ListItemIcon>
<ListItemText
primary="Twitter"
secondary={twitterHandle ? `@${twitterHandle}` : 'Não possui'}
/>
</ListItemButton>
</ListItem>
<ListItem disablePadding sx={{ width: 'fit-content' }}>
<ListItemButton href={repository} target="_blank">
<ListItemIcon>
<AccessTimeIcon />
</ListItemIcon>
<ListItemText
primary="Data da coleta"
secondary={
timestamp
? new Date(1000 * timestamp).toLocaleDateString('pt-BR')
: 'Não Coletado'
}
/>
</ListItemButton>
</ListItem>
</List>
</AccordionDetails>
</Accordion>
);
};

export default MoreInfoAccordion;
32 changes: 28 additions & 4 deletions src/pages/grupo/[summary].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,17 @@ const GraphWithNavigation: React.FC<{ id: string; title: string }> = ({
const [data, setData] = useState<any[]>([]);
const [year, setYear] = useState(getCurrentYear());
const [agencyData, setAgencyData] = useState<any>();
const [agencyTotals, setAgencyTotals] = useState<v2AgencyTotalsYear>();
const [dataLoading, setDataLoading] = useState(true);
const [plotData, setPlotData] = useState<AggregateIndexes[]>([]);

useEffect(() => {
setDataLoading(true);
Promise.all([fetchAgencyData(), fetchPlotData()]).finally(() =>
setDataLoading(false),
);
Promise.all([
fetchAgencyData(),
fetchPlotData(),
fetchAgencyTotalData(),
]).finally(() => setDataLoading(false));
}, [year]);
async function fetchAgencyData() {
try {
Expand All @@ -172,7 +175,6 @@ const GraphWithNavigation: React.FC<{ id: string; title: string }> = ({
console.log(err);
}
}

async function fetchPlotData() {
try {
const { data: transparencyPlot } = await api.default.get(
Expand All @@ -183,10 +185,32 @@ const GraphWithNavigation: React.FC<{ id: string; title: string }> = ({
console.log(err);
}
}
const fetchAgencyTotalData = async () => {
const currentYear = getCurrentYear();
await fetchAgencyTotalDataRecursive(currentYear);
};

const fetchAgencyTotalDataRecursive = async (yearParam: number) => {
try {
const { data: agencyTotalsResponse } = await api.ui.get(
`/v2/orgao/totais/${id}/${year}`,
);

if (agencyTotalsResponse.meses) {
setAgencyTotals(agencyTotalsResponse);
} else {
const previousYear = yearParam - 1;
await fetchAgencyTotalDataRecursive(previousYear);
}
} catch (err) {
console.log(err);
}
};
return (
<div id={id}>
<AgencyWithoutNavigation
data={data}
agencyTotals={agencyTotals}
dataLoading={dataLoading}
id={id}
title={title}
Expand Down
36 changes: 31 additions & 5 deletions src/pages/orgao/[agency]/[year]/[month].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import api from '../../../../services/api';
import OMASummary from '../../../../components/OmaChart';
import ErrorTable from '../../../../components/ErrorTable';
import MonthPopover from '../../../../components/MonthPopover';
import MoreInfoAccordion from '../../../../components/MoreInfoAccordion';

function UnixToHumanDate(unix) {
const d = new Date(unix * 1000);
Expand All @@ -48,6 +49,7 @@ export default function OmaPage({
}) {
const [chartData, setChartData] = useState<AgencyRemuneration>();
const [loading, setLoading] = useState(true);
const [agencyInfo, setAgencyInfo] = useState<Agency>();
function getNextDate() {
let m = +month;
let y = +year;
Expand Down Expand Up @@ -78,7 +80,7 @@ export default function OmaPage({
useEffect(() => {
// then it checks the next and the previous year to block the navigation buttons or to help to choose the right year
// finally it fetchs the data from the api to fill the chart with the agency/month/year data
fetchChartData();
Promise.all([fetchChartData(), fetchAgencyInfo()]);
}, [year, month]);
async function fetchChartData() {
try {
Expand All @@ -94,6 +96,17 @@ export default function OmaPage({
setLoading(false);
}
}
async function fetchAgencyInfo() {
try {
const { data: agencyObj }: { data: Agency } = await api.default.get(
`orgao/${agency}`,
);
setAgencyInfo(agencyObj);
} catch (error) {
console.log(error);
router.push('/404');
}
}
function handleNavigateToNextSummaryOption() {
const { m, y } = getNextDate();
setLoading(true);
Expand Down Expand Up @@ -126,10 +139,23 @@ export default function OmaPage({
<Header />
<Container fixed>
<Box py={4}>
<Typography variant="h2" textAlign="center">
{oma ? oma?.orgao : 'Coleta não realizada!'} (
{agency.toLocaleUpperCase('pt')})
</Typography>
<MoreInfoAccordion
ombudsman={agencyInfo?.ouvidoria}
timestamp={oma?.timestamp.seconds}
twitterHandle={agencyInfo?.twitter_handle}
repository={mi?.dados_coleta.repositorio_coletor}
>
<Typography
variant="h2"
title="Mais informações sobre o órgão"
textAlign="center"
width="100%"
pb={0}
>
{oma ? oma?.orgao : 'Coleta não realizada!'} (
{agency.toLocaleUpperCase('pt')})
</Typography>
</MoreInfoAccordion>
<Grid container justifyContent="center" alignItems="center">
<Grid item>
<IconButton
Expand Down
16 changes: 16 additions & 0 deletions src/pages/orgao/[agency]/[year]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useEffect, useState } from 'react';
import styled from 'styled-components';
import Head from 'next/head';
import { GetServerSideProps } from 'next';
Expand Down Expand Up @@ -27,11 +28,25 @@ export default function AgencyPage({
summaryPackage: Backup;
plotData: AggregateIndexes[];
}) {
const [MI, setMI] = useState<SummaryzedMI[]>([]);
const router = useRouter();
function navigateToGivenYear(y: number) {
router.push(`/orgao/${id}/${y}`);
}
const pageTitle = `${id.toUpperCase()} - ${year}`;

useEffect(() => {
fetchMIData();
}, []);
const fetchMIData = async () => {
try {
const { data: mi } = await api.default.get(`/dados/${id}/${year}`);
setMI(mi);
} catch (error) {
console.log(error);
router.push(`/404`);
}
};
return (
<Page>
<Head>
Expand All @@ -51,6 +66,7 @@ export default function AgencyPage({
<AgencyWithNavigation
data={data}
id={id}
mi={MI}
year={year}
agency={agency}
dataLoading={false}
Expand Down
Loading

0 comments on commit d8511f1

Please sign in to comment.