import { Amplify, API, Auth, withSSRContext, graphqlOperation, Storage } from "aws-amplify";
import { DashboardProps } from "../common/dashboard-props";
import Webcam from "react-webcam";
import { useRef } from "react";
import { useCallback, SetStateAction, useState, Dispatch } from "react";
import { GraphQLResult, GRAPHQL_AUTH_MODE } from "@aws-amplify/api";
import { callGraphQL, callGraphQLWithSimpleInput, callGraphQLSimpleQuery } from "../common/common-types"
import { loginuser } from "../src/graphql/queries"
import { LoginuserQuery } from "../src/API"
import { HandThumbsUp } from 'react-bootstrap-icons';
interface LoginPageProps {
CompanyId: string,
Confidence: number,
DOB: string,
FaceId: string,
FaceImage: string,
FirstName: string,
LastName: string,
Message: string,
RegistrationStatus: string,
Success: boolean,
Busy: boolean,
UserId: string,
}
interface LoginSummaryRowData {
header: string,
value: string,
}
const initialProps = {
CompanyId: '',
Confidence: 0,
DOB: '',
FaceId: '',
FaceImage: '',
FirstName: '',
LastName: '',
Message: '',
RegistrationStatus: '',
Success: true,
Busy: false,
UserId: '',
}
const initialPropsBusy = {...JSON.parse(JSON.stringify(initialProps)), Busy: true};
const LoginSummaryRow = (props: LoginSummaryRowData) => {
return (
| {props.header} |
{props.value} |
)
}
async function doLogin(imageBytesb64: string, setState: Dispatch>) {
let input = {
imageDataBase64: imageBytesb64
};
setState({...initialPropsBusy});
// https://dev.to/rmuhlfeldner/how-to-use-an-aws-amplify-graphql-api-with-a-react-typescript-frontend-2g79
const { data } = await callGraphQLWithSimpleInput(
{
query: loginuser,
authMode: GRAPHQL_AUTH_MODE.AMAZON_COGNITO_USER_POOLS,
variables: input
}
);
setState({
CompanyId: data?.loginuser?.CompanyId as string,
Confidence: data?.loginuser?.Confidence as number,
DOB: data?.loginuser?.DOB as string,
FaceId: data?.loginuser?.FaceId as string,
FaceImage: data?.loginuser?.FaceImage as string,
FirstName: data?.loginuser?.FirstName as string,
LastName: data?.loginuser?.LastName as string,
Message: data?.loginuser?.Message as string,
RegistrationStatus: data?.loginuser?.RegistrationStatus as string,
Success: data?.loginuser?.Success as boolean,
Busy: false,
UserId: data?.loginuser?.UserId as string,
});
}
export const LoginUser = (props: DashboardProps) => {
const videoConstraints = {
width: 300,
height: 169,
facingMode: "user"
};
const [state, setState] = useState(initialProps as LoginPageProps);
const webcamRef = useRef(null) as any;
const capture = useCallback(
async () => {
const base64header = 'data:image/jpeg;base64,';
const imageSrc = webcamRef?.current?.getScreenshot();
const imageBytesb64 = imageSrc.replace(base64header, '');
await doLogin(imageBytesb64, setState);
},
[webcamRef]
);
return (
Login failed
);
};
export default LoginUser;