|
1 | | -import React from 'react'; |
2 | | -import { inject, hooksObserver } from 'app/componentConnectors'; |
| 1 | +import getTemplateDefinition from '@codesandbox/common/lib/templates'; |
| 2 | +import React, { FunctionComponent, MouseEvent } from 'react'; |
3 | 3 | import TrashIcon from 'react-icons/lib/fa/trash'; |
4 | | -import * as templates from '@codesandbox/common/lib/templates'; |
5 | | -import { TemplateConfig } from './TemplateConfig'; |
| 4 | + |
| 5 | +import { useOvermind } from 'app/overmind'; |
| 6 | + |
6 | 7 | import { Group } from '../elements'; |
| 8 | + |
7 | 9 | import { Container, CenteredText, Action } from './elements'; |
| 10 | +import { TemplateConfig } from './TemplateConfig'; |
8 | 11 |
|
9 | | -export const SandboxConfig = inject('store', 'signals')( |
10 | | - hooksObserver( |
11 | | - ({ |
12 | | - store: { |
13 | | - user, |
14 | | - editor: { |
15 | | - currentSandbox: { template, customTemplate }, |
16 | | - }, |
17 | | - workspace: { |
18 | | - project: { title, description }, |
19 | | - }, |
| 12 | +export const SandboxConfig: FunctionComponent = () => { |
| 13 | + const { |
| 14 | + actions: { |
| 15 | + modalOpened, |
| 16 | + workspace: { addedTemplate, deleteTemplate }, |
| 17 | + }, |
| 18 | + state: { |
| 19 | + user, |
| 20 | + editor: { |
| 21 | + currentSandbox: { customTemplate, template }, |
20 | 22 | }, |
21 | | - signals: { |
22 | | - modalOpened, |
23 | | - workspace: { addedTemplate, deleteTemplate }, |
| 23 | + workspace: { |
| 24 | + project: { description, title }, |
24 | 25 | }, |
25 | | - }) => { |
26 | | - const onCreateTemplate = (e: React.MouseEvent<HTMLButtonElement>) => { |
27 | | - e.preventDefault(); |
| 26 | + }, |
| 27 | + } = useOvermind(); |
28 | 28 |
|
29 | | - if (!user) { |
30 | | - modalOpened({ modal: 'signInForTemplates' }); |
31 | | - } |
| 29 | + const onCreateTemplate = ({ |
| 30 | + preventDefault, |
| 31 | + }: MouseEvent<HTMLButtonElement>) => { |
| 32 | + preventDefault(); |
32 | 33 |
|
33 | | - addedTemplate({ |
34 | | - template: { |
35 | | - color: |
36 | | - (customTemplate && customTemplate.color) || |
37 | | - templates.default(template).color(), |
38 | | - title, |
39 | | - description, |
40 | | - }, |
41 | | - }); |
42 | | - }; |
| 34 | + if (!user) { |
| 35 | + modalOpened({ modal: 'signInForTemplates' }); |
| 36 | + } |
43 | 37 |
|
44 | | - const onDelete = (e: React.MouseEvent<HTMLButtonElement>) => { |
45 | | - e.preventDefault(); |
46 | | - if (customTemplate) { |
47 | | - deleteTemplate(); |
48 | | - } else { |
49 | | - modalOpened({ modal: 'deleteSandbox' }); |
50 | | - } |
51 | | - }; |
| 38 | + addedTemplate({ |
| 39 | + color: |
| 40 | + (customTemplate && customTemplate.color) || |
| 41 | + getTemplateDefinition(template).color(), |
| 42 | + description, |
| 43 | + title, |
| 44 | + }); |
| 45 | + }; |
52 | 46 |
|
53 | | - return ( |
54 | | - <> |
55 | | - {customTemplate && <TemplateConfig />} |
56 | | - <Group> |
57 | | - <Container> |
58 | | - {!customTemplate && ( |
59 | | - <Action onClick={onCreateTemplate}> |
60 | | - <CenteredText> |
61 | | - <span>Create Template</span> |
62 | | - </CenteredText> |
63 | | - </Action> |
64 | | - )} |
65 | | - <Action danger onClick={onDelete}> |
66 | | - <CenteredText> |
67 | | - <TrashIcon /> |
68 | | - <span>{`Delete ${ |
69 | | - customTemplate ? `Template` : `Sandbox` |
70 | | - }`}</span> |
71 | | - </CenteredText> |
72 | | - </Action> |
73 | | - </Container> |
74 | | - </Group> |
75 | | - </> |
76 | | - ); |
| 47 | + const onDelete = ({ preventDefault }: MouseEvent<HTMLButtonElement>) => { |
| 48 | + preventDefault(); |
| 49 | + |
| 50 | + if (customTemplate) { |
| 51 | + deleteTemplate(); |
| 52 | + } else { |
| 53 | + modalOpened({ modal: 'deleteSandbox' }); |
77 | 54 | } |
78 | | - ) |
79 | | -); |
| 55 | + }; |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + {customTemplate && <TemplateConfig />} |
| 60 | + |
| 61 | + <Group> |
| 62 | + <Container> |
| 63 | + {!customTemplate && ( |
| 64 | + <Action onClick={onCreateTemplate}> |
| 65 | + <CenteredText> |
| 66 | + <span>Create Template</span> |
| 67 | + </CenteredText> |
| 68 | + </Action> |
| 69 | + )} |
| 70 | + |
| 71 | + <Action danger onClick={onDelete}> |
| 72 | + <CenteredText> |
| 73 | + <TrashIcon /> |
| 74 | + |
| 75 | + <span>{`Delete ${customTemplate ? `Template` : `Sandbox`}`}</span> |
| 76 | + </CenteredText> |
| 77 | + </Action> |
| 78 | + </Container> |
| 79 | + </Group> |
| 80 | + </> |
| 81 | + ); |
| 82 | +}; |
0 commit comments