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

Random hr auth #266

Merged
merged 2 commits into from
Feb 10, 2024
Merged
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
73 changes: 31 additions & 42 deletions sections/HRContactDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import AddIcon from "@mui/icons-material/Add";
import DeleteIcon from "@mui/icons-material/Delete";
import {
Box,
Button,
IconButton,
Modal,
Stack,
TextField,
Tooltip,
} from "@mui/material";
import { Box, Button, IconButton, Modal, Stack, Tooltip } from "@mui/material";
import { GridColDef } from "@mui/x-data-grid";
import { useRouter } from "next/router";
import React, { useEffect, useState } from "react";
import { useForm } from "react-hook-form";

import DataGrid from "@components/DataGrid";
import addCompanyRequest, { HR } from "@callbacks/admin/company/company";
Expand Down Expand Up @@ -70,33 +61,40 @@ function DeleteHR(props: { id: string }) {
</>
);
}
function AuthHR(props: { id: string; name: string }) {
const {
register,
handleSubmit,
formState: { errors },
reset,
} = useForm<HRAuthResponse>();
function AuthHR(props: { id: string; Name: string }) {
const { token } = useStore();
const det = props;

const handleAuthHR = (data: HRAuthResponse) => {
const authHR = async () => {
const finData = { ...data, user_id: det.id, name: det.name };
const response = await HRAuth.post(token, finData);
if (response) {
reset({ password: "" });
}
};
authHR();
};
const { id, Name } = props;
const [openAuthHR, setOpenAuthHR] = useState(false);
const handleOpenAuthHR = () => {
setOpenAuthHR(true);
};
const handleCloseAuthHR = () => {
setOpenAuthHR(false);
};
const handleSubmit = () => {
const authHR = async () => {
const randomPwd = generateRandomPassword();
const newPwd: HRAuthResponse = { password: randomPwd };
const finData = { ...newPwd, user_id: id, name: Name };
let response = await HRAuth.post(token, finData);
if (response) {
handleCloseAuthHR();
}
};
authHR();
};

function generateRandomPassword(length = 12) {
const charset =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,<>?";
let password = "";
for (let i = 0; i < length; i += 1) {
const randomIndex = Math.floor(Math.random() * charset.length);
password += charset[randomIndex];
}
return password;
}

return (
<div>
<ActiveButton sx={{ height: 30 }} onClick={handleOpenAuthHR}>
Expand All @@ -105,22 +103,13 @@ function AuthHR(props: { id: string; name: string }) {
<Modal open={openAuthHR} onClose={handleCloseAuthHR}>
<Box sx={boxStyle}>
<Stack spacing={3}>
<h2>Enter New Password</h2>
<TextField
label="Enter New Password"
id="password"
type="password"
variant="standard"
{...register("password", { required: true })}
error={!!errors.password}
helperText={errors.password && "Password is required"}
/>
<h2>Generate New Password</h2>
<Button
variant="contained"
sx={{ width: "100%" }}
onClick={handleSubmit(handleAuthHR)}
onClick={handleSubmit}
>
Submit
Generate
</Button>
</Stack>
</Box>
Expand Down Expand Up @@ -182,7 +171,7 @@ const HRcotactDetailsColumns: GridColDef[] = [
field: "button2",
headerName: "Send Authorization Mail",
renderCell: (params) => (
<AuthHR id={params.row.email} name={params.row.name} />
<AuthHR id={params.row.email} Name={params.row.name} />
),
width: 100,
},
Expand Down
Loading