Commit 51d8910d authored by aleclofabbro's avatar aleclofabbro

Merge branch...

Merge branch 'fix/search_follow_checks_and_programmatic_workaround_for_follow_button_link' into develop
parents 2a5ba56a f096c953
...@@ -68,13 +68,13 @@ export const SearchPageHOC = connectInfiniteHits(_SearchPageHOC); ...@@ -68,13 +68,13 @@ export const SearchPageHOC = connectInfiniteHits(_SearchPageHOC);
const CollectionPreviewHit: React.FC<{ hit: CollectionHit }> = ({ hit }) => { const CollectionPreviewHit: React.FC<{ hit: CollectionHit }> = ({ hit }) => {
const isLocal = useIsLocal(hit); const isLocal = useIsLocal(hit);
const previewFragment = collectionHit2gql(hit, isLocal); const previewFragment = collectionHit2gql(hit, isLocal);
const toggleFollowFormik = useToggleFollowHitFormik(hit); const { toggleFollowFormik } = useFollowHitHelper(hit);
const props = const props =
previewFragment && previewFragment &&
collectionFragment2UIProps({ collectionFragment2UIProps({
collection: previewFragment, collection: previewFragment,
hideActions: false, hideActions: false,
toggleFollowFormik: toggleFollowFormik toggleFollowFormik
}); });
!props && console.warn(`Could not preview searchHit:`, hit); !props && console.warn(`Could not preview searchHit:`, hit);
// console.log(`Collection:`, props) // console.log(`Collection:`, props)
...@@ -84,14 +84,17 @@ const CollectionPreviewHit: React.FC<{ hit: CollectionHit }> = ({ hit }) => { ...@@ -84,14 +84,17 @@ const CollectionPreviewHit: React.FC<{ hit: CollectionHit }> = ({ hit }) => {
const CommunityPreviewHit: React.FC<{ hit: CommunityHit }> = ({ hit }) => { const CommunityPreviewHit: React.FC<{ hit: CommunityHit }> = ({ hit }) => {
const isLocal = useIsLocal(hit); const isLocal = useIsLocal(hit);
const previewFragment = communityHit2gql(hit, isLocal); const previewFragment = communityHit2gql(hit, isLocal);
const toggleFollowFormik = useToggleFollowHitFormik(hit); const {
toggleFollowFormik: toggleJoinFormik,
hitSearchFollow
} = useFollowHitHelper(hit);
const props = const props =
previewFragment && previewFragment &&
communityFragment2UIProps({ communityFragment2UIProps({
community: previewFragment, community: previewFragment,
hideActions: false, hideActions: false,
toggleJoinFormik: toggleFollowFormik, toggleJoinFormik,
isCreator: false //FIXME isCreator: !!hitSearchFollow?.isCreator
}); });
!props && console.warn(`Could not preview searchHit:`, hit); !props && console.warn(`Could not preview searchHit:`, hit);
// console.log(`Community:`, props) // console.log(`Community:`, props)
...@@ -136,13 +139,13 @@ const useIsLocal = (hit: Hit) => { ...@@ -136,13 +139,13 @@ const useIsLocal = (hit: Hit) => {
// console.log(`---`) // console.log(`---`)
return isLocal; return isLocal;
}; };
const useToggleFollowHitFormik = (hit: Hit) => { const useFollowHitHelper = (hit: Hit) => {
const { data } = useSearchHostIndexAndMyFollowsQuery({ const { data } = useSearchHostIndexAndMyFollowsQuery({
context: mnCtx({ noShowError: true }) context: mnCtx({ noShowError: true })
}); });
const [follow, followResult] = useSearchFollowMutation(); const [follow, followResult] = useSearchFollowMutation();
const [unfollow, unfollowResult] = useSearchUnfollowMutation(); const [unfollow, unfollowResult] = useSearchUnfollowMutation();
const myFollow = React.useMemo( const hitSearchFollow = React.useMemo(
() => () =>
(data?.me?.searchFollows || []).find( (data?.me?.searchFollows || []).find(
searchFollow => searchFollow.canonicalUrl === hit.canonicalUrl searchFollow => searchFollow.canonicalUrl === hit.canonicalUrl
...@@ -150,20 +153,34 @@ const useToggleFollowHitFormik = (hit: Hit) => { ...@@ -150,20 +153,34 @@ const useToggleFollowHitFormik = (hit: Hit) => {
[data, hit] [data, hit]
); );
const canFollow = !hitSearchFollow?.followId;
const canUnfollow =
hitSearchFollow?.followId &&
!(hitSearchFollow.communityId && hitSearchFollow.isCreator);
const toggleFollowFormik = useFormik({ const toggleFollowFormik = useFormik({
initialValues: {}, initialValues: {},
onSubmit: async () => { onSubmit: async () => {
if (!hit.canonicalUrl || followResult.loading || unfollowResult.loading) { if (!hit.canonicalUrl || followResult.loading || unfollowResult.loading) {
return; return;
} }
return myFollow if (hitSearchFollow && canUnfollow) {
? unfollow({ return unfollow({
variables: { variables: {
contextId: myFollow.canonicalUrl /*FIXME with myFollow.followId */ contextId: hitSearchFollow.followId
} }
}) });
: follow({ variables: { url: hit.canonicalUrl } }); } else if (canFollow) {
return follow({ variables: { url: hit.canonicalUrl } });
}
return;
} }
}); });
return toggleFollowFormik; return React.useMemo(
() => ({
toggleFollowFormik,
hitSearchFollow
}),
[toggleFollowFormik, hitSearchFollow]
);
}; };
...@@ -27,7 +27,7 @@ export type SearchMeFragment = ( ...@@ -27,7 +27,7 @@ export type SearchMeFragment = (
{ __typename: 'Me' } { __typename: 'Me' }
& { searchFollows: Array<( & { searchFollows: Array<(
{ __typename: 'SearchFollow' } { __typename: 'SearchFollow' }
& Pick<Types.SearchFollow, 'canonicalUrl' | 'collectionId' | 'communityId'> & Pick<Types.SearchFollow, 'canonicalUrl' | 'collectionId' | 'communityId' | 'followId' | 'isCreator'>
)> } )> }
); );
...@@ -65,6 +65,8 @@ export const SearchMeFragmentDoc = gql` ...@@ -65,6 +65,8 @@ export const SearchMeFragmentDoc = gql`
canonicalUrl canonicalUrl
collectionId collectionId
communityId communityId
followId
isCreator
} }
} }
`; `;
......
This source diff could not be displayed because it is too large. You can view the blob instead.
This diff is collapsed.
...@@ -64,7 +64,10 @@ export const Collection: React.FC<Props> = ({ ...@@ -64,7 +64,10 @@ export const Collection: React.FC<Props> = ({
<ActionItem <ActionItem
bordered bordered
isFollowing={isFollowing ? true : false} isFollowing={isFollowing ? true : false}
onClick={toggleFollowFormik.submitForm} onClick={e => {
e.preventDefault();
toggleFollowFormik.submitForm();
}}
> >
<ActionText <ActionText
ml={1} ml={1}
......
...@@ -100,7 +100,10 @@ export const Community: React.FC<Props> = ({ ...@@ -100,7 +100,10 @@ export const Community: React.FC<Props> = ({
variant="outline" variant="outline"
isDisabled={isCreator ? true : false} isDisabled={isCreator ? true : false}
isSubmitting={toggleJoinFormik.isSubmitting} isSubmitting={toggleJoinFormik.isSubmitting}
onClick={toggleJoinFormik.submitForm} onClick={e => {
e.preventDefault();
toggleJoinFormik.submitForm();
}}
> >
<Text variant={'suptitle'}> <Text variant={'suptitle'}>
{joined ? <Trans>Leave</Trans> : <Trans>Join</Trans>} {joined ? <Trans>Leave</Trans> : <Trans>Join</Trans>}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment