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 (