Skip to content

Commit 9b54f58

Browse files
committed
fix redirect bug
1 parent edc54da commit 9b54f58

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

client/modules/IDE/components/Header/Toolbar.jsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useState, useEffect } from 'react';
22
import classNames from 'classnames';
33
import PropTypes from 'prop-types';
44
import { useTranslation } from 'react-i18next';
@@ -31,20 +31,21 @@ const Toolbar = (props) => {
3131
const autorefresh = useSelector((state) => state.preferences.autorefresh);
3232
const dispatch = useDispatch();
3333
const { t } = useTranslation();
34-
3534
const userIsOwner = user?.username === project.owner?.username;
35+
const [isPrivate, setIsPrivate] = useState(project.visibility === 'Private');
36+
useEffect(() => {
37+
setIsPrivate(project.visibility === 'Private');
38+
}, [project]);
39+
3640
const toggleVisibility = (e) => {
3741
try {
3842
const isChecked = e.target.checked;
39-
dispatch(
40-
changeVisibility(
41-
project.id,
42-
project.name,
43-
isChecked ? 'Private' : 'Public'
44-
)
45-
);
43+
const newVisibility = isChecked ? 'Private' : 'Public';
44+
setIsPrivate(isChecked);
45+
dispatch(changeVisibility(project.id, project.name, newVisibility));
4646
} catch (error) {
4747
console.log(error);
48+
setIsPrivate(project.visibility === 'Private');
4849
}
4950
};
5051

@@ -124,7 +125,7 @@ const Toolbar = (props) => {
124125
<input
125126
type="checkbox"
126127
className="toolbar__togglevisibility"
127-
defaultChecked={project.visibility === 'Private'}
128+
checked={isPrivate}
128129
onChange={toggleVisibility}
129130
/>
130131
</main>

client/modules/IDE/reducers/project.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ const project = (state, action) => {
2121
case ActionTypes.SET_PROJECT_NAME:
2222
return Object.assign({}, { ...state }, { name: action.name });
2323
case ActionTypes.SET_PROJECT_VISIBILITY:
24-
return state.map((sketch) => {
25-
if (sketch.id === action.id) {
26-
return { ...sketch, visibility: action.visibility };
27-
}
28-
return sketch;
29-
});
24+
return Object.assign({}, { ...state }, { visibility: action.visibility });
3025
case ActionTypes.NEW_PROJECT:
3126
return {
3227
id: action.project.id,

client/protected-route.jsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { Route, Redirect } from 'react-router-dom';
3-
import { useSelector } from 'react-redux';
3+
import { useSelector, useDispatch } from 'react-redux';
44
import { getIsUserOwner } from './modules/IDE/selectors/users';
5+
import { resetProject } from './modules/IDE/actions/project';
56

67
// eslint-disable-next-line react/prop-types
78
const ProtectedSketchRoute = ({ component: Component, ...rest }) => {
89
const project = useSelector((state) => state.project);
910
const isUserOwner = useSelector(getIsUserOwner);
11+
const dispatch = useDispatch();
12+
const hasAccess = isUserOwner || project.visibility !== 'Private';
13+
useEffect(() => {
14+
if (!hasAccess) {
15+
dispatch(resetProject());
16+
}
17+
}, [hasAccess, dispatch]);
1018

11-
console.log(project.id);
1219
return (
1320
<Route
1421
{...rest}
1522
render={(props) => {
16-
// Allow access if user is owner or sketch is public
17-
if (isUserOwner || project.visibility !== 'Private') {
18-
return <Component {...props} />;
23+
if (!hasAccess) {
24+
return <Redirect to="/" />;
1925
}
20-
21-
// Redirect if not so
22-
return (
23-
<Redirect
24-
to={{
25-
pathname: '/'
26-
}}
27-
/>
28-
);
26+
return <Component {...props} />;
2927
}}
3028
/>
3129
);

client/routes.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Route.propTypes = {
4646

4747
const routes = (
4848
<Switch>
49-
<Route exact path="/" component={IDEView} />
49+
<ProtectedSketchRoute exact path="/" component={IDEView} />
5050
<Route path="/login" component={LoginView} />
5151
<Route path="/signup" component={SignupView} />
5252
<Route

p5.js-web-editor

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit edc54da56a8fc86e902e0f09ffc4c480301689c1

0 commit comments

Comments
 (0)