Skip to content

Commit 1228b2b

Browse files
committed
🔨 Switch LikeHeart to use useOvermind
1 parent 729d375 commit 1228b2b

File tree

4 files changed

+53
-37
lines changed

4 files changed

+53
-37
lines changed

‎packages/app/src/app/overmind/namespaces/editor/actions.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,10 @@ export const forkSandboxClicked: AsyncAction = async ({
253253
});
254254
};
255255

256-
export const likeSandboxToggled: AsyncAction<{
257-
id: string;
258-
}> = async ({ state, effects }, { id }) => {
256+
export const likeSandboxToggled: AsyncAction<string> = async (
257+
{ state, effects },
258+
id
259+
) => {
259260
if (state.editor.sandboxes[id].userLiked) {
260261
state.editor.sandboxes[id].likeCount--;
261262
await effects.api.unlikeSandbox(id);

‎packages/app/src/app/pages/Sandbox/Editor/Header/index.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const LikeButton = inject('store')(
5050
style,
5151
likeCount,
5252
store: { editor },
53-
}: ButtonProps & { likeCount: string; store: any }) => (
53+
}: ButtonProps & { likeCount: number; store: any }) => (
5454
<LikeHeart
5555
colorless
5656
style={style}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
2+
import React, { FunctionComponent } from 'react';
3+
4+
type Props = {
5+
disableTooltip?: boolean;
6+
loggedIn: boolean;
7+
title: string;
8+
};
9+
export const MaybeTooltip: FunctionComponent<Props> = ({
10+
children,
11+
disableTooltip = false,
12+
loggedIn,
13+
title,
14+
}) =>
15+
loggedIn && !disableTooltip ? (
16+
<Tooltip content={title} style={{ display: 'flex' }}>
17+
{children}
18+
</Tooltip>
19+
) : (
20+
<>{children}</>
21+
);

‎packages/app/src/app/pages/common/LikeHeart/index.tsx‎

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,56 @@
1-
import Tooltip from '@codesandbox/common/lib/components/Tooltip';
21
import { Sandbox } from '@codesandbox/common/lib/types';
3-
import noop from 'lodash/noop';
2+
import React, { ComponentProps, FunctionComponent } from 'react';
3+
44
import { useOvermind } from 'app/overmind';
5-
import React from 'react';
5+
66
// @ts-ignore
77
import HeartIcon from '-!svg-react-loader!@codesandbox/common/lib/icons/heart-open.svg'; // eslint-disable-line import/no-webpack-loader-syntax
88
// @ts-ignore
99
import FullHeartIcon from '-!svg-react-loader!@codesandbox/common/lib/icons/heart.svg'; // eslint-disable-line import/no-webpack-loader-syntax
1010

1111
import { Container } from './elements';
12+
import { MaybeTooltip } from './MaybeTooltip';
1213

13-
const MaybeTooltip = ({ loggedIn, disableTooltip, title, children }) =>
14-
loggedIn && !disableTooltip ? (
15-
<Tooltip content={title} style={{ display: 'flex' }}>
16-
{children}
17-
</Tooltip>
18-
) : (
19-
children
20-
);
14+
const noop = () => undefined;
2115

22-
interface ILikeHeartProps {
23-
sandbox: Sandbox;
24-
className?: string;
16+
type Props = {
2517
colorless?: boolean;
26-
text?: string;
27-
style?: React.CSSProperties;
28-
disableTooltip?: boolean;
29-
highlightHover?: boolean;
30-
}
31-
32-
export const LikeHeart: React.FC<ILikeHeartProps> = ({
33-
sandbox,
18+
sandbox: Sandbox;
19+
text?: number;
20+
} & Pick<ComponentProps<typeof MaybeTooltip>, 'disableTooltip'> &
21+
Pick<
22+
ComponentProps<typeof Container>,
23+
'className' | 'highlightHover' | 'style'
24+
>;
25+
export const LikeHeart: FunctionComponent<Props> = ({
3426
className,
3527
colorless,
36-
text,
37-
style,
3828
disableTooltip,
3929
highlightHover,
30+
sandbox,
31+
style,
32+
text,
4033
}) => {
4134
const {
35+
actions: {
36+
editor: { likeSandboxToggled },
37+
},
4238
state: { isLoggedIn },
43-
actions: { editor },
4439
} = useOvermind();
40+
4541
return (
4642
<Container
47-
style={style}
48-
hasText={text !== undefined}
49-
loggedIn={isLoggedIn}
50-
liked={sandbox.userLiked}
5143
className={className}
44+
hasText={text !== undefined}
5245
highlightHover={highlightHover}
53-
onClick={
54-
isLoggedIn ? () => editor.likeSandboxToggled({ id: sandbox.id }) : noop
55-
}
46+
liked={sandbox.userLiked}
47+
loggedIn={isLoggedIn}
48+
onClick={isLoggedIn ? () => likeSandboxToggled(sandbox.id) : noop}
49+
style={style}
5650
>
5751
<MaybeTooltip
58-
loggedIn={isLoggedIn}
5952
disableTooltip={disableTooltip}
53+
loggedIn={isLoggedIn}
6054
title={sandbox.userLiked ? 'Undo like' : 'Like'}
6155
>
6256
{sandbox.userLiked ? (

0 commit comments

Comments
 (0)