diff --git a/backend/backend/urls.py b/backend/backend/urls.py index 159e552a..19ec7962 100644 --- a/backend/backend/urls.py +++ b/backend/backend/urls.py @@ -41,6 +41,6 @@ path('visitor-chart9/', views.visitor_chart9, name="visitor-chart9"), path('visitor-chart10/', views.visitor_chart10, name="visitor-chart10"), path('pid-to-name/', views.pid_to_name, name="pid-to-name"), + path('name-to-pid/', views.name_to_pid, name="name-to-pid"), path('checked-in/', views.checked_in, name="checked-in") -] - +] \ No newline at end of file diff --git a/backend/checkin/views.py b/backend/checkin/views.py index d6bd02d1..fe5c5c2e 100644 --- a/backend/checkin/views.py +++ b/backend/checkin/views.py @@ -11,6 +11,7 @@ from datetime import datetime, date, timedelta from functools import reduce from django.views.decorators.csrf import ensure_csrf_cookie +from django.views.decorators.csrf import csrf_exempt from django.forms.models import model_to_dict import json @@ -685,6 +686,7 @@ def checkin_data(request): data = serializers.serialize('json', dataset) return JsonResponse(data, safe=False) +@csrf_exempt def pid_to_name(request): data = json.loads(request.body) result = Checkin.objects.filter(PID=data['pid']).values("name").first() @@ -694,6 +696,17 @@ def pid_to_name(request): returnData = {"name": None} return JsonResponse(data=returnData) +@csrf_exempt +def name_to_pid(request): + data = json.loads(request.body) + name = data['name'] + result = Checkin.objects.filter(name__iexact=name.lower()).values("PID").first() + if (result): + returnData = {"pid": result["PID"]} + else: + returnData = {"pid": None} + return JsonResponse(data=returnData) + def checked_in(request): data = json.loads(request.body) result = Checkin.objects.filter(PID=data['pid'], checkedIn=True) diff --git a/src/Screens/CheckIn.js b/src/Screens/CheckIn.js index e335d6cf..b2f11d5c 100644 --- a/src/Screens/CheckIn.js +++ b/src/Screens/CheckIn.js @@ -61,7 +61,8 @@ export default class CheckIn extends React.Component { keypresses: Array(9).fill(null), keysPidBox: [], pid: "", - name: "" + name: "", + reason: '' }; this.handleChecked = this.handleChecked.bind(this); // set this, because you need get methods from CheckBox this.handleFirstTimeChecked = this.handleFirstTimeChecked.bind(this); @@ -104,20 +105,6 @@ export default class CheckIn extends React.Component { var date = today.getFullYear() + '-' + (today.getMonth() + 1) + '-' + today.getDate(); var time = today.getHours() + ":" + today.getMinutes(); - const item = { - name: name, // add name input field, make blank=false - PID: pid, - date: date, - timeIn: time, - timeOut: '00:00', // leave empty - reason: reason, - staff: "", - checkedIn: true, - hasPID: !noPID, - firstTime: false, - heard_about_al_through: "" - }; - // need to figure out how to send authorization token in http requests //axios.post('/api/checkins/', item); @@ -144,6 +131,7 @@ export default class CheckIn extends React.Component { handleChecked() { this.setState({ isChecked: !this.state.isChecked }); + this.setState({ }); } handleFirstTimeChecked() { @@ -200,7 +188,7 @@ export default class CheckIn extends React.Component { } handleKeyPress = (e) => { - if (!(e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA")) { + if (!(e.target.nodeName === "INPUT" || e.target.nodeName === "TEXTAREA")) { const pidArray = this.state.keypresses.slice(1, 9); pidArray.push(String.fromCharCode(e.keyCode)); if (pidArray.every(x => !isNaN(parseInt(x)))) { @@ -216,9 +204,9 @@ export default class CheckIn extends React.Component { }); } } else { - if (!(String.fromCharCode(e.keyCode) === '\b')) { + if ((!(String.fromCharCode(e.keyCode) === '\b') && (!(String.fromCharCode(e.keyCode) === '\x10')) && (!(String.fromCharCode(e.keyCode) === '\r'))) && (!(String.fromCharCode(e.keyCode) === '\t')) && (!(String.fromCharCode(e.keyCode) === '\x14'))) { this.state.keysPidBox.push(String.fromCharCode(e.keyCode)) - } else { + } else if (String.fromCharCode(e.keyCode) === '\b'){ this.state.keysPidBox.pop() } @@ -232,11 +220,43 @@ export default class CheckIn extends React.Component { this.setState({ keypresses: pidArray }); + } + } else if (this.state.keysPidBox.some(x => isNaN(parseInt(x)))){ + const nameArray = this.state.keysPidBox; + if (String.fromCharCode(e.keyCode) === '\r' || String.fromCharCode(e.keyCode) === '\t') { + const newName = nameArray.join(""); + this.checkForHistoryName(newName); } } } } + checkForHistoryName = (name) => { + + axios({ + method: "POST", + url: "/name-to-pid/", + headers: { 'X-CSRFToken': csrfToken }, + data: { + name: name + } + }).then((response => { + if (response.data.pid) { + if (this.state.pid.trim() === "") { + this.setState({ + pid: response.data.pid, + firstTime: false + }); + } + this.reasonRef.current.focus(); + } else { + this.setState({ + firstTime: true + }); + } + })); + } + pidChange = (event) => { this.setState({ pid: event.target.value @@ -247,7 +267,13 @@ export default class CheckIn extends React.Component { this.setState({ name: event.target.value }); - } + } + + reasonChange = (event) => { + this.setState({ + reason: event.target.value + }); + } componentDidMount() { document.addEventListener('keydown', this.handleKeyPress); @@ -269,6 +295,7 @@ export default class CheckIn extends React.Component { value, onChange: this.onChange }; + return (
Check In
- + this.checkForHistoryName(this.state.name)}/>

(Scanner can be used to input PID)

- +
- + + + {this.state.firstTime ?
-
+
: ""} +
- + ); diff --git a/src/Screens/CheckOut.js b/src/Screens/CheckOut.js index dae93eec..e38b3a68 100644 --- a/src/Screens/CheckOut.js +++ b/src/Screens/CheckOut.js @@ -3,7 +3,8 @@ import axios from "axios"; import '../App.css'; import { confirmAlert } from 'react-confirm-alert'; // Import import 'react-confirm-alert/src/react-confirm-alert.css'; // Import css -import {checkUserOut} from '../Utilities/checkoutUtils' +import {checkUserOut} from '../Utilities/checkoutUtils'; +import { getDateTime } from "../Utilities/timeUtils"; export default class CheckOut extends React.Component { @@ -23,15 +24,19 @@ export default class CheckOut extends React.Component { } submit = (obj) => { + let notify = true; + const currentDate = getDateTime().date; + if (currentDate === obj.date) { + notify = false; + } confirmAlert({ customUI: ({ onClose }) => { return (
-

Are you sure you want to check out?

- - +

Are you sure you want to check out?

+ {!notify ? : ""} + {!notify ? : ""} + {notify ? : ""}
); } diff --git a/src/Screens/Home.js b/src/Screens/Home.js index 594736ea..a4f6cedc 100644 --- a/src/Screens/Home.js +++ b/src/Screens/Home.js @@ -29,7 +29,7 @@ const Home = () => { } const handleKeyPress = (e) => { - if (e.target.nodeName == "INPUT" || e.target.nodeName == "TEXTAREA") + if (e.target.nodeName === "INPUT" || e.target.nodeName === "TEXTAREA") return; if (e.target.isContentEditable) return; diff --git a/src/Utilities/checkoutUtils.js b/src/Utilities/checkoutUtils.js index b238ec73..c15784b1 100644 --- a/src/Utilities/checkoutUtils.js +++ b/src/Utilities/checkoutUtils.js @@ -1,10 +1,10 @@ import axios from "axios"; +import { getDateTime } from "./timeUtils"; export const checkUserOut = (obj) => { // if clicked => mark checkedIn as false and set timeOut to current time - var today = new Date(); - var time = today.getHours() + ":" + today.getMinutes(); + const time = getDateTime().time const item = { name: obj.name, diff --git a/src/Utilities/timeUtils.js b/src/Utilities/timeUtils.js new file mode 100644 index 00000000..6c581a49 --- /dev/null +++ b/src/Utilities/timeUtils.js @@ -0,0 +1,9 @@ +export const getDateTime = () => { + const today = new Date() + const dateTime = { + date: today.getFullYear() + '-' + ('0' + (today.getMonth() + 1)) + '-' + today.getDate(), + time: today.getHours() + ":" + today.getMinutes(), + } + + return dateTime; +} \ No newline at end of file