Skip to content

Commit

Permalink
remove redundant code, render button by permission
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeiStella committed Aug 2, 2023
1 parent b023884 commit 8b25df5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/components/LeaderBoard/Leaderboard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
assignStarDotColors,
showStar,
} from 'utils/leaderboardPermissions';
import MouseoverTextTotalTime from '../mouseoverText/mouseoverTextTotalTime';
import MouseoverTextTotalTimeButton from '../mouseoverText/MouseoverTextTotalTimeButton';
import MouseoverTextTotalTimeUpdater from '../mouseoverText/MouseoverTextTotalTimeUpdater';

function useDeepEffect(effectFunc, deps) {
const isFirst = useRef(true);
Expand Down Expand Up @@ -219,12 +220,10 @@ const LeaderBoard = ({
<span className="d-none d-sm-inline-block" title={mouseoverText}>Total Time </span>
</div>
{isOwner && (
<MouseoverTextTotalTime onUpdate={handleMouseoverTextUpdate} />
<MouseoverTextTotalTimeButton onUpdate={handleMouseoverTextUpdate} />
)}
{!isOwner && (
<div style={{ visibility: 'hidden' }}>
<MouseoverTextTotalTime onUpdate={handleMouseoverTextUpdate} />
</div>
<MouseoverTextTotalTimeUpdater onUpdate={handleMouseoverTextUpdate} />
)}
</div>
</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { connect } from 'react-redux';
import { getMouseoverText, createMouseoverText, updateMouseoverText } from '../../actions/mouseoverTextAction';


function MouseoverTextTotalTime({
function MouseoverTextTotalTimeButton({
getMouseoverText,
createMouseoverText,
updateMouseoverText,
Expand Down Expand Up @@ -46,16 +46,13 @@ function MouseoverTextTotalTime({
};
if (text) {
updateMouseoverText(mouseoverTextId, mouseoverText);
setModalOpen(false);
setText(newText);
onUpdate(newText);
} else {
//console.log('createMouseoverText is ' + mouseoverText.newMouseoverText);
createMouseoverText(mouseoverText);
setModalOpen(false);
setText(newText);
onUpdate(newText);
}
setModalOpen(false);
setText(newText);
onUpdate(newText);
};

const handleCancelSave = () => {
Expand Down Expand Up @@ -116,7 +113,7 @@ const mapDispatchToProps = dispatch => ({
dispatch(updateMouseoverText(mouseoverTextId, mouseoverText)),
});

export default connect(mapStateToProps, mapDispatchToProps)(MouseoverTextTotalTime);
export default connect(mapStateToProps, mapDispatchToProps)(MouseoverTextTotalTimeButton);



Expand Down
29 changes: 29 additions & 0 deletions src/components/mouseoverText/MouseoverTextTotalTimeUpdater.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useEffect } from 'react';
import { connect } from 'react-redux';
import { getMouseoverText } from '../../actions/mouseoverTextAction';

function MouseoverTextTotalTimeUpdater({ getMouseoverText, mouseoverText, onUpdate }) {
useEffect(() => {
getMouseoverText();
}, [getMouseoverText]);

useEffect(() => {
if (mouseoverText) {
onUpdate(mouseoverText); // Call the onUpdate function to pass the mouseoverText value to the parent component
}
}, [mouseoverText, onUpdate]);

return null;
}

const mapStateToProps = (state) => {
return {
mouseoverText: state?.mouseoverText?.[0]?.mouseoverText,
};
};

const mapDispatchToProps = (dispatch) => ({
getMouseoverText: () => dispatch(getMouseoverText()),
});

export default connect(mapStateToProps, mapDispatchToProps)(MouseoverTextTotalTimeUpdater);

0 comments on commit 8b25df5

Please sign in to comment.