update base url
This commit is contained in:
@ -1,135 +1,109 @@
|
||||
import React, { useState } from "react";
|
||||
import { TextField, Button, Box, Typography, Grid, Paper, Avatar, Divider } from "@mui/material";
|
||||
import {Link, useNavigate } from 'react-router-dom';
|
||||
import Cookies from "js-cookie";
|
||||
import { fetchApi } from '../config/axios';
|
||||
import logo from '../logo/logo.jpeg';
|
||||
import React, { useState } from "react"
|
||||
import { TextField, Button, Box, Typography, Grid, Paper, Avatar, Divider } from "@mui/material"
|
||||
import { Link, useNavigate } from "react-router-dom"
|
||||
import Cookies from "js-cookie"
|
||||
import { fetchApi } from "../config/axios"
|
||||
import logo from "../logo/logo.jpeg"
|
||||
|
||||
const LoginPage = () => {
|
||||
// State for email and password
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const navigate = useNavigate();
|
||||
// State for email and password
|
||||
const [username, setUsername] = useState("")
|
||||
const [password, setPassword] = useState("")
|
||||
const navigate = useNavigate()
|
||||
|
||||
// Handle form submission
|
||||
const handleSubmit = async (e) => {
|
||||
try {
|
||||
e.preventDefault();
|
||||
if (username && password) {
|
||||
// GET CREDIENTIAL
|
||||
let user = await fetchApi('/api/login', 'POST', { username, password });
|
||||
if (user) {
|
||||
if (user.data && user.data.data) {
|
||||
let resUserLogin = user.data.data;
|
||||
console.log('resUserLogin', resUserLogin);
|
||||
let roleList = JSON.stringify(resUserLogin.roles_list);
|
||||
Cookies.set('token', resUserLogin.token.access_token, { expires: 1, path: '/' });
|
||||
Cookies.set('role', resUserLogin.role_name, { expires: 1, path: '/' });
|
||||
Cookies.set('userId', resUserLogin.user.id, { expires: 1, path: '/' });
|
||||
Cookies.set('username', resUserLogin.user.username, { expires: 1, path: '/' });
|
||||
// Cookies.set('roles_list', roleList, { expires: 1, path: '/' });
|
||||
localStorage.setItem('roles_list', roleList)
|
||||
navigate('/');
|
||||
} else {
|
||||
alert(user.response.data.message);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alert("Please fill in both fields.");
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response) alert(error.response.data.message)
|
||||
else alert(error.message)
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
// Handle form submission
|
||||
const handleSubmit = async (e) => {
|
||||
try {
|
||||
e.preventDefault()
|
||||
if (username && password) {
|
||||
// GET CREDIENTIAL
|
||||
let user = await fetchApi("/d/login", "POST", { username, password })
|
||||
if (user) {
|
||||
if (user.data && user.data.data) {
|
||||
let resUserLogin = user.data.data
|
||||
console.log("resUserLogin", resUserLogin)
|
||||
let roleList = JSON.stringify(resUserLogin.roles_list)
|
||||
Cookies.set("token", resUserLogin.token.access_token, { expires: 1, path: "/" })
|
||||
Cookies.set("role", resUserLogin.role_name, { expires: 1, path: "/" })
|
||||
Cookies.set("userId", resUserLogin.user.id, { expires: 1, path: "/" })
|
||||
Cookies.set("username", resUserLogin.user.username, { expires: 1, path: "/" })
|
||||
// Cookies.set('roles_list', roleList, { expires: 1, path: '/' });
|
||||
localStorage.setItem("roles_list", roleList)
|
||||
navigate("/")
|
||||
} else {
|
||||
alert(user.response.data.message)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
alert("Please fill in both fields.")
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.response) alert(error.response.data.message)
|
||||
else alert(error.message)
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Grid
|
||||
container
|
||||
justifyContent="center"
|
||||
alignItems="center"
|
||||
style={{ minHeight: "100vh", backgroundColor: "#f4f6f8" }}
|
||||
>
|
||||
<Grid item xs={12} sm={6} md={4}>
|
||||
<Paper elevation={3} sx={{ padding: 3 }}>
|
||||
<Typography variant="h5" align="center" gutterBottom>
|
||||
Login
|
||||
</Typography>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Box mb={2}>
|
||||
<TextField
|
||||
label="Email"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
type="text"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2}>
|
||||
<TextField
|
||||
label="Password"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mt={2} textAlign="center">
|
||||
<Button>
|
||||
<Link to="/register">Register</Link>
|
||||
</Button>
|
||||
<Button
|
||||
variant="contained"
|
||||
color="primary"
|
||||
type="submit"
|
||||
fullWidth
|
||||
>
|
||||
Login
|
||||
</Button>
|
||||
</Box>
|
||||
</form>
|
||||
</Paper>
|
||||
</Grid>
|
||||
<Box
|
||||
sx={{
|
||||
width: "100%",
|
||||
position: "fixed",
|
||||
bottom: 0,
|
||||
textAlign: "center",
|
||||
bgcolor: "#f9f9f9", // Background color
|
||||
py: 2, // Padding
|
||||
}}
|
||||
>
|
||||
{/* Divider (Horizontal Line) */}
|
||||
<Divider sx={{ mb: 2 }} />
|
||||
|
||||
{/* Content */}
|
||||
<Box justifyItems={'center'}
|
||||
sx={{
|
||||
display: "flex",
|
||||
alignItems: "center", // Align items vertically
|
||||
justifyContent: "center", // Center content horizontally
|
||||
gap: 1, // Space between logo and text
|
||||
mt: 2, // Optional margin-top
|
||||
}}>
|
||||
<Avatar
|
||||
src={logo}
|
||||
alt="T-PAY"
|
||||
sx={{ backgroundColor: 'red', width: '50px', height: '50px' }}
|
||||
/>
|
||||
<Typography variant="h6" sx={{ fontWeight: "bold" }}>T-PAY</Typography>
|
||||
</Box>
|
||||
<Typography variant="body2" sx={{ color: "gray" }}>
|
||||
© 2025 TelinDigital Solution
|
||||
</Typography>
|
||||
</Box>
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
return (
|
||||
<Grid container justifyContent="center" alignItems="center" style={{ minHeight: "100vh", backgroundColor: "#f4f6f8" }}>
|
||||
<Grid item xs={12} sm={6} md={4}>
|
||||
<Paper elevation={3} sx={{ padding: 3 }}>
|
||||
<Typography variant="h5" align="center" gutterBottom>
|
||||
Login
|
||||
</Typography>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Box mb={2}>
|
||||
<TextField label="Email" variant="outlined" fullWidth value={username} onChange={(e) => setUsername(e.target.value)} type="text" required />
|
||||
</Box>
|
||||
<Box mb={2}>
|
||||
<TextField label="Password" variant="outlined" fullWidth value={password} onChange={(e) => setPassword(e.target.value)} type="password" required />
|
||||
</Box>
|
||||
<Box mt={2} textAlign="center">
|
||||
<Button>
|
||||
<Link to="/register">Register</Link>
|
||||
</Button>
|
||||
<Button variant="contained" color="primary" type="submit" fullWidth>
|
||||
Login
|
||||
</Button>
|
||||
</Box>
|
||||
</form>
|
||||
</Paper>
|
||||
</Grid>
|
||||
<Box
|
||||
sx={{
|
||||
width: "100%",
|
||||
position: "fixed",
|
||||
bottom: 0,
|
||||
textAlign: "center",
|
||||
bgcolor: "#f9f9f9", // Background color
|
||||
py: 2, // Padding
|
||||
}}
|
||||
>
|
||||
{/* Divider (Horizontal Line) */}
|
||||
<Divider sx={{ mb: 2 }} />
|
||||
|
||||
export default LoginPage;
|
||||
{/* Content */}
|
||||
<Box
|
||||
justifyItems={"center"}
|
||||
sx={{
|
||||
display: "flex",
|
||||
alignItems: "center", // Align items vertically
|
||||
justifyContent: "center", // Center content horizontally
|
||||
gap: 1, // Space between logo and text
|
||||
mt: 2, // Optional margin-top
|
||||
}}
|
||||
>
|
||||
<Avatar src={logo} alt="T-PAY" sx={{ backgroundColor: "red", width: "50px", height: "50px" }} />
|
||||
<Typography variant="h6" sx={{ fontWeight: "bold" }}>
|
||||
T-PAY
|
||||
</Typography>
|
||||
</Box>
|
||||
<Typography variant="body2" sx={{ color: "gray" }}>
|
||||
© 2025 TelinDigital Solution
|
||||
</Typography>
|
||||
</Box>
|
||||
</Grid>
|
||||
)
|
||||
}
|
||||
|
||||
export default LoginPage
|
||||
|
||||
@ -1,184 +1,144 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
TextField,
|
||||
Button,
|
||||
Box,
|
||||
Typography,
|
||||
Paper,
|
||||
FormControl,
|
||||
Select,
|
||||
InputLabel,
|
||||
MenuItem,
|
||||
FormControlLabel,
|
||||
Checkbox,
|
||||
FormLabel
|
||||
} from "@mui/material";
|
||||
import { fetchApi } from '../config/axios';
|
||||
import WarningDialog from '../components/WarningDialog';
|
||||
import Cookies from "js-cookie";
|
||||
import React, { useState, useEffect } from "react"
|
||||
import { TextField, Button, Box, Typography, Paper, FormControl, Select, InputLabel, MenuItem, FormControlLabel, Checkbox, FormLabel } from "@mui/material"
|
||||
import { fetchApi } from "../config/axios"
|
||||
import WarningDialog from "../components/WarningDialog"
|
||||
import Cookies from "js-cookie"
|
||||
|
||||
const ProfilePage = () => {
|
||||
// State for form fields
|
||||
const [formData, setFormData] = useState({
|
||||
username: '',
|
||||
name: '',
|
||||
email: '',
|
||||
status: '',
|
||||
idRole: '',
|
||||
password: '',
|
||||
oldPassword: '',
|
||||
confirmPassword: '',
|
||||
newPassword: ''
|
||||
// nik: '',
|
||||
// position: '',
|
||||
// telp: '',
|
||||
// address: '',
|
||||
// organisasi: '',
|
||||
// devisi: '',
|
||||
// unit: '',
|
||||
});
|
||||
const [roles, setRoles] = useState([]);
|
||||
const [isModalOpen, setIsModalOpen] = useState(false); // ERROR MSG
|
||||
const [error, setError] = useState(""); // ERROR MSG
|
||||
// State for form fields
|
||||
const [formData, setFormData] = useState({
|
||||
username: "",
|
||||
name: "",
|
||||
email: "",
|
||||
status: "",
|
||||
idRole: "",
|
||||
password: "",
|
||||
oldPassword: "",
|
||||
confirmPassword: "",
|
||||
newPassword: "",
|
||||
// nik: '',
|
||||
// position: '',
|
||||
// telp: '',
|
||||
// address: '',
|
||||
// organisasi: '',
|
||||
// devisi: '',
|
||||
// unit: '',
|
||||
})
|
||||
const [roles, setRoles] = useState([])
|
||||
const [isModalOpen, setIsModalOpen] = useState(false) // ERROR MSG
|
||||
const [error, setError] = useState("") // ERROR MSG
|
||||
|
||||
useEffect(() => {
|
||||
fetchUser()
|
||||
fetchUserRoles()
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
fetchUser()
|
||||
fetchUserRoles()
|
||||
// eslint-disable-next-line
|
||||
}, [])
|
||||
|
||||
async function fetchUser(params) {
|
||||
try {
|
||||
let userId = Cookies.get(`userId`);
|
||||
let token = Cookies.get(`token`);
|
||||
|
||||
let fetchUsers = await fetchApi(`/api/user/detail/${userId}`, 'GET', null, { token: token });
|
||||
// setFormData({})
|
||||
console.log(fetchUsers);
|
||||
setFormData(fetchUsers.data.data)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
async function fetchUser(params) {
|
||||
try {
|
||||
let userId = Cookies.get(`userId`)
|
||||
let token = Cookies.get(`token`)
|
||||
|
||||
async function fetchUserRoles(params) {
|
||||
try {
|
||||
let userRoles = await fetchApi('/api/user_role/list', 'GET', null, {
|
||||
limit: 10,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: 'id',
|
||||
order_direction: 'ASC'
|
||||
});
|
||||
console.log(userRoles);
|
||||
|
||||
if (userRoles.data && userRoles.data.data) setRoles(userRoles.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
let fetchUsers = await fetchApi(`d/user/detail/${userId}`, "GET", null, { token: token })
|
||||
// setFormData({})
|
||||
console.log(fetchUsers)
|
||||
setFormData(fetchUsers.data.data)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle form submission
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault();
|
||||
// if (name && email) {
|
||||
// // Handle registration logic, like calling an API to create the user
|
||||
// console.log("Registered with:", { name, email });
|
||||
// } else {
|
||||
// alert("Please fill in all fields.");
|
||||
// }
|
||||
};
|
||||
async function fetchUserRoles(params) {
|
||||
try {
|
||||
let userRoles = await fetchApi("d/user_role/list", "GET", null, {
|
||||
limit: 10,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: "id",
|
||||
order_direction: "ASC",
|
||||
})
|
||||
console.log(userRoles)
|
||||
|
||||
const handleChange = (e) => {
|
||||
let { name, value } = e.target;
|
||||
if (name === 'status' && value === "Y") value = "N"
|
||||
else if (name === 'status' && value === "N") value = "Y"
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
};
|
||||
if (userRoles.data && userRoles.data.data) setRoles(userRoles.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleError = (msg) => { // ERROR MSG
|
||||
setError(msg);
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
// Handle form submission
|
||||
const handleSubmit = (e) => {
|
||||
e.preventDefault()
|
||||
// if (name && email) {
|
||||
// // Handle registration logic, like calling an API to create the user
|
||||
// console.log("Registered with:", { name, email });
|
||||
// } else {
|
||||
// alert("Please fill in all fields.");
|
||||
// }
|
||||
}
|
||||
|
||||
const closeModal = () => { // ERROR MSG
|
||||
setIsModalOpen(false);
|
||||
setError("");
|
||||
};
|
||||
|
||||
async function updatePassword() {
|
||||
try {
|
||||
let updatePassword = await fetchApi(`/api/user/update_password`, 'PUT', {
|
||||
password: formData.password,
|
||||
retype_password: formData.confirmPassword
|
||||
});
|
||||
console.log(updatePassword);
|
||||
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
const handleChange = (e) => {
|
||||
let { name, value } = e.target
|
||||
if (name === "status" && value === "Y") value = "N"
|
||||
else if (name === "status" && value === "N") value = "Y"
|
||||
setFormData((prev) => ({ ...prev, [name]: value }))
|
||||
}
|
||||
|
||||
return (
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<WarningDialog // ERROR MSG
|
||||
open={isModalOpen}
|
||||
onClose={closeModal}
|
||||
errorMessage={error}
|
||||
/>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'flex-start', pr: 2, }}>
|
||||
<Typography sx={{ color: 'gray', fontSize: '30px' }}>User Profile</Typography>
|
||||
</Box>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Paper sx={{ width: '100%', overflow: 'hidden' }}>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Username"
|
||||
name="username"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
value={formData.username}
|
||||
onChange={handleChange}
|
||||
required
|
||||
disabled
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Name"
|
||||
name="name"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Email"
|
||||
name="email"
|
||||
variant="outlined"
|
||||
fullWidth
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
type="email"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2} sx={{ display: 'flex', justifyContent: 'flex-start' }}>
|
||||
<FormLabel component="legend">Status :
|
||||
<FormControlLabel required label="Aktif" control={
|
||||
<Checkbox value={formData.status} name="status" onClick={handleChange}
|
||||
checked={ formData.status === "Y" ? true : false } />
|
||||
}/>
|
||||
</FormLabel>
|
||||
</Box>
|
||||
{/* <Box mb={2} m={2}>
|
||||
const handleError = (msg) => {
|
||||
// ERROR MSG
|
||||
setError(msg)
|
||||
setIsModalOpen(true)
|
||||
}
|
||||
|
||||
const closeModal = () => {
|
||||
// ERROR MSG
|
||||
setIsModalOpen(false)
|
||||
setError("")
|
||||
}
|
||||
|
||||
async function updatePassword() {
|
||||
try {
|
||||
let updatePassword = await fetchApi(`d/user/update_password`, "PUT", {
|
||||
password: formData.password,
|
||||
retype_password: formData.confirmPassword,
|
||||
})
|
||||
console.log(updatePassword)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<WarningDialog // ERROR MSG
|
||||
open={isModalOpen}
|
||||
onClose={closeModal}
|
||||
errorMessage={error}
|
||||
/>
|
||||
<Box sx={{ display: "flex", justifyContent: "flex-start", pr: 2 }}>
|
||||
<Typography sx={{ color: "gray", fontSize: "30px" }}>User Profile</Typography>
|
||||
</Box>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Paper sx={{ width: "100%", overflow: "hidden" }}>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Username" name="username" variant="outlined" fullWidth value={formData.username} onChange={handleChange} required disabled />
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Name" name="name" variant="outlined" fullWidth value={formData.name} onChange={handleChange} required />
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Email" name="email" variant="outlined" fullWidth value={formData.email} onChange={handleChange} type="email" required />
|
||||
</Box>
|
||||
<Box mb={2} m={2} sx={{ display: "flex", justifyContent: "flex-start" }}>
|
||||
<FormLabel component="legend">
|
||||
Status :
|
||||
<FormControlLabel required label="Aktif" control={<Checkbox value={formData.status} name="status" onClick={handleChange} checked={formData.status === "Y" ? true : false} />} />
|
||||
</FormLabel>
|
||||
</Box>
|
||||
{/* <Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="NIK"
|
||||
variant="outlined"
|
||||
@ -269,94 +229,62 @@ const ProfilePage = () => {
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Box> */}
|
||||
<Box mb={2} m={2}>
|
||||
<FormControl fullWidth required>
|
||||
<InputLabel id="organization-label">Role</InputLabel>
|
||||
<Select
|
||||
labelId="organization-label"
|
||||
id="organization"
|
||||
value={formData.idRole}
|
||||
label="Role"
|
||||
onChange={handleChange}
|
||||
name='idRole'
|
||||
>
|
||||
{
|
||||
roles.map((el, idx) => (
|
||||
<MenuItem key={idx} value={el.id}>{el.name}</MenuItem>
|
||||
))
|
||||
}
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Password"
|
||||
name="password"
|
||||
value={formData.password}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
required
|
||||
disabled
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<FormControl fullWidth required>
|
||||
<InputLabel id="organization-label">Role</InputLabel>
|
||||
<Select labelId="organization-label" id="organization" value={formData.idRole} label="Role" onChange={handleChange} name="idRole">
|
||||
{roles.map((el, idx) => (
|
||||
<MenuItem key={idx} value={el.id}>
|
||||
{el.name}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Password" name="password" value={formData.password} onChange={handleChange} fullWidth type="password" required disabled />
|
||||
</Box>
|
||||
|
||||
<Box mt={2} mb={2} textAlign="center">
|
||||
<Button style={{ marginRight: '5px' }} variant="contained">Cancel</Button>
|
||||
<Button variant="contained" color="success">Submit</Button>
|
||||
</Box>
|
||||
</form>
|
||||
</Paper>
|
||||
</Box>
|
||||
</Box>
|
||||
<Box mt={2} mb={2} textAlign="center">
|
||||
<Button style={{ marginRight: "5px" }} variant="contained">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant="contained" color="success">
|
||||
Submit
|
||||
</Button>
|
||||
</Box>
|
||||
</form>
|
||||
</Paper>
|
||||
</Box>
|
||||
</Box>
|
||||
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Typography sx={{ color: 'gray', fontSize: '30px' }}>User Password</Typography>
|
||||
<Paper sx={{ width: '100%', overflow: 'hidden' }}>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Old Password"
|
||||
name="oldPassword"
|
||||
value={formData.oldPassword}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="New Password"
|
||||
name="newPassword"
|
||||
value={formData.newPassword}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField
|
||||
label="Confirm Password"
|
||||
name="confirmPassword"
|
||||
value={formData.confirmPassword}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
</Box>
|
||||
|
||||
<Box mt={2} mb={2} textAlign="center">
|
||||
<Button style={{ marginRight: '5px' }} variant="contained">Cancel</Button>
|
||||
<Button variant="contained" onClick={() => updatePassword} color="success">Update</Button>
|
||||
</Box>
|
||||
</Paper>
|
||||
</Box>
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
};
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<Typography sx={{ color: "gray", fontSize: "30px" }}>User Password</Typography>
|
||||
<Paper sx={{ width: "100%", overflow: "hidden" }}>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Old Password" name="oldPassword" value={formData.oldPassword} onChange={handleChange} fullWidth type="password" required />
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="New Password" name="newPassword" value={formData.newPassword} onChange={handleChange} fullWidth type="password" required />
|
||||
</Box>
|
||||
<Box mb={2} m={2}>
|
||||
<TextField label="Confirm Password" name="confirmPassword" value={formData.confirmPassword} onChange={handleChange} fullWidth type="password" required />
|
||||
</Box>
|
||||
|
||||
export default ProfilePage;
|
||||
<Box mt={2} mb={2} textAlign="center">
|
||||
<Button style={{ marginRight: "5px" }} variant="contained">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button variant="contained" onClick={() => updatePassword} color="success">
|
||||
Update
|
||||
</Button>
|
||||
</Box>
|
||||
</Paper>
|
||||
</Box>
|
||||
</Box>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
export default ProfilePage
|
||||
|
||||
@ -1,26 +1,7 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Container,
|
||||
Typography,
|
||||
Button,
|
||||
Dialog,
|
||||
DialogTitle,
|
||||
DialogContent,
|
||||
DialogActions,
|
||||
TextField,
|
||||
IconButton,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableContainer,
|
||||
TableHead,
|
||||
TableRow,
|
||||
Checkbox,
|
||||
Paper,
|
||||
Box
|
||||
} from '@mui/material';
|
||||
import AddIcon from '@mui/icons-material/Add';
|
||||
import { fetchApi } from '../config/axios';
|
||||
import React, { useState, useEffect } from "react"
|
||||
import { Container, Typography, Button, Dialog, DialogTitle, DialogContent, DialogActions, TextField, IconButton, Table, TableBody, TableCell, TableContainer, TableHead, TableRow, Checkbox, Paper, Box } from "@mui/material"
|
||||
import AddIcon from "@mui/icons-material/Add"
|
||||
import { fetchApi } from "../config/axios"
|
||||
|
||||
// Initial list of roles and menus
|
||||
// const initialRoles = ['Admin', 'Escrow', 'Agent'];
|
||||
@ -38,203 +19,200 @@ import { fetchApi } from '../config/axios';
|
||||
// ];
|
||||
|
||||
const RoleMenuTable = () => {
|
||||
const [roles, setRoles] = useState([]);
|
||||
// const [roleMenus, setRoleMenus] = useState([]);
|
||||
const [menus, setMenus] = useState([]);
|
||||
const [openDialog, setOpenDialog] = useState(false);
|
||||
const [newRole, setNewRole] = useState('');
|
||||
const [confirmDialog, setConfirmDialog] = useState({
|
||||
open: false, roleName: '', menuData: [], roleId: '', menuId: '', status: null, menuName: ''
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
fetchData()
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
const [roles, setRoles] = useState([])
|
||||
// const [roleMenus, setRoleMenus] = useState([]);
|
||||
const [menus, setMenus] = useState([])
|
||||
const [openDialog, setOpenDialog] = useState(false)
|
||||
const [newRole, setNewRole] = useState("")
|
||||
const [confirmDialog, setConfirmDialog] = useState({
|
||||
open: false,
|
||||
roleName: "",
|
||||
menuData: [],
|
||||
roleId: "",
|
||||
menuId: "",
|
||||
status: null,
|
||||
menuName: "",
|
||||
})
|
||||
|
||||
async function fetchData(params) {
|
||||
try {
|
||||
let userRole = await fetchApi('/api/user_role/list', 'GET', null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: 'id',
|
||||
order_direction: 'ASC'
|
||||
});
|
||||
let userMenu = await fetchApi('/api/menus/list', 'GET', null, {
|
||||
limit: 50,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: 'id',
|
||||
order_direction: 'ASC'
|
||||
});
|
||||
setMenus(userMenu.data.data.list)
|
||||
|
||||
console.log('role',userRole);
|
||||
console.log('menu',userMenu);
|
||||
if (userRole.data && userRole.data.data) setRoles(userRole.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
useEffect(() => {
|
||||
fetchData()
|
||||
// eslint-disable-next-line
|
||||
}, [])
|
||||
|
||||
const handleCheckboxChange = (roleName, menuData, roleId, menuId, status, menuName) => {
|
||||
setConfirmDialog({ open: true, roleName, menuData, roleId, menuId, status, menuName });
|
||||
};
|
||||
async function fetchData(params) {
|
||||
try {
|
||||
let userRole = await fetchApi("d/user_role/list", "GET", null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: "id",
|
||||
order_direction: "ASC",
|
||||
})
|
||||
let userMenu = await fetchApi("/api/menus/list", "GET", null, {
|
||||
limit: 50,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: "id",
|
||||
order_direction: "ASC",
|
||||
})
|
||||
setMenus(userMenu.data.data.list)
|
||||
|
||||
const handleConfirm = async (confirm) => {
|
||||
const { roleName, menuData, roleId, menuId, status } = confirmDialog;
|
||||
try {
|
||||
if (confirm) {
|
||||
console.log(confirm,confirmDialog);
|
||||
if (status) {
|
||||
let generateRoles = menuData.filter(el => +el !== +menuId);
|
||||
console.log(generateRoles);
|
||||
let userMenu = await fetchApi(`/api/user_role/update/${roleId}`, 'PUT', {
|
||||
name: roleName,
|
||||
roles: generateRoles,
|
||||
status: "Y"
|
||||
})
|
||||
console.log(userMenu);
|
||||
await fetchData()
|
||||
} else {
|
||||
let generateRoles = menuData.filter(el => +el !== +menuId);
|
||||
generateRoles.push(`${menuId}`)
|
||||
console.log(generateRoles);
|
||||
let userMenu = await fetchApi(`/api/user_role/update/${roleId}`, 'PUT', {
|
||||
name: roleName,
|
||||
roles: generateRoles,
|
||||
status: "Y"
|
||||
})
|
||||
console.log(userMenu);
|
||||
await fetchData()
|
||||
}
|
||||
}
|
||||
setConfirmDialog({ open: false, role: '', menu: '' });
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
};
|
||||
console.log("role", userRole)
|
||||
console.log("menu", userMenu)
|
||||
if (userRole.data && userRole.data.data) setRoles(userRole.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
const handleAddRole = async () => {
|
||||
try {
|
||||
await fetchApi('/api/user_role/create', 'POST', {
|
||||
"name": newRole,
|
||||
"roles": [],
|
||||
"status": "Y"
|
||||
});
|
||||
await fetchData();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
setNewRole('');
|
||||
setOpenDialog(false);
|
||||
};
|
||||
const handleCheckboxChange = (roleName, menuData, roleId, menuId, status, menuName) => {
|
||||
setConfirmDialog({ open: true, roleName, menuData, roleId, menuId, status, menuName })
|
||||
}
|
||||
|
||||
const deleteRole = async (role) => {
|
||||
try {
|
||||
let resDeleteRole = await fetchApi(`/api/user_role/delete/${role.id}/true`, 'DELETE');
|
||||
console.log(resDeleteRole);
|
||||
await fetchData();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
const handleConfirm = async (confirm) => {
|
||||
const { roleName, menuData, roleId, menuId, status } = confirmDialog
|
||||
try {
|
||||
if (confirm) {
|
||||
console.log(confirm, confirmDialog)
|
||||
if (status) {
|
||||
let generateRoles = menuData.filter((el) => +el !== +menuId)
|
||||
console.log(generateRoles)
|
||||
let userMenu = await fetchApi(`d/user_role/update/${roleId}`, "PUT", {
|
||||
name: roleName,
|
||||
roles: generateRoles,
|
||||
status: "Y",
|
||||
})
|
||||
console.log(userMenu)
|
||||
await fetchData()
|
||||
} else {
|
||||
let generateRoles = menuData.filter((el) => +el !== +menuId)
|
||||
generateRoles.push(`${menuId}`)
|
||||
console.log(generateRoles)
|
||||
let userMenu = await fetchApi(`d/user_role/update/${roleId}`, "PUT", {
|
||||
name: roleName,
|
||||
roles: generateRoles,
|
||||
status: "Y",
|
||||
})
|
||||
console.log(userMenu)
|
||||
await fetchData()
|
||||
}
|
||||
}
|
||||
setConfirmDialog({ open: false, role: "", menu: "" })
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Container>
|
||||
<Typography variant="h4" align="center" gutterBottom marginTop={5}>
|
||||
Role Menu Management
|
||||
</Typography>
|
||||
<IconButton
|
||||
style={{ position: 'absolute', top: 16, right: 16 }}
|
||||
color="primary"
|
||||
onClick={() => setOpenDialog(true)}
|
||||
>
|
||||
<AddIcon />
|
||||
</IconButton>
|
||||
const handleAddRole = async () => {
|
||||
try {
|
||||
await fetchApi("d/user_role/create", "POST", {
|
||||
name: newRole,
|
||||
roles: [],
|
||||
status: "Y",
|
||||
})
|
||||
await fetchData()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
setNewRole("")
|
||||
setOpenDialog(false)
|
||||
}
|
||||
|
||||
<Box sx={{ display: 'flex', justifyContent: 'flex-end', alignItems: 'center', pr: 2, }}>
|
||||
<Button variant="contained" color="primary" onClick={() => setOpenDialog(true)}>Create Role</Button>
|
||||
</Box>
|
||||
const deleteRole = async (role) => {
|
||||
try {
|
||||
let resDeleteRole = await fetchApi(`d/user_role/delete/${role.id}/true`, "DELETE")
|
||||
console.log(resDeleteRole)
|
||||
await fetchData()
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
|
||||
{/* Table for roles and menus */}
|
||||
<TableContainer component={Paper} style={{ marginTop: '20px' }}>
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<TableCell><strong>Role</strong></TableCell>
|
||||
{menus.map((menu, idx) => (
|
||||
<TableCell key={idx} align="center"><strong>{menu.name}</strong></TableCell>
|
||||
))}
|
||||
<TableCell><strong>Action</strong></TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{roles.map((role) => (
|
||||
<TableRow key={role.id}>
|
||||
<TableCell>{role.name}</TableCell>
|
||||
{menus.map((menu, idx) => (
|
||||
<TableCell key={idx} align="center">
|
||||
<Checkbox
|
||||
checked={(role.roles.find(el => +el === menu.id)) ? true : false}
|
||||
onChange={() => handleCheckboxChange(
|
||||
role.name, role.roles, role.id, menu.id, (role.roles.find(el => +el === menu.id)) ? true : false, menu.name
|
||||
)}
|
||||
/>
|
||||
</TableCell>
|
||||
))}
|
||||
<TableCell align="center">
|
||||
<Button onClick={() => deleteRole(role)}>Delete</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
return (
|
||||
<Container>
|
||||
<Typography variant="h4" align="center" gutterBottom marginTop={5}>
|
||||
Role Menu Management
|
||||
</Typography>
|
||||
<IconButton style={{ position: "absolute", top: 16, right: 16 }} color="primary" onClick={() => setOpenDialog(true)}>
|
||||
<AddIcon />
|
||||
</IconButton>
|
||||
|
||||
{/* Dialog for adding new role */}
|
||||
<Dialog open={openDialog} onClose={() => setOpenDialog(false)}>
|
||||
<DialogTitle>Add New Role</DialogTitle>
|
||||
<DialogContent>
|
||||
<TextField
|
||||
autoFocus
|
||||
margin="dense"
|
||||
label="Role Name"
|
||||
type="text"
|
||||
fullWidth
|
||||
value={newRole}
|
||||
onChange={(e) => setNewRole(e.target.value)}
|
||||
/>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => setOpenDialog(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleAddRole} color="primary">
|
||||
Add
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
<Box sx={{ display: "flex", justifyContent: "flex-end", alignItems: "center", pr: 2 }}>
|
||||
<Button variant="contained" color="primary" onClick={() => setOpenDialog(true)}>
|
||||
Create Role
|
||||
</Button>
|
||||
</Box>
|
||||
|
||||
{/* Confirmation Dialog for checkbox */}
|
||||
<Dialog open={confirmDialog.open} onClose={() => handleConfirm(false)}>
|
||||
<DialogTitle>Confirm Menu Selection</DialogTitle>
|
||||
<DialogContent>
|
||||
Are you sure you want to <strong>{confirmDialog.status ? 'remove ' : 'add '}</strong>
|
||||
the menu <strong>{confirmDialog.menuName}</strong> for role <strong>{confirmDialog.roleName}</strong>?
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => handleConfirm(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={() => handleConfirm(true)} color="primary">
|
||||
Confirm
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</Container>
|
||||
);
|
||||
};
|
||||
{/* Table for roles and menus */}
|
||||
<TableContainer component={Paper} style={{ marginTop: "20px" }}>
|
||||
<Table>
|
||||
<TableHead>
|
||||
<TableRow>
|
||||
<TableCell>
|
||||
<strong>Role</strong>
|
||||
</TableCell>
|
||||
{menus.map((menu, idx) => (
|
||||
<TableCell key={idx} align="center">
|
||||
<strong>{menu.name}</strong>
|
||||
</TableCell>
|
||||
))}
|
||||
<TableCell>
|
||||
<strong>Action</strong>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
</TableHead>
|
||||
<TableBody>
|
||||
{roles.map((role) => (
|
||||
<TableRow key={role.id}>
|
||||
<TableCell>{role.name}</TableCell>
|
||||
{menus.map((menu, idx) => (
|
||||
<TableCell key={idx} align="center">
|
||||
<Checkbox checked={role.roles.find((el) => +el === menu.id) ? true : false} onChange={() => handleCheckboxChange(role.name, role.roles, role.id, menu.id, role.roles.find((el) => +el === menu.id) ? true : false, menu.name)} />
|
||||
</TableCell>
|
||||
))}
|
||||
<TableCell align="center">
|
||||
<Button onClick={() => deleteRole(role)}>Delete</Button>
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</TableContainer>
|
||||
|
||||
export default RoleMenuTable;
|
||||
{/* Dialog for adding new role */}
|
||||
<Dialog open={openDialog} onClose={() => setOpenDialog(false)}>
|
||||
<DialogTitle>Add New Role</DialogTitle>
|
||||
<DialogContent>
|
||||
<TextField autoFocus margin="dense" label="Role Name" type="text" fullWidth value={newRole} onChange={(e) => setNewRole(e.target.value)} />
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => setOpenDialog(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleAddRole} color="primary">
|
||||
Add
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
|
||||
{/* Confirmation Dialog for checkbox */}
|
||||
<Dialog open={confirmDialog.open} onClose={() => handleConfirm(false)}>
|
||||
<DialogTitle>Confirm Menu Selection</DialogTitle>
|
||||
<DialogContent>
|
||||
Are you sure you want to <strong>{confirmDialog.status ? "remove " : "add "}</strong>
|
||||
the menu <strong>{confirmDialog.menuName}</strong> for role <strong>{confirmDialog.roleName}</strong>?
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => handleConfirm(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={() => handleConfirm(true)} color="primary">
|
||||
Confirm
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export default RoleMenuTable
|
||||
|
||||
@ -1,287 +1,247 @@
|
||||
// Import React and Material-UI components
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Paper, Button, Dialog, DialogActions, DialogContent, DialogTitle, TableContainer, FormLabel,
|
||||
Checkbox, FormControlLabel, Box, Typography, TextField, Grid, MenuItem, InputLabel, Select, FormControl
|
||||
} from '@mui/material';
|
||||
import ReusableTable from '../components/Table';
|
||||
import { fetchApi } from '../config/axios';
|
||||
import WarningDialog from '../components/WarningDialog'; // ERROR MSG
|
||||
import React, { useState, useEffect } from "react"
|
||||
import { Paper, Button, Dialog, DialogActions, DialogContent, DialogTitle, TableContainer, FormLabel, Checkbox, FormControlLabel, Box, Typography, TextField, Grid, MenuItem, InputLabel, Select, FormControl } from "@mui/material"
|
||||
import ReusableTable from "../components/Table"
|
||||
import { fetchApi } from "../config/axios"
|
||||
import WarningDialog from "../components/WarningDialog" // ERROR MSG
|
||||
|
||||
const columns = [
|
||||
{ id: 'username', label: 'Username' },
|
||||
{ id: 'name', label: 'Name' },
|
||||
{ id: 'email', label: 'Email' },
|
||||
{ id: 'role_name', label: 'Roles' },
|
||||
{ id: 'status', label: 'Status' },
|
||||
{ id: 'created_at', label: 'Created' },
|
||||
{ id: 'actions', label: 'Actions' },
|
||||
];
|
||||
{ id: "username", label: "Username" },
|
||||
{ id: "name", label: "Name" },
|
||||
{ id: "email", label: "Email" },
|
||||
{ id: "role_name", label: "Roles" },
|
||||
{ id: "status", label: "Status" },
|
||||
{ id: "created_at", label: "Created" },
|
||||
{ id: "actions", label: "Actions" },
|
||||
]
|
||||
|
||||
const initiateForm = {
|
||||
username: '',
|
||||
name: '',
|
||||
email: '',
|
||||
status: '',
|
||||
id_role: '',
|
||||
password: '',
|
||||
confirmPassword: '',
|
||||
// nik: '',
|
||||
// position: '',
|
||||
// telp: '',
|
||||
// address: '',
|
||||
// organisasi: '',
|
||||
// devisi: '',
|
||||
// unit: '',
|
||||
username: "",
|
||||
name: "",
|
||||
email: "",
|
||||
status: "",
|
||||
id_role: "",
|
||||
password: "",
|
||||
confirmPassword: "",
|
||||
// nik: '',
|
||||
// position: '',
|
||||
// telp: '',
|
||||
// address: '',
|
||||
// organisasi: '',
|
||||
// devisi: '',
|
||||
// unit: '',
|
||||
}
|
||||
|
||||
export default function UserManagement() {
|
||||
const [users, setUsers] = useState([]);
|
||||
const [open, setOpen] = useState(false);
|
||||
const [selectedUser, setSelectedUser] = useState(null);
|
||||
const [selectedRoles, setSelectedRoles] = useState([]);
|
||||
const [roles, setRoles] = useState([]);
|
||||
const [openDialog, setOpenDialog] = useState(false);
|
||||
const [labelDialog, setLabelDialog] = useState('');
|
||||
const [formData, setFormData] = useState(initiateForm);
|
||||
const [errors, setErrors] = useState({ passwordMatch: false, });
|
||||
const [isModalOpen, setIsModalOpen] = useState(false); // ERROR MSG
|
||||
const [error, setError] = useState(""); // ERROR MSG
|
||||
const [users, setUsers] = useState([])
|
||||
const [open, setOpen] = useState(false)
|
||||
const [selectedUser, setSelectedUser] = useState(null)
|
||||
const [selectedRoles, setSelectedRoles] = useState([])
|
||||
const [roles, setRoles] = useState([])
|
||||
const [openDialog, setOpenDialog] = useState(false)
|
||||
const [labelDialog, setLabelDialog] = useState("")
|
||||
const [formData, setFormData] = useState(initiateForm)
|
||||
const [errors, setErrors] = useState({ passwordMatch: false })
|
||||
const [isModalOpen, setIsModalOpen] = useState(false) // ERROR MSG
|
||||
const [error, setError] = useState("") // ERROR MSG
|
||||
|
||||
useEffect(() => {
|
||||
fetchUserRoles()
|
||||
fetchUser()
|
||||
// eslint-disable-next-line
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
fetchUserRoles()
|
||||
fetchUser()
|
||||
// eslint-disable-next-line
|
||||
}, [])
|
||||
|
||||
async function fetchUser(params) {
|
||||
try {
|
||||
let fetchUsers = await fetchApi('/api/user/list', 'GET', null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: 'id',
|
||||
order_direction: 'ASC'
|
||||
});
|
||||
console.log(fetchUsers);
|
||||
if (fetchUsers.data && fetchUsers.data.data) {
|
||||
let resFetchUsers = fetchUsers.data.data.list
|
||||
let temp = resFetchUsers.map(el => {
|
||||
if (el.status === 'Y') el.status = 'Aktif'
|
||||
else el.status ='Tidak Aktif'
|
||||
el.actions = [
|
||||
{ label: 'Edit', color: 'info', onClick: (row) => handleOpen(row) },
|
||||
{ label: 'Delete', color: 'secondary', onClick: (row) => handleDelete(row) },
|
||||
]
|
||||
return el
|
||||
})
|
||||
setUsers(temp)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
async function fetchUser(params) {
|
||||
try {
|
||||
let fetchUsers = await fetchApi("d/user/list", "GET", null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: "id",
|
||||
order_direction: "ASC",
|
||||
})
|
||||
console.log(fetchUsers)
|
||||
if (fetchUsers.data && fetchUsers.data.data) {
|
||||
let resFetchUsers = fetchUsers.data.data.list
|
||||
let temp = resFetchUsers.map((el) => {
|
||||
if (el.status === "Y") el.status = "Aktif"
|
||||
else el.status = "Tidak Aktif"
|
||||
el.actions = [
|
||||
{ label: "Edit", color: "info", onClick: (row) => handleOpen(row) },
|
||||
{ label: "Delete", color: "secondary", onClick: (row) => handleDelete(row) },
|
||||
]
|
||||
return el
|
||||
})
|
||||
setUsers(temp)
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchUserRoles(params) {
|
||||
try {
|
||||
let userRoles = await fetchApi('/api/user_role/list', 'GET', null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: 'id',
|
||||
order_direction: 'ASC'
|
||||
});
|
||||
if (userRoles.data && userRoles.data.data) setRoles(userRoles.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
async function fetchUserRoles(params) {
|
||||
try {
|
||||
let userRoles = await fetchApi("d/user_role/list", "GET", null, {
|
||||
limit: 5,
|
||||
page: 1,
|
||||
with_deleted: true,
|
||||
order_field: "id",
|
||||
order_direction: "ASC",
|
||||
})
|
||||
if (userRoles.data && userRoles.data.data) setRoles(userRoles.data.data.list)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
const handleOpen = (user) => {
|
||||
setLabelDialog(`Edit User`)
|
||||
setFormData(user)
|
||||
setOpenDialog(true)
|
||||
// setSelectedUser(user);
|
||||
// setSelectedRoles(user.role_name);
|
||||
// setOpen(true);
|
||||
};
|
||||
const handleOpen = (user) => {
|
||||
setLabelDialog(`Edit User`)
|
||||
setFormData(user)
|
||||
setOpenDialog(true)
|
||||
// setSelectedUser(user);
|
||||
// setSelectedRoles(user.role_name);
|
||||
// setOpen(true);
|
||||
}
|
||||
|
||||
const handleClose = () => {
|
||||
setOpen(false);
|
||||
setSelectedUser(null);
|
||||
setSelectedRoles([]);
|
||||
};
|
||||
const handleClose = () => {
|
||||
setOpen(false)
|
||||
setSelectedUser(null)
|
||||
setSelectedRoles([])
|
||||
}
|
||||
|
||||
const handleRoleChange = (role) => {
|
||||
setSelectedRoles((prevRoles) =>
|
||||
prevRoles.includes(role)
|
||||
? prevRoles.filter((r) => r !== role)
|
||||
: [...prevRoles, role]
|
||||
);
|
||||
};
|
||||
const handleRoleChange = (role) => {
|
||||
setSelectedRoles((prevRoles) => (prevRoles.includes(role) ? prevRoles.filter((r) => r !== role) : [...prevRoles, role]))
|
||||
}
|
||||
|
||||
const handleDelete = (row) => {
|
||||
const newData = users.filter((item) => item.id !== row.id);
|
||||
setUsers(newData);
|
||||
alert(`Deleted ${row.name}`);
|
||||
};
|
||||
const handleDelete = (row) => {
|
||||
const newData = users.filter((item) => item.id !== row.id)
|
||||
setUsers(newData)
|
||||
alert(`Deleted ${row.name}`)
|
||||
}
|
||||
|
||||
const handleSave = () => {
|
||||
setUsers((prevUsers) =>
|
||||
prevUsers.map((user) =>
|
||||
user.id === selectedUser.id ? { ...user, roles: selectedRoles } : user
|
||||
)
|
||||
);
|
||||
handleClose();
|
||||
};
|
||||
const handleSave = () => {
|
||||
setUsers((prevUsers) => prevUsers.map((user) => (user.id === selectedUser.id ? { ...user, roles: selectedRoles } : user)))
|
||||
handleClose()
|
||||
}
|
||||
|
||||
const handleChange = (e) => {
|
||||
const { name, value } = e.target;
|
||||
setFormData((prev) => ({ ...prev, [name]: value }));
|
||||
const handleChange = (e) => {
|
||||
const { name, value } = e.target
|
||||
setFormData((prev) => ({ ...prev, [name]: value }))
|
||||
|
||||
if (name === 'confirmPassword' || name === 'password') {
|
||||
setErrors((prev) => ({
|
||||
...prev,
|
||||
passwordMatch: name === 'confirmPassword' ? value !== formData.password : formData.confirmPassword !== value,
|
||||
}));
|
||||
}
|
||||
};
|
||||
if (name === "confirmPassword" || name === "password") {
|
||||
setErrors((prev) => ({
|
||||
...prev,
|
||||
passwordMatch: name === "confirmPassword" ? value !== formData.password : formData.confirmPassword !== value,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
if (!errors.passwordMatch) {
|
||||
console.log('Form Data:', formData);
|
||||
// setOpen(false);
|
||||
// setFormData(initiateForm);
|
||||
}
|
||||
console.log(formData);
|
||||
formData.status = "Y";
|
||||
if (labelDialog === `Create User`) {
|
||||
delete formData.confirmPassword
|
||||
let createUser = await fetchApi('/api/user/create', 'POST', formData);
|
||||
console.log(createUser);
|
||||
} else if (labelDialog === `Edit User`) {
|
||||
delete formData.created_at
|
||||
delete formData.updated_at
|
||||
delete formData.role_name
|
||||
delete formData.actions
|
||||
delete formData.confirmPassword
|
||||
console.log(formData);
|
||||
let editUser = await fetchApi(`/api/user/update/${formData.id}`, 'PUT', formData);
|
||||
console.log('editUser', editUser);
|
||||
}
|
||||
await fetchUser()
|
||||
setOpenDialog(false)
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
const handleSubmit = async () => {
|
||||
try {
|
||||
if (!errors.passwordMatch) {
|
||||
console.log("Form Data:", formData)
|
||||
// setOpen(false);
|
||||
// setFormData(initiateForm);
|
||||
}
|
||||
console.log(formData)
|
||||
formData.status = "Y"
|
||||
if (labelDialog === `Create User`) {
|
||||
delete formData.confirmPassword
|
||||
let createUser = await fetchApi("d/user/create", "POST", formData)
|
||||
console.log(createUser)
|
||||
} else if (labelDialog === `Edit User`) {
|
||||
delete formData.created_at
|
||||
delete formData.updated_at
|
||||
delete formData.role_name
|
||||
delete formData.actions
|
||||
delete formData.confirmPassword
|
||||
console.log(formData)
|
||||
let editUser = await fetchApi(`d/user/update/${formData.id}`, "PUT", formData)
|
||||
console.log("editUser", editUser)
|
||||
}
|
||||
await fetchUser()
|
||||
setOpenDialog(false)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
handleError(`${error.message}. \n ${error.response.data.error}`)
|
||||
}
|
||||
}
|
||||
|
||||
const createNewUserBtn = () => {
|
||||
setLabelDialog(`Create User`);
|
||||
setFormData(initiateForm);
|
||||
setOpenDialog(true);
|
||||
}
|
||||
const createNewUserBtn = () => {
|
||||
setLabelDialog(`Create User`)
|
||||
setFormData(initiateForm)
|
||||
setOpenDialog(true)
|
||||
}
|
||||
|
||||
const handleError = (msg) => { // ERROR MSG
|
||||
setError(msg);
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
|
||||
const closeModal = () => { // ERROR MSG
|
||||
setIsModalOpen(false);
|
||||
setError("");
|
||||
};
|
||||
const handleError = (msg) => {
|
||||
// ERROR MSG
|
||||
setError(msg)
|
||||
setIsModalOpen(true)
|
||||
}
|
||||
|
||||
return (
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<WarningDialog // ERROR MSG
|
||||
open={isModalOpen}
|
||||
onClose={closeModal}
|
||||
errorMessage={error}
|
||||
/>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'flex-start', pr: 2, }}>
|
||||
<Typography sx={{ color: 'gray', fontSize: '30px' }}>User Management</Typography>
|
||||
</Box>
|
||||
<Box p={3}>
|
||||
<Box sx={{ display: 'flex', justifyContent: 'flex-end', alignItems: 'center', pr: 2, }}>
|
||||
<Button variant="contained" color="primary" onClick={() => createNewUserBtn()}>Create New User</Button>
|
||||
</Box>
|
||||
<TableContainer component={Paper} sx={{ marginTop: 3 }}>
|
||||
<ReusableTable columns={columns} data={users} />
|
||||
const closeModal = () => {
|
||||
// ERROR MSG
|
||||
setIsModalOpen(false)
|
||||
setError("")
|
||||
}
|
||||
|
||||
<Dialog open={open} onClose={handleClose}>
|
||||
<DialogTitle>Add Roles to {selectedUser?.name}</DialogTitle>
|
||||
<DialogContent>
|
||||
{roles.map((role) => (
|
||||
<FormControlLabel
|
||||
key={role.id}
|
||||
control={
|
||||
<Checkbox
|
||||
checked={selectedRoles.includes(role.name)}
|
||||
onChange={() => handleRoleChange(role.name)}
|
||||
/>
|
||||
}
|
||||
label={role.name}
|
||||
/>
|
||||
))}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={handleClose} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} color="primary" variant="contained">
|
||||
Save
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</TableContainer>
|
||||
return (
|
||||
<Box sx={{ padding: 3 }}>
|
||||
<WarningDialog // ERROR MSG
|
||||
open={isModalOpen}
|
||||
onClose={closeModal}
|
||||
errorMessage={error}
|
||||
/>
|
||||
<Box sx={{ display: "flex", justifyContent: "flex-start", pr: 2 }}>
|
||||
<Typography sx={{ color: "gray", fontSize: "30px" }}>User Management</Typography>
|
||||
</Box>
|
||||
<Box p={3}>
|
||||
<Box sx={{ display: "flex", justifyContent: "flex-end", alignItems: "center", pr: 2 }}>
|
||||
<Button variant="contained" color="primary" onClick={() => createNewUserBtn()}>
|
||||
Create New User
|
||||
</Button>
|
||||
</Box>
|
||||
<TableContainer component={Paper} sx={{ marginTop: 3 }}>
|
||||
<ReusableTable columns={columns} data={users} />
|
||||
|
||||
<Dialog open={openDialog} onClose={() => setOpenDialog(false)} maxWidth="sm" fullWidth>
|
||||
<DialogTitle>{labelDialog}</DialogTitle>
|
||||
<DialogContent sx={{ marginTop: 2 }}>
|
||||
<Grid container spacing={2}>
|
||||
<Grid item xs={12}>
|
||||
<TextField
|
||||
sx={{ marginTop: 2 }}
|
||||
label="Username"
|
||||
name="username"
|
||||
value={formData.username}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
required
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField
|
||||
sx={{ marginTop: 2 }}
|
||||
label="Name"
|
||||
name="name"
|
||||
value={formData.name}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
required
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField
|
||||
label="Email"
|
||||
name="email"
|
||||
value={formData.email}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="email"
|
||||
required
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<FormLabel component="legend">Status</FormLabel>
|
||||
<FormControlLabel control={<Checkbox name='status' defaultChecked onChange={handleChange} value={formData.status === 'Y' ? true : false}/>} label="Aktif" />
|
||||
</Grid>
|
||||
{/* <Grid item xs={12}>
|
||||
<Dialog open={open} onClose={handleClose}>
|
||||
<DialogTitle>Add Roles to {selectedUser?.name}</DialogTitle>
|
||||
<DialogContent>
|
||||
{roles.map((role) => (
|
||||
<FormControlLabel key={role.id} control={<Checkbox checked={selectedRoles.includes(role.name)} onChange={() => handleRoleChange(role.name)} />} label={role.name} />
|
||||
))}
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={handleClose} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSave} color="primary" variant="contained">
|
||||
Save
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</TableContainer>
|
||||
|
||||
<Dialog open={openDialog} onClose={() => setOpenDialog(false)} maxWidth="sm" fullWidth>
|
||||
<DialogTitle>{labelDialog}</DialogTitle>
|
||||
<DialogContent sx={{ marginTop: 2 }}>
|
||||
<Grid container spacing={2}>
|
||||
<Grid item xs={12}>
|
||||
<TextField sx={{ marginTop: 2 }} label="Username" name="username" value={formData.username} onChange={handleChange} fullWidth required />
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField sx={{ marginTop: 2 }} label="Name" name="name" value={formData.name} onChange={handleChange} fullWidth required />
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField label="Email" name="email" value={formData.email} onChange={handleChange} fullWidth type="email" required />
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<FormLabel component="legend">Status</FormLabel>
|
||||
<FormControlLabel control={<Checkbox name="status" defaultChecked onChange={handleChange} value={formData.status === "Y" ? true : false} />} label="Aktif" />
|
||||
</Grid>
|
||||
{/* <Grid item xs={12}>
|
||||
<TextField
|
||||
sx={{ marginTop: 2 }}
|
||||
label="Nik"
|
||||
@ -377,68 +337,36 @@ export default function UserManagement() {
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Grid> */}
|
||||
<Grid item xs={12}>
|
||||
<FormControl fullWidth required>
|
||||
<InputLabel id="organization-label">Role</InputLabel>
|
||||
<Select
|
||||
labelId="organization-label"
|
||||
id="organization"
|
||||
value={formData.id_role}
|
||||
label="Role"
|
||||
onChange={handleChange}
|
||||
name='id_role'
|
||||
>
|
||||
{
|
||||
roles.map((el, idx) => (
|
||||
<MenuItem key={idx} value={el.id}>{el.name}</MenuItem>
|
||||
))
|
||||
}
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField
|
||||
label="Password"
|
||||
name="password"
|
||||
value={formData.password}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
required
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField
|
||||
label="Confirm Password"
|
||||
name="confirmPassword"
|
||||
value={formData.confirmPassword}
|
||||
onChange={handleChange}
|
||||
fullWidth
|
||||
type="password"
|
||||
error={errors.passwordMatch}
|
||||
helperText={errors.passwordMatch ? "Passwords do not match" : ""}
|
||||
required
|
||||
/>
|
||||
</Grid>
|
||||
</Grid>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => setOpenDialog(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={handleSubmit}
|
||||
color="primary"
|
||||
disabled={!formData.name || !formData.email || !formData.password || errors.passwordMatch || !formData.id_role
|
||||
|| !formData.status || !formData.username
|
||||
}
|
||||
>
|
||||
Submit
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
<Grid item xs={12}>
|
||||
<FormControl fullWidth required>
|
||||
<InputLabel id="organization-label">Role</InputLabel>
|
||||
<Select labelId="organization-label" id="organization" value={formData.id_role} label="Role" onChange={handleChange} name="id_role">
|
||||
{roles.map((el, idx) => (
|
||||
<MenuItem key={idx} value={el.id}>
|
||||
{el.name}
|
||||
</MenuItem>
|
||||
))}
|
||||
</Select>
|
||||
</FormControl>
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField label="Password" name="password" value={formData.password} onChange={handleChange} fullWidth type="password" required />
|
||||
</Grid>
|
||||
<Grid item xs={12}>
|
||||
<TextField label="Confirm Password" name="confirmPassword" value={formData.confirmPassword} onChange={handleChange} fullWidth type="password" error={errors.passwordMatch} helperText={errors.passwordMatch ? "Passwords do not match" : ""} required />
|
||||
</Grid>
|
||||
</Grid>
|
||||
</DialogContent>
|
||||
<DialogActions>
|
||||
<Button onClick={() => setOpenDialog(false)} color="secondary">
|
||||
Cancel
|
||||
</Button>
|
||||
<Button onClick={handleSubmit} color="primary" disabled={!formData.name || !formData.email || !formData.password || errors.passwordMatch || !formData.id_role || !formData.status || !formData.username}>
|
||||
Submit
|
||||
</Button>
|
||||
</DialogActions>
|
||||
</Dialog>
|
||||
</Box>
|
||||
</Box>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user