|
1 | | -import React from 'react'; |
| 1 | +import React, { useState, useEffect } from 'react'; |
2 | 2 | import classNames from 'classnames'; |
3 | 3 | import PropTypes from 'prop-types'; |
4 | 4 | import { useTranslation } from 'react-i18next'; |
@@ -31,20 +31,21 @@ const Toolbar = (props) => { |
31 | 31 | const autorefresh = useSelector((state) => state.preferences.autorefresh); |
32 | 32 | const dispatch = useDispatch(); |
33 | 33 | const { t } = useTranslation(); |
34 | | - |
35 | 34 | 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 | + |
36 | 40 | const toggleVisibility = (e) => { |
37 | 41 | try { |
38 | 42 | 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)); |
46 | 46 | } catch (error) { |
47 | 47 | console.log(error); |
| 48 | + setIsPrivate(project.visibility === 'Private'); |
48 | 49 | } |
49 | 50 | }; |
50 | 51 |
|
@@ -124,7 +125,7 @@ const Toolbar = (props) => { |
124 | 125 | <input |
125 | 126 | type="checkbox" |
126 | 127 | className="toolbar__togglevisibility" |
127 | | - defaultChecked={project.visibility === 'Private'} |
| 128 | + checked={isPrivate} |
128 | 129 | onChange={toggleVisibility} |
129 | 130 | /> |
130 | 131 | </main> |
|
0 commit comments