-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathuseSketchActions.js
85 lines (76 loc) · 2.38 KB
/
useSketchActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { useDispatch, useSelector } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { useParams } from 'react-router';
import {
autosaveProject,
exportProjectAsZip,
newProject,
newModuleProject,
saveProject,
setProjectName
} from '../actions/project';
import { showToast } from '../actions/toast';
import { showErrorModal, showShareModal } from '../actions/ide';
import { selectCanEditSketch } from '../selectors/users';
const useSketchActions = () => {
const unsavedChanges = useSelector((state) => state.ide.unsavedChanges);
const authenticated = useSelector((state) => state.user.authenticated);
const project = useSelector((state) => state.project);
const user = useSelector((state) => state.user);
const canEditProjectName = useSelector(selectCanEditSketch);
const dispatch = useDispatch();
const { t } = useTranslation();
const params = useParams();
function newSketch() {
if (!unsavedChanges) {
dispatch(showToast('Toast.OpenedNewSketch'));
dispatch(newProject());
} else if (window.confirm(t('Nav.WarningUnsavedChanges'))) {
dispatch(showToast('Toast.OpenedNewSketch'));
dispatch(newProject());
}
}
function newModuleSketch() {
if (!unsavedChanges) {
dispatch(showToast('Toast.OpenedNewModuleSketch'));
dispatch(newModuleProject());
} else if (window.confirm(t('Nav.WarningUnsavedChanges'))) {
dispatch(showToast('Toast.OpenedNewModuleSketch'));
dispatch(newModuleProject());
}
}
function saveSketch(cmController) {
if (authenticated) {
dispatch(saveProject(cmController?.getContent()));
} else {
dispatch(showErrorModal('forceAuthentication'));
}
}
function downloadSketch() {
if (authenticated && user.id === project.owner.id) {
dispatch(autosaveProject());
exportProjectAsZip(project.id);
}
}
function shareSketch() {
const { username } = params;
dispatch(showShareModal(project.id, project.name, username));
}
function changeSketchName(name) {
const newProjectName = name.trim();
if (newProjectName.length > 0) {
dispatch(setProjectName(newProjectName));
if (project.id) dispatch(saveProject());
}
}
return {
newSketch,
newModuleSketch,
saveSketch,
downloadSketch,
shareSketch,
changeSketchName,
canEditProjectName
};
};
export default useSketchActions;