Commit 5096aa74 by Giang Tran

update code

parent d0825019
import React, { useState, useEffect } from "react";
import HomeView from "./HomeView"; import HomeView from "./HomeView";
import { useLocation } from "react-router-dom";
const HomeContainer = (props) => { const HomeContainer = (props) => {
return <HomeView />; const location = useLocation();
const [state, setState] = useState({
latitude: null,
longitude: null,
});
useEffect(() => {
getPosition();
}, []);
const [user, setUser] = useState({
name: location.state.fullName,
code: location.state.code,
});
const getPosition = async () => {
navigator.geolocation.getCurrentPosition((position) => {
if (position.coords.longitude)
setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
});
};
const onCheckIn = () => {
console.log(state);
};
return <HomeView user={user} onCheckIn={onCheckIn} />;
}; };
export default HomeContainer; export default HomeContainer;
...@@ -12,38 +12,25 @@ import { ...@@ -12,38 +12,25 @@ import {
import ButtonCheckIn from "./ButtonCheckIn"; import ButtonCheckIn from "./ButtonCheckIn";
const HomeView = (props) => { const HomeView = (props) => {
const [state, setState] = useState({ const { user, onCheckIn } = props;
latitude: null,
longitude: null,
});
const [status, setStatus] = useState(0); const [status, setStatus] = useState(0);
const getPosition = () => {
navigator.geolocation.getCurrentPosition((position) => {
console.log(position.coords.longitude);
console.log(position.coords.latitude);
});
if (status == 0) {
setStatus(1);
} else {
setStatus(0);
}
};
useEffect(() => {
getPosition();
}, []);
return ( return (
<Container fixed style={{ height: "100vh" }}> <Container fixed style={{ height: "100vh" }}>
<div style={{ marginTop: 50 }} /> <div style={{ marginTop: 50 }} />
<div style={{ textAlign: "center", fontSize: "35px" }}> <div style={{ textAlign: "center", fontSize: "35px" }}>
Tên: Lê Trng Trí <Typography variant={"h4"} style={{ color: " #3399ff" }}>
Chào mng {user ? user.name : ""}
</Typography>
</div> </div>
<br /> <br />
<Grid container style={{ height: "100vh", justifyContent: "center" }}> <Grid container style={{ height: "100vh", justifyContent: "center" }}>
<Fab <Fab
color="secondary" color="secondary"
aria-label="edit" aria-label="edit"
style={{ width: "20vw", height: "20vw" }} style={{ width: "30vw", height: "30vw" }}
onClick={getPosition} onClick={onCheckIn}
> >
<span style={{ fontSize: "3vw" }}> <span style={{ fontSize: "3vw" }}>
{status == 0 ? "Checkin" : "Checkout"} {status == 0 ? "Checkin" : "Checkout"}
......
import React from "react"; import React, { useState } from "react";
import Avatar from "@material-ui/core/Avatar"; import Avatar from "@material-ui/core/Avatar";
import Button from "@material-ui/core/Button"; import Button from "@material-ui/core/Button";
import CssBaseline from "@material-ui/core/CssBaseline"; import CssBaseline from "@material-ui/core/CssBaseline";
import TextField from "@material-ui/core/TextField"; import TextField from "@material-ui/core/TextField";
import FormControlLabel from "@material-ui/core/FormControlLabel"; import { FormHelperText, Link, Grid, Box, Typography } from "@material-ui/core";
import Checkbox from "@material-ui/core/Checkbox"; import { useHistory } from "react-router-dom";
import Link from "@material-ui/core/Link";
import Grid from "@material-ui/core/Grid";
import Box from "@material-ui/core/Box";
import LockOutlinedIcon from "@material-ui/icons/LockOutlined"; import LockOutlinedIcon from "@material-ui/icons/LockOutlined";
import Typography from "@material-ui/core/Typography";
import { makeStyles } from "@material-ui/core/styles"; import { makeStyles } from "@material-ui/core/styles";
import Container from "@material-ui/core/Container"; import Container from "@material-ui/core/Container";
import { loginApi } from "../../api/user";
function Copyright() { function Copyright() {
return ( return (
...@@ -46,27 +43,63 @@ const useStyles = makeStyles((theme) => ({ ...@@ -46,27 +43,63 @@ const useStyles = makeStyles((theme) => ({
const LoginScreen = () => { const LoginScreen = () => {
const classes = useStyles(); const classes = useStyles();
let history = useHistory();
const [state, setState] = useState({
appcode: "Dcv",
username: "",
password: "",
});
const [helperText, sethelperText] = useState("");
const onLogin = async () => {
console.log("Abc");
if (state.username === "" || state.password === "") {
sethelperText("");
sethelperText("Cần phải điền đầy đủ email và password");
} else {
sethelperText("");
console.log(state);
const res = await loginApi(state);
console.log(res.data);
if (res.data && res.data.errorCode === 0) {
localStorage.setItem("TOKEN_KEY", res.data.data.accessToken);
history.push({
pathname: "/home",
state: { code: res.data.data.code, fullName: res.data.data.fullName },
});
} else if (res.data && res.data.errorCode === 1) {
sethelperText("");
sethelperText("Sai email hoặc mật khẩu");
}
}
};
return ( return (
<div style={{ height: "100vh" }}> <div style={{ height: "100vh" }}>
<Container component="main" maxWidth="xs"> <Container component="main" maxWidth="xs">
<CssBaseline /> <CssBaseline />
<div className={classes.paper}> <div className={classes.paper}>
<Avatar className={classes.avatar}> <img
<LockOutlinedIcon /> style={{ width: 200, height: 100, marginTop: 10, marginBottom: 10 }}
</Avatar> src="/logo-DCV.png"
<Typography component="h1" variant="h5"> />
Sign in <Typography color={"primary"} component="h1" variant="h5">
ĐĂNG NHP
</Typography> </Typography>
<form className={classes.form} noValidate>
<TextField <TextField
variant="outlined" variant="outlined"
margin="normal" margin="normal"
required required
fullWidth fullWidth
id="email" label="email"
label="Email Address"
name="email"
autoComplete="email" autoComplete="email"
onChange={(e) => {
setState({
...state,
username: e.target.value,
});
}}
autoFocus autoFocus
/> />
<TextField <TextField
...@@ -74,38 +107,41 @@ const LoginScreen = () => { ...@@ -74,38 +107,41 @@ const LoginScreen = () => {
margin="normal" margin="normal"
required required
fullWidth fullWidth
name="password" label="Mật khẩu"
label="Password"
type="password" type="password"
id="password" onChange={(e) => {
setState({
...state,
password: e.target.value,
});
}}
autoComplete="current-password" autoComplete="current-password"
/> />
<FormControlLabel <div style={{ width: "100%" }}>
control={<Checkbox value="remember" color="primary" />} <FormHelperText
label="Remember me" style={{ marginLeft: "16px", color: "red", fontSize: "13px" }}
/> >
{helperText}
</FormHelperText>
</div>
<Button <Button
type="submit" type="submit"
fullWidth fullWidth
variant="contained" variant="contained"
color="primary" color="primary"
className={classes.submit} className={classes.submit}
onClick={onLogin}
> >
Sign In Đăng nhp
</Button> </Button>
<Grid container> <Grid container>
<Grid item xs> <Grid item xs>
<Link href="#" variant="body2"> <Link href="#" variant="body2">
Forgot password? Quên mt khu?
</Link>
</Grid>
<Grid item>
<Link href="#" variant="body2">
{"Don't have an account? Sign Up"}
</Link> </Link>
</Grid> </Grid>
</Grid> </Grid>
</form>
</div> </div>
<Box mt={8}> <Box mt={8}>
<Copyright /> <Copyright />
......
import axios from "axios";
axios.defaults.timeout = 10000;
export async function GetData(url, data) {
const token = localStorage.getItem("TOKEN_KEY");
let myRequest = {
method: "get",
url,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
params: {
...data,
},
timeout: 60 * 1000,
// withCredentials: true,
};
console.log("My request", myRequest);
return await axios(myRequest)
.then((response) => response)
.then((response) => response)
.catch((error) => {
console.log(error.request);
const err = {
message: "error",
status: error.request.status,
};
return err;
});
}
export async function PostLogin(url, json, isAuth = true) {
let myRequest = {
method: "post",
url,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
timeout: 60 * 1000,
data: JSON.stringify(json),
};
console.log("post data mobile", myRequest);
return await axios(myRequest)
.then((response) => response)
.then((response) => response)
.catch((error) => {
console.log(error.request);
const err = {
message: "error",
status: error.request.status,
};
return err;
});
}
export async function PostData(url, json, isAuth = true) {
const token = localStorage.getItem("TOKEN_KEY");
let myRequest = {
method: "post",
url,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
timeout: 60 * 1000,
data: JSON.stringify(json),
};
console.log("post data mobile", myRequest);
return await axios(myRequest)
.then((response) => response)
.then((response) => response)
.catch((error) => {
console.log(error.request);
const err = {
message: "error",
status: error.request.status,
};
return err;
});
}
import { GetData, PostLogin, PostData } from "./api";
export const loginApi = async (body) =>
PostLogin("http://hrm.auth.dcv.vn/api/v1/auth/login", body)
.then((res) => res)
.catch((err) => err);
export const getDataApi = async (body) =>
GetData("http://hrm.auth.dcv.vn/api/v1/auth/login", body)
.then((res) => res)
.catch((err) => err);
export const checkInApi = async (body) =>
PostData("http://hrm.japi.dcv.vn/venus-service/checkinService/checkin", body)
.then((res) => res)
.catch((err) => err);
export const checkOutApi = async (body) =>
PostData("http://hrm.japi.dcv.vn/venus-service/checkinService/checkout", body)
.then((res) => res)
.catch((err) => err);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment