Skip to content

Commit 2aad125

Browse files
MichaelDeBoeySaraVieira
authored andcommitted
🔨 Switch Keywords to use useOvermind (codesandbox#2567)
* 🔨 Switch Keywords to use useOvermind * Extract tagsChanged into an action
1 parent 47099a3 commit 2aad125

File tree

3 files changed

+89
-94
lines changed

3 files changed

+89
-94
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export const cliMounted: AsyncAction = withLoadApp(
3030
export const notificationAdded: Action<{
3131
title: string;
3232
notificationType: NotificationType;
33-
timeAlive: number;
34-
}> = ({ effects }, { title, notificationType, timeAlive }) => {
33+
timeAlive?: number;
34+
}> = ({ effects }, { title, notificationType, timeAlive = 1 }) => {
3535
effects.notificationToast.add({
3636
message: title,
3737
status: convertTypeToStatus(notificationType),

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

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ export const valueChanged: Action<{
1111
state.workspace.project[field] = value;
1212
};
1313

14-
export const tagChanged: Action<{
15-
tagName: string;
16-
}> = ({ state }, { tagName }) => {
14+
export const tagChanged: Action<string> = ({ state }, tagName) => {
1715
state.workspace.tags.tagName = tagName;
1816
};
1917

@@ -35,36 +33,54 @@ export const tagAdded: AsyncAction = withOwnedSandbox(
3533
}
3634
);
3735

38-
export const tagRemoved: AsyncAction<{
39-
tag: string;
40-
}> = withOwnedSandbox(async ({ state, effects, actions }, { tag }) => {
41-
const sandbox = state.editor.currentSandbox;
42-
const tagIndex = sandbox.tags.indexOf(tag);
36+
export const tagRemoved: AsyncAction<string> = withOwnedSandbox(
37+
async ({ state, effects, actions }, tag) => {
38+
const sandbox = state.editor.currentSandbox;
39+
const tagIndex = sandbox.tags.indexOf(tag);
4340

44-
sandbox.tags.splice(tagIndex, 1);
41+
sandbox.tags.splice(tagIndex, 1);
4542

46-
try {
47-
sandbox.tags = await effects.api.deleteTag(sandbox.id, tag);
43+
try {
44+
sandbox.tags = await effects.api.deleteTag(sandbox.id, tag);
4845

49-
// Create a "joint action" on this
50-
const { parsed } = state.editor.parsedConfigurations.package;
46+
// Create a "joint action" on this
47+
const { parsed } = state.editor.parsedConfigurations.package;
5148

52-
parsed.keywords = sandbox.tags;
53-
parsed.name = slugify(sandbox.title || sandbox.id);
54-
parsed.description = sandbox.description;
49+
parsed.keywords = sandbox.tags;
50+
parsed.name = slugify(sandbox.title || sandbox.id);
51+
parsed.description = sandbox.description;
5552

56-
const code = JSON.stringify(parsed, null, 2);
57-
const moduleShortid = state.editor.currentPackageJSON.shortid;
53+
const code = JSON.stringify(parsed, null, 2);
54+
const moduleShortid = state.editor.currentPackageJSON.shortid;
5855

59-
await actions.editor.internal.saveCode({
60-
code,
61-
moduleShortid,
62-
cbID: null,
63-
});
64-
} catch (error) {
65-
sandbox.tags.splice(tagIndex, 0, tag);
56+
await actions.editor.internal.saveCode({
57+
code,
58+
moduleShortid,
59+
cbID: null,
60+
});
61+
} catch (error) {
62+
sandbox.tags.splice(tagIndex, 0, tag);
63+
}
6664
}
67-
});
65+
);
66+
67+
export const tagsChanged: AsyncAction<{
68+
newTags: string[];
69+
removedTags: string[];
70+
}> = async ({ actions, effects, state }, { newTags, removedTags }) => {
71+
const { tags } = state.editor.currentSandbox;
72+
if (tags.length > 5) {
73+
return effects.notificationToast.error('You can have a maximum of 5 tags');
74+
}
75+
76+
const tagWasRemoved =
77+
newTags.length < tags.length && removedTags.length === 1;
78+
if (tagWasRemoved) {
79+
return removedTags.forEach(actions.workspace.tagRemoved);
80+
}
81+
82+
return actions.workspace.tagAdded();
83+
};
6884

6985
export const sandboxInfoUpdated: AsyncAction = withOwnedSandbox(
7086
async ({ state, effects, actions }) => {
Lines changed: 45 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,56 @@
1-
import React from 'react';
2-
import { inject, hooksObserver, clone } from 'app/componentConnectors';
1+
import React, { FunctionComponent } from 'react';
32
import Tags from '@codesandbox/common/lib/components/Tags';
43
import getTemplateDefinition from '@codesandbox/common/lib/templates';
4+
5+
import { clone } from 'app/componentConnectors';
56
import { EditableTags } from 'app/components/EditableTags';
7+
import { useOvermind } from 'app/overmind';
8+
69
import { Item } from './elements';
710

8-
interface IKeywordsProps {
11+
type Props = {
912
editable?: boolean;
10-
store: any;
11-
signals: any;
12-
}
13-
14-
export const Keywords = inject('store', 'signals')(
15-
hooksObserver(
16-
({
17-
editable,
18-
store: {
19-
editor: {
20-
currentSandbox: { template, tags },
21-
},
22-
workspace: {
23-
tags: { tagName },
24-
},
13+
};
14+
export const Keywords: FunctionComponent<Props> = ({ editable }) => {
15+
const {
16+
actions: {
17+
workspace: { tagChanged, tagsChanged },
18+
},
19+
state: {
20+
editor: {
21+
currentSandbox: { template, tags },
2522
},
26-
signals: {
27-
notificationAdded,
28-
workspace: { tagAdded, tagChanged, tagRemoved },
23+
workspace: {
24+
tags: { tagName },
2925
},
30-
}: IKeywordsProps) => {
31-
const changeTags = (newTags: string[], removedTags: string[]) => {
32-
if (tags.length > 5) {
33-
notificationAdded('You can have a maximum of 5 tags', 'error');
34-
return;
35-
}
36-
37-
const tagWasRemoved =
38-
newTags.length < tags.length && removedTags.length === 1;
26+
},
27+
} = useOvermind();
3928

40-
if (tagWasRemoved) {
41-
removedTags.forEach(tag => {
42-
tagRemoved({ tag });
43-
});
44-
} else {
45-
tagAdded();
46-
}
47-
};
29+
if (tags.length === 0 && !editable) {
30+
return null;
31+
}
4832

49-
if (tags.length === 0 && !editable) {
50-
return null;
51-
}
33+
const changeTags = (newTags: string[], removedTags: string[]) =>
34+
tagsChanged({ newTags, removedTags });
5235

53-
return (
54-
<Item>
55-
{editable ? (
56-
<EditableTags
57-
template={getTemplateDefinition(template)}
58-
value={clone(tags)}
59-
onChange={changeTags}
60-
onChangeInput={(value: string) => {
61-
tagChanged({ tagName: value });
62-
}}
63-
maxTags={5}
64-
inputValue={tagName}
65-
renderInput={({ addTag, ...props }: any) =>
66-
tags.length !== 5 ? <input type="text" {...props} /> : null
67-
}
68-
onlyUnique
69-
/>
70-
) : (
71-
<Tags style={{ fontSize: 13 }} tags={tags} />
72-
)}
73-
</Item>
74-
);
75-
}
76-
)
77-
);
36+
return (
37+
<Item>
38+
{editable ? (
39+
<EditableTags
40+
inputValue={tagName}
41+
maxTags={5}
42+
onChange={changeTags}
43+
onChangeInput={tagChanged}
44+
onlyUnique
45+
renderInput={({ addTag, ...props }: any) =>
46+
tags.length !== 5 ? <input type="text" {...props} /> : null
47+
}
48+
template={getTemplateDefinition(template)}
49+
value={clone(tags)}
50+
/>
51+
) : (
52+
<Tags style={{ fontSize: 13 }} tags={tags} />
53+
)}
54+
</Item>
55+
);
56+
};

0 commit comments

Comments
 (0)