Skip to content

Commit

Permalink
Merge pull request #591 from ydcjeff/issue-277
Browse files Browse the repository at this point in the history
feat: show login modal when anonymous user clicks on comment btn
  • Loading branch information
MrOrz authored Dec 22, 2024
2 parents 9f97ed6 + 07086fc commit ed528ce
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
61 changes: 32 additions & 29 deletions components/AppLayout/AppLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import LoginModal from './LoginModal';
import fetchAPI from 'lib/fetchAPI';
import { blockUserBrowserAndRefreshIfNeeded } from 'lib/isUserBlocked';
import Snackbar from '@material-ui/core/Snackbar';
import { AppProvider } from './context';

const USER_QUERY = gql`
query AppLayoutQuery {
Expand Down Expand Up @@ -86,35 +87,37 @@ function AppLayout({ children, container = true }) {

return (
<Fragment>
<AppHeader
user={data?.GetUser}
showProgress={isRouteChanging}
onMenuButtonClick={toggleSidebar}
onLoginModalOpen={openLoginModal}
onLogout={logout}
/>
<AppSidebar
open={sidebarOpen}
toggle={setSidebarOpen}
user={data?.GetUser}
onLoginModalOpen={openLoginModal}
onLogout={logout}
/>
{container ? (
<Container className={classes.container}>{children}</Container>
) : (
children
)}
<AppFooter />
{loginModalOpen && (
<LoginModal onClose={() => setLoginModalOpen(false)} />
)}
<Snackbar
open={snackMsg ? true : false}
onClose={() => setSnackMsg('')}
message={snackMsg}
/>
<UpgradeDialog />
<AppProvider value={{ openLoginModal }}>
<AppHeader
user={data?.GetUser}
showProgress={isRouteChanging}
onMenuButtonClick={toggleSidebar}
onLoginModalOpen={openLoginModal}
onLogout={logout}
/>
<AppSidebar
open={sidebarOpen}
toggle={setSidebarOpen}
user={data?.GetUser}
onLoginModalOpen={openLoginModal}
onLogout={logout}
/>
{container ? (
<Container className={classes.container}>{children}</Container>
) : (
children
)}
<AppFooter />
{loginModalOpen && (
<LoginModal onClose={() => setLoginModalOpen(false)} />
)}
<Snackbar
open={snackMsg ? true : false}
onClose={() => setSnackMsg('')}
message={snackMsg}
/>
<UpgradeDialog />
</AppProvider>
</Fragment>
);
}
Expand Down
9 changes: 9 additions & 0 deletions components/AppLayout/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createContext, useContext } from 'react';

const AppContext = createContext();

export const useAppContext = () => useContext(AppContext);

export const AppProvider = ({ children, value }) => {
return <AppContext.Provider value={value}>{children}</AppContext.Provider>;
};
4 changes: 3 additions & 1 deletion components/CreateReplyRequestForm/CreateReplyRequestForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ReportAbuseMenuItem, {
} from 'components/ActionMenu/ReportAbuseMenuItem';
import useCurrentUser from 'lib/useCurrentUser';
import cx from 'clsx';
import { useAppContext } from 'components/AppLayout/context';

const localStorage = typeof window === 'undefined' ? {} : window.localStorage;

Expand Down Expand Up @@ -201,12 +202,13 @@ const CreateReplyRequestForm = React.memo(

const classes = useStyles();
const user = useCurrentUser();
const { openLoginModal } = useAppContext();

const onCommentButtonClick = () => {
if (user) setShowForm(!showForm);
else {
setShowForm(false);
alert(t`Please login first.`);
openLoginModal();
}
};

Expand Down

0 comments on commit ed528ce

Please sign in to comment.