Skip to content

Commit

Permalink
Merge pull request #374 from EnMarche/minor-fixes
Browse files Browse the repository at this point in the history
Minor fixes
  • Loading branch information
BastienTeissier authored May 6, 2021
2 parents 42d4970 + 5b78e50 commit c7b63d3
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
28 changes: 14 additions & 14 deletions src/components/AuthorAndSupports/AuthorAndSupports.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,26 @@ const AuthorAndSupports: FunctionComponent<AuthorAndSupportsProps> = ({ cause, s
const isUserLoggedIn = Boolean(useSelector(isUserLogged));
const intl = useIntl();

if (cause.author === undefined || cause.author === null) {
return null;
}

return (
<Container>
{showAuthor === true ? (
<AuthorContainer>
<IconAndLabel
iconSrc="/images/user.svg"
label={intl.formatMessage(
{ id: 'cause.author' },
{
firstName: cause.author.first_name,
lastName:
isUserLoggedIn && cause.author.last_name !== undefined
? cause.author.last_name
: cause.author.last_name_initial,
},
)}
label={
cause.author === undefined || cause.author === null
? intl.formatMessage({ id: 'cause.anonymous-author' })
: intl.formatMessage(
{ id: 'cause.author' },
{
firstName: cause.author.first_name,
lastName:
isUserLoggedIn && cause.author.last_name !== undefined
? cause.author.last_name
: cause.author.last_name_initial,
},
)
}
/>
</AuthorContainer>
) : null}
Expand Down
10 changes: 6 additions & 4 deletions src/components/Cause/Cause.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ const Cause: FunctionComponent<CauseProps> = ({ cause }: CauseProps) => {
<StyledContent>
<CoalitionsDisplay cause={cause} small />
<CauseName>{cause.name}</CauseName>
{cause.author !== undefined && cause.author !== null ? (
<Author>
<Author>
{cause.author !== undefined && cause.author !== null ? (
<FormattedMessage
id="cause.author"
values={{
Expand All @@ -71,8 +71,10 @@ const Cause: FunctionComponent<CauseProps> = ({ cause }: CauseProps) => {
: cause.author.last_name_initial,
}}
/>
</Author>
) : null}
) : (
<FormattedMessage id="cause.anonymous-author" />
)}
</Author>
<AuthorAndSupports cause={cause} />
<ButtonContainer>
{Boolean(cause.supported) || (
Expand Down
5 changes: 1 addition & 4 deletions src/components/CoalitionCards/CoalitionCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ const CoalitionCards: FunctionComponent<CoalitionCardsProps> = ({
const { fetchCoalitions, isFetchingCoalitions } = useFetchCoalitions();

useEffect(() => {
if (coalitions.length === 0) {
fetchCoalitions();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
fetchCoalitions();
}, [fetchCoalitions]);

const renderCoalitionCard = (coalition: Coalition) => {
Expand Down
7 changes: 5 additions & 2 deletions src/redux/Coalition/hooks/useFetchCoalitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { updateCoalitions, markCoalitionsAsFollowed } from '../slice';
import { Coalition } from '../types';
import HandleErrorService from 'services/HandleErrorService';
import { useFetchFollowedCoalitions } from './useFetchFollowedCoalitions';
import useSelector from 'redux/useSelector';
import { isUserLogged } from 'redux/Login/selectors';

export const useFetchCoalitions = () => {
const dispatch = useDispatch();
const isUserLoggedIn = Boolean(useSelector(isUserLogged));

const { loading: loadingFollowed, doFetchFollowedCoalitions } = useFetchFollowedCoalitions();

Expand All @@ -30,11 +33,11 @@ export const useFetchCoalitions = () => {
return;
}

const followedCoalitions = await doFetchFollowedCoalitions();
const followedCoalitions = await doFetchFollowedCoalitions(isUserLoggedIn);

dispatch(updateCoalitions(coalitions));
dispatch(markCoalitionsAsFollowed(followedCoalitions));
}, [dispatch, doFetchCoalitions, doFetchFollowedCoalitions]);
}, [dispatch, doFetchCoalitions, doFetchFollowedCoalitions, isUserLoggedIn]);

return { fetchCoalitions, isFetchingCoalitions: isFetchingCoalitions || loadingFollowed };
};
23 changes: 11 additions & 12 deletions src/redux/Coalition/hooks/useFetchFollowedCoalitions.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { useTypedAsyncFn } from 'redux/useTypedAsyncFn';
import { authenticatedApiClient } from 'services/networking/client';
import { isUserLogged } from 'redux/Login';
import useSelector from 'redux/useSelector';

export const useFetchFollowedCoalitions = () => {
const isUserLoggedIn = Boolean(useSelector(isUserLogged));

const [{ loading, error }, doFetchFollowedCoalitions] = useTypedAsyncFn(async () => {
if (!isUserLoggedIn) return [];
try {
return await authenticatedApiClient.get('v3/coalitions/followed');
} catch (error) {
return [];
}
}, []);
const [{ loading, error }, doFetchFollowedCoalitions] = useTypedAsyncFn(
async (isUserLoggedIn: boolean) => {
if (!isUserLoggedIn) return [];
try {
return await authenticatedApiClient.get('v3/coalitions/followed');
} catch (error) {
return [];
}
},
[],
);
return { loading, error, doFetchFollowedCoalitions };
};
1 change: 1 addition & 0 deletions src/translations/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
},
"cause": {
"author": "Par {firstName} {lastName}",
"anonymous-author": "Auteur Anonyme",
"support": "{supportsNumber} soutien",
"supports": "{supportsNumber} soutiens",
"support-button": "Soutenir",
Expand Down

0 comments on commit c7b63d3

Please sign in to comment.