Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new forceOAuthLogin option to appSlice #146

Merged
merged 1 commit into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions template-sample-app/template/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { useRoutes } from 'react-router-dom';

import {
Expand All @@ -14,6 +15,7 @@ import { cartoThemeOptions } from '@carto/react/ui';

import routes from './routes';
import { Header } from 'components/common/Header';
import Login from 'components/views/login/Login';

let theme = createMuiTheme(cartoThemeOptions);
theme = responsiveFontSizes(theme, {
Expand All @@ -40,19 +42,30 @@ theme = responsiveFontSizes(theme, {
const useStyles = makeStyles(() => ({
root: {
flex: 1,
overflow: 'hidden',
overflow: 'hidden'
},
}));

function App() {
const classes = useStyles();
const classes = useStyles();
const forceLogin = useSelector((state) => state.app.forceOAuthLogin);
const user = useSelector((state) => state.oauth.userInfo);

const displayLogin = forceLogin && !user;

const routing = useRoutes(routes);
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Grid container direction='column' alignItems='stretch' className={classes.root}>
<Header></Header>
{routing}
<Grid container direction='column' className={classes.root}>
{displayLogin ? (
<Login />
) : (
<>
<Header />
{routing}
</>
)}
</Grid>
</ThemeProvider>
);
Expand Down
77 changes: 77 additions & 0 deletions template-sample-app/template/src/components/views/login/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from "react";
import { useSelector, useDispatch } from "react-redux";
import { Button, Container, Grid, makeStyles, Paper, Typography } from "@material-ui/core";
import { useOAuthLogin } from "@carto/react/oauth";
import { setTokenAndUserInfoAsync } from "@carto/react/redux";

import { setError } from "config/appSlice";

const useStyles = makeStyles((theme) => ({
containerWrapper: {
flex: "1 1 auto",
display: "flex",
},
paperForm: {
width: theme.spacing(40),
height: theme.spacing(32),
padding: theme.spacing(4)
}
}));

export default function Login() {
const classes = useStyles();
const dispatch = useDispatch();
const oauthApp = useSelector((state) => state.oauth.oauthApp);

const onParamsRefreshed = (oauthParams) => {
if (oauthParams.error) {
dispatch(setError(`OAuth error: ${oauthParams.error}`));
} else {
dispatch(setTokenAndUserInfoAsync(oauthParams));
}
};

const [handleLogin] = useOAuthLogin(oauthApp, onParamsRefreshed);

return (
<Container className={classes.containerWrapper}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a Grid component here is better, unless you've found any issue to use it here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merging now, but I'll consider it when we apply the new design, thx!

<Grid
container
spacing={0}
direction="column"
alignItems="center"
justify="center"
style={{ minHeight: "100vh" }}
>

<Grid item xs={12}>
<Paper
variant="elevation"
elevation={4}
className={classes.paperForm}
>
<Grid container
spacing={4}
direction="column"
alignItems="center"
justify="center">
<Grid item>
<img src="/logo-primary.svg" alt="CARTO" />
</Grid>
<Grid item>
<Typography>Your credentials are required to login into <strong>CARTO for React app</strong>
</Typography>
</Grid>
<Grid item>
<Button color="inherit" variant="outlined" onClick={handleLogin}>
Login
</Button>
</Grid>
</Grid>
</Paper>
</Grid>

</Grid>
</Container>
);
}
1 change: 1 addition & 0 deletions template-sample-app/template/src/config/appSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const slice = createSlice({
error: null,
isolineResult: null,
bottomSheetOpen: false,
forceOAuthLogin: false
},
reducers: {
setIsolineResult: (state, action) => {
Expand Down
19 changes: 16 additions & 3 deletions template-skeleton/template/src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { useSelector } from 'react-redux';
import { useRoutes } from 'react-router-dom';

import {
Expand All @@ -14,6 +15,7 @@ import { cartoThemeOptions } from '@carto/react/ui';

import routes from './routes';
import { Header } from 'components/common/Header';
import Login from 'components/views/login/Login';

let theme = createMuiTheme(cartoThemeOptions);
theme = responsiveFontSizes(theme, {
Expand All @@ -40,19 +42,30 @@ theme = responsiveFontSizes(theme, {
const useStyles = makeStyles(() => ({
root: {
flex: 1,
overflow: 'hidden'
overflow: 'hidden',
},
}));

function App() {
const classes = useStyles();
const forceLogin = useSelector((state) => state.app.forceOAuthLogin);
const user = useSelector((state) => state.oauth.userInfo);

const displayLogin = forceLogin && !user;

const routing = useRoutes(routes);
return (
<ThemeProvider theme={theme}>
<CssBaseline />
<Grid container direction='column' className={classes.root}>
<Header></Header>
{routing}
{displayLogin ? (
<Login />
) : (
<>
<Header />
{routing}
</>
)}
</Grid>
</ThemeProvider>
);
Expand Down
82 changes: 82 additions & 0 deletions template-skeleton/template/src/components/views/login/Login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React from 'react';
import { useSelector, useDispatch } from 'react-redux';
import {
Button,
Container,
Grid,
makeStyles,
Paper,
Typography,
} from '@material-ui/core';
import { useOAuthLogin } from '@carto/react/oauth';
import { setTokenAndUserInfoAsync } from '@carto/react/redux';

import { setError } from 'config/appSlice';

const useStyles = makeStyles((theme) => ({
containerWrapper: {
flex: '1 1 auto',
display: 'flex',
},
paperForm: {
width: theme.spacing(40),
height: theme.spacing(32),
padding: theme.spacing(4),
},
}));

export default function Login() {
const classes = useStyles();
const dispatch = useDispatch();
const oauthApp = useSelector((state) => state.oauth.oauthApp);

const onParamsRefreshed = (oauthParams) => {
if (oauthParams.error) {
dispatch(setError(`OAuth error: ${oauthParams.error}`));
} else {
dispatch(setTokenAndUserInfoAsync(oauthParams));
}
};

const [handleLogin] = useOAuthLogin(oauthApp, onParamsRefreshed);

return (
<Container className={classes.containerWrapper}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as sample-app

<Grid
container
spacing={0}
direction='column'
alignItems='center'
justify='center'
style={{ minHeight: '100vh' }}
>
<Grid item xs={12}>
<Paper variant='elevation' elevation={4} className={classes.paperForm}>
<Grid
container
spacing={4}
direction='column'
alignItems='center'
justify='center'
>
<Grid item>
<img src='/logo-primary.svg' alt='CARTO' />
</Grid>
<Grid item>
<Typography>
Your credentials are required to login into{' '}
<strong>CARTO for React app</strong>
</Typography>
</Grid>
<Grid item>
<Button color='inherit' variant='outlined' onClick={handleLogin}>
Login
</Button>
</Grid>
</Grid>
</Paper>
</Grid>
</Grid>
</Container>
);
}
10 changes: 7 additions & 3 deletions template-skeleton/template/src/config/appSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const slice = createSlice({
initialState: {
error: null,
isolineResult: null,
bottomSheetOpen: false
bottomSheetOpen: false,
forceOAuthLogin: false,
},
reducers: {
setIsolineResult: (state, action) => {
Expand All @@ -16,12 +17,15 @@ const slice = createSlice({
},
setBottomSheetOpen: (state, action) => {
state.bottomSheetOpen = action.payload;
}
},
},
});

export default slice.reducer;

export const setIsolineResult = (payload) => ({ type: 'app/setIsolineResult', payload });
export const setError = (payload) => ({ type: 'app/setError', payload });
export const setBottomSheetOpen = (payload) => ({ type: 'app/setBottomSheetOpen', payload });
export const setBottomSheetOpen = (payload) => ({
type: 'app/setBottomSheetOpen',
payload,
});