Skip to content

Commit

Permalink
use APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
bfabio committed Jul 29, 2024
1 parent f14352f commit 779def1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
2 changes: 1 addition & 1 deletion _layouts/software-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ <h2>
<div class="col-7 col-sm-7 col-md-3">
<p>
<span class="label">Stato publiccode.yml</span>
<publiccode-badge repo="{{ page.publiccode.url | escape }}"></publiccode-badge>
<publiccode-badge software="{{ page.id }}"></publiccode-badge>
<p>
</div>

Expand Down
15 changes: 6 additions & 9 deletions assets/js/components/PubliccodeBadge.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,30 @@ const useStyle = createUseStyles({
},
});

export const PubliccodeBadge = React.memo(({ repo }) => {
export const PubliccodeBadge = React.memo(({ softwareId }) => {

Check failure on line 26 in assets/js/components/PubliccodeBadge.js

View workflow job for this annotation

GitHub Actions / test

'softwareId' is missing in props validation
const [publiccodeState, setPubliccodeState] = useState('loading');

const classes = useStyle(publiccodeState)

useEffect(async () => {

Check failure on line 29 in assets/js/components/PubliccodeBadge.js

View workflow job for this annotation

GitHub Actions / test

React Hook useEffect contains a call to 'setPubliccodeState'. Without a list of dependencies, this can lead to an infinite chain of updates. To fix this, pass [logUrl] as a second argument to the useEffect Hook
const res = await fetch(logUrl);

if (res.status >= 200 || res.status <= 299) {
const data = await res.json();
const good = data.find(entry => entry?.message?.includes('activity index in the last'));
const good = data[0]?.message.includes('GOOD publiccode.yml');

setPubliccodeState(good ? 'good' : 'error');
} else {
setPubliccodeState('error');
}
});

const repoPath = repo.replaceAll(/^(.+):\/\/|.git$/ig, '');
const logUrl = `https://crawler.developers.italia.it/${repoPath}/log.json`
const logUrl = `https://api.developers.italia.it/v1/software/${softwareId}/logs`

return (
<>
<div className="lead">
<span className={`badge badge-${stateClass[publiccodeState]}`}>
{publiccodeState === 'loading' && <Spinner />}
{publiccodeState !== 'loading' && publiccodeState }
{publiccodeState !== 'loading' && publiccodeState}
</span>
</div>

Expand All @@ -59,14 +56,14 @@ export const PubliccodeBadge = React.memo(({ repo }) => {
target="_blank"
aria-label="Log file of the latest publiccode.yml crawling"
>
(JSON log)
(log)
</a>
</>
);
});

PubliccodeBadge.propTypes = {
repo: PropTypes.string.isRequired,
software: PropTypes.string.isRequired,
};

PubliccodeBadge.displayName = 'PubliccodeBadge';

0 comments on commit 779def1

Please sign in to comment.