Skip to content
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
15 changes: 15 additions & 0 deletions resources/js/components/follow-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export function useIsFollowing(
return useSharedState(isFollowingKey(user), isFollowing);
}

const isPendingKey = (user: Pick<User, 'id'>): string =>
`follow_pending:${user.id}`;

const hasBlockKey = (user: Pick<User, 'id'>): string => `has_block:${user.id}`;

/**
Expand Down Expand Up @@ -102,6 +105,9 @@ export function FollowLabel({
);
}

/**
* Follows are async visits, so following several people in a row never cancels the follows still on their way. Each person's follow waits for the one before it to answer, so a double click can't follow and then unfollow.
*/
export function FollowButton({
user,
isFollowing: initialIsFollowing,
Expand All @@ -121,6 +127,10 @@ export function FollowButton({
user,
initialIsFollowing,
);
const [isPending, setIsPending] = useSharedState<boolean>(
isPendingKey(user),
false,
);
const { isJustToggled, markToggled, onMouseLeave } = useFollowHover();

if (auth.user === null) {
Expand Down Expand Up @@ -152,6 +162,11 @@ export function FollowButton({
}
as="button"
preserveScroll
async
onBefore={() => !isPending}
onStart={() => setIsPending(true)}
onFinish={() => setIsPending(false)}
aria-disabled={isPending}
onHttpException={(response) =>
onFollowHttpException(user, response)
}
Expand Down
49 changes: 49 additions & 0 deletions tests/Unit/FollowState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -332,3 +332,52 @@ test('a preview unfollow that fails after a block keeps who to follow unfollowed

assert.equal(suggestionLabel(user), 'Follow @raced');
});

function suggestionFollowLink(user: SuggestedUser): Props {
const button = find(
SuggestedUserRow({ user }),
(props) => props['data-test'] === 'suggested-follow-button',
);
assert.ok(button);

const link = find(render(button), (props) => 'aria-label' in props);
assert.ok(link);

return link.props;
}

test('following several suggestions in a row sends every follow without cancelling the others', () => {
const first = suggestion('pauline');
const second = suggestion('brent');

const firstFollow = suggestionFollowLink(first);
(firstFollow.onStart as () => void)();
const secondFollow = suggestionFollowLink(second);

assert.equal(firstFollow.async, true);
assert.equal(secondFollow.async, true);
assert.equal((secondFollow.onBefore as () => boolean)(), true);

(secondFollow.onStart as () => void)();
for (const follow of [secondFollow, firstFollow]) {
(follow.onSuccess as () => void)();
(follow.onFinish as () => void)();
}

assert.equal(suggestionLabel(first), 'Unfollow @pauline');
assert.equal(suggestionLabel(second), 'Unfollow @brent');
});

test('a second click on a follow still on its way sends nothing', () => {
const user = suggestion('matthieu');

(suggestionFollowLink(user).onStart as () => void)();

const pending = suggestionFollowLink(user);
assert.equal(pending['aria-disabled'], true);
assert.equal((pending.onBefore as () => boolean)(), false);

(pending.onFinish as () => void)();

assert.equal((suggestionFollowLink(user).onBefore as () => boolean)(), true);
});