Skip to content

Commit ec092de

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
🔨 Switch LikeHeart to use useOvermind (codesandbox#3144)
* 🔨 Switch LikeHeart to use useOvermind * Fix types
1 parent 9ec7e52 commit ec092de

File tree

4 files changed

+58
-42
lines changed

4 files changed

+58
-42
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
@@ -301,9 +301,10 @@ export const forkSandboxClicked: AsyncAction = async ({
301301
});
302302
};
303303

304-
export const likeSandboxToggled: AsyncAction<{
305-
id: string;
306-
}> = async ({ state, effects }, { id }) => {
304+
export const likeSandboxToggled: AsyncAction<string> = async (
305+
{ state, effects },
306+
id
307+
) => {
307308
if (state.editor.sandboxes[id].userLiked) {
308309
state.editor.sandboxes[id].likeCount--;
309310
await effects.api.unlikeSandbox(id);

‎packages/app/src/app/pages/Sandbox/Editor/Header/Buttons/LikeButton/LikeButton.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export const LikeButton: FunctionComponent = () => {
1414
return (
1515
<LikeHeart
1616
colorless
17-
text={String(currentSandbox.likeCount)}
18-
sandbox={currentSandbox}
1917
disableTooltip
2018
highlightHover
19+
sandbox={currentSandbox}
20+
text={currentSandbox.likeCount}
2121
/>
2222
);
2323
};
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,
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: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
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+
} & Partial<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,
39-
highlightHover,
29+
highlightHover = false,
30+
sandbox: { id, userLiked },
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={userLiked}
47+
loggedIn={isLoggedIn}
48+
onClick={isLoggedIn ? () => likeSandboxToggled(id) : noop}
49+
style={style}
5650
>
5751
<MaybeTooltip
58-
loggedIn={isLoggedIn}
5952
disableTooltip={disableTooltip}
60-
title={sandbox.userLiked ? 'Undo like' : 'Like'}
53+
loggedIn={isLoggedIn}
54+
title={userLiked ? 'Undo like' : 'Like'}
6155
>
62-
{sandbox.userLiked ? (
63-
<FullHeartIcon style={colorless ? null : { color: '#E01F4E' }} />
56+
{userLiked ? (
57+
<FullHeartIcon style={colorless ? {} : { color: '#E01F4E' }} />
6458
) : (
6559
<HeartIcon />
6660
)}

0 commit comments

Comments
 (0)