Skip to content

Commit 9d695b4

Browse files
MihirGHCompuIves
authored andcommitted
Refactor|Overmind: Refactor MoveSandboxFolderModal to use overmind (codesandbox#2751)
1 parent 0815717 commit 9d695b4

File tree

1 file changed

+87
-98
lines changed
  • packages/app/src/app/pages/common/Modals/MoveSandboxFolderModal

1 file changed

+87
-98
lines changed
Lines changed: 87 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React from 'react';
2-
import { inject, observer } from 'app/componentConnectors';
1+
import React, { useState, useEffect, useCallback } from 'react';
2+
import { useOvermind } from 'app/overmind';
33
import { basename } from 'path';
44
import track from '@codesandbox/common/lib/utils/analytics';
55
import { Button } from '@codesandbox/common/lib/components/Button';
@@ -11,99 +11,88 @@ import { Container } from '../elements';
1111
import { Block, CancelButton } from './elements';
1212
import { addSandboxesToFolder } from '../../../Dashboard/queries';
1313

14-
class MoveSandboxFolderModal extends React.Component {
15-
state = {
16-
loading: false,
17-
error: undefined,
18-
};
19-
20-
constructor(props) {
21-
super(props);
22-
23-
const sandbox = props.store.editor.currentSandbox;
24-
const { collection } = sandbox;
25-
26-
this.state = {
27-
teamId: sandbox.team ? sandbox.team.id || undefined : undefined,
28-
path: collection ? collection.path : '/',
29-
};
30-
}
31-
32-
onSelect = ({ teamId, path }) => {
33-
this.setState({ teamId, path });
34-
};
35-
36-
handleMove = () => {
37-
this.setState({ loading: true, error: undefined }, () => {
38-
addSandboxesToFolder(
39-
[this.props.store.editor.currentSandbox.id],
40-
this.state.path,
41-
this.state.teamId
42-
)
43-
.then(() => {
44-
this.props.signals.refetchSandboxInfo();
45-
46-
this.setState({ loading: false });
47-
this.props.signals.modalClosed();
48-
49-
track('Move Sandbox From Editor');
50-
})
51-
.catch(e => {
52-
this.setState({ error: e.message, loading: false });
53-
});
54-
});
55-
};
56-
57-
render() {
58-
const { path, teamId } = this.state;
59-
const { signals } = this.props;
60-
61-
return (
62-
<div>
63-
<Block>Move to Folder</Block>
64-
<Container css={{ maxHeight: 400, overflow: 'auto' }}>
65-
<DirectoryPicker
66-
onSelect={this.onSelect}
67-
currentTeamId={teamId}
68-
currentPath={path}
69-
/>
70-
</Container>
71-
72-
{this.state.error}
73-
74-
<Block right>
75-
<CancelButton
76-
onClick={() => {
77-
signals.modalClosed();
78-
}}
79-
>
80-
Cancel
81-
</CancelButton>
82-
83-
<Button
84-
onClick={this.handleMove}
85-
css={{ display: 'inline-flex', alignItems: 'center' }}
86-
small
87-
disabled={this.state.loading}
88-
>
89-
{this.state.loading ? (
90-
'Moving Sandbox...'
91-
) : (
92-
<>
93-
Move to{' '}
94-
{path !== '/'
95-
? basename(path)
96-
: `${teamId ? 'Our' : 'My'} Sandboxes`}
97-
<ChevronRight
98-
css={{ marginRight: '-.25rem', marginLeft: '.25rem' }}
99-
/>
100-
</>
101-
)}
102-
</Button>
103-
</Block>
104-
</div>
105-
);
106-
}
107-
}
108-
109-
export default inject('store', 'signals')(observer(MoveSandboxFolderModal));
14+
const MoveSandboxFolderModal = () => {
15+
const {
16+
state: {
17+
editor: { currentSandbox: sandbox },
18+
},
19+
actions: { refetchSandboxInfo, modalClosed },
20+
} = useOvermind();
21+
const { collection } = sandbox;
22+
23+
const [loading, setLoading] = useState(false);
24+
const [error, setError] = useState(undefined);
25+
const [teamId, setTeamId] = useState(
26+
sandbox.team ? sandbox.team.id || undefined : undefined
27+
);
28+
const [path, setPath] = useState(collection ? collection.path : '/');
29+
30+
const onSelect = useCallback(({ teamId: newTeamId, path: newPath }) => {
31+
setTeamId(newTeamId);
32+
setPath(newPath);
33+
}, []);
34+
35+
const handleMove = useCallback(() => {
36+
setLoading(true);
37+
setError(undefined);
38+
}, []);
39+
40+
useEffect(() => {
41+
if (!loading) return;
42+
addSandboxesToFolder([sandbox.id], path, teamId)
43+
.then(() => {
44+
refetchSandboxInfo();
45+
46+
setLoading(false);
47+
modalClosed();
48+
49+
track('Move Sandbox From Editor');
50+
})
51+
.catch(e => {
52+
setError(e.message);
53+
setLoading(false);
54+
});
55+
}, [loading, path, teamId, sandbox, refetchSandboxInfo, modalClosed]);
56+
57+
return (
58+
<div>
59+
<Block>Move to Folder</Block>
60+
<Container css={{ maxHeight: 400, overflow: 'auto' }}>
61+
<DirectoryPicker
62+
onSelect={onSelect}
63+
currentTeamId={teamId}
64+
currentPath={path}
65+
/>
66+
</Container>
67+
68+
{error}
69+
70+
<Block right>
71+
<CancelButton onClick={modalClosed}>Cancel</CancelButton>
72+
73+
<Button
74+
onClick={handleMove}
75+
css={{ display: 'inline-flex', alignItems: 'center' }}
76+
small
77+
disabled={loading}
78+
>
79+
{loading ? (
80+
'Moving Sandbox...'
81+
) : (
82+
<>
83+
Move to{' '}
84+
{path !== '/'
85+
? basename(path)
86+
: `${teamId ? 'Our' : 'My'} Sandboxes`}
87+
<ChevronRight
88+
css={{ marginRight: '-.25rem', marginLeft: '.25rem' }}
89+
/>
90+
</>
91+
)}
92+
</Button>
93+
</Block>
94+
</div>
95+
);
96+
};
97+
98+
export default MoveSandboxFolderModal;

0 commit comments

Comments
 (0)