From f57d899200066f599b9061068fa6bee4d15058ab Mon Sep 17 00:00:00 2001 From: Matas Date: Fri, 22 Nov 2019 00:59:50 +0200 Subject: [PATCH 1/2] added hw2 --- assets/js/app.js | 27 ++++++++++++++------------- assets/js/validation.js | 24 +++++++++++++++++++++++- package.json | 4 ++-- src/Controller/PeopleController.php | 15 ++++++++++++++- templates/people/index.html.twig | 8 +++++++- yarn.lock | 26 +++++++++++++------------- 6 files changed, 73 insertions(+), 31 deletions(-) diff --git a/assets/js/app.js b/assets/js/app.js index f0bafb1df..4b5951618 100644 --- a/assets/js/app.js +++ b/assets/js/app.js @@ -6,24 +6,25 @@ */ // any CSS you require will output into a single css file (app.css in this case) -require('../css/app.scss'); +require("../css/app.scss"); // Need jQuery? Install it with "yarn add jquery", then uncomment to require it. -const $ = require('jquery'); -require('bootstrap'); +const $ = require("jquery"); +require("bootstrap"); $(document).ready(function() { - $('[data-toggle="popover"]').popover(); + $('[data-toggle="popover"]').popover(); }); -const axios = require('axios'); +const axios = require("axios"); + if (typeof usingVersionedFileJs !== "undefined") { - let versionedFileElement = document.getElementById('versionedFile'); - axios.get('/build/manifest.json') - .then(function (response) { - versionedFileElement.innerText = response.data['build/app.js']; - }) - .catch(function (error) { - versionedFileElement.innerText = 'Error: '.error; - }); + let versionedFileElement = document.getElementById("versionedFile"); + axios + .get("/build/manifest.json") + .then( + response => + (versionedFileElement.innerText = response.data["build/app.js"]) + ) + .catch(error => (versionedFileElement.innerText = "Error: ".error)); } diff --git a/assets/js/validation.js b/assets/js/validation.js index 1881c8a67..900c5086f 100644 --- a/assets/js/validation.js +++ b/assets/js/validation.js @@ -2,7 +2,8 @@ const axios = require('axios'); let name = document.getElementById('name'); let validationResult = document.getElementById('validation-result'); -const validateName = function () { + +const validateName = () => { validationResult.innerText = '...'; axios.post(validationResult.dataset.path, {input: name.value}) .then(function(response) { @@ -19,3 +20,24 @@ const validateName = function () { name.onkeyup = validateName; name.onchange = validateName; + +let team = document.getElementById('team'); +let validationResultTeam = document.getElementById('validation-result-team'); + +const validateTeam = () => { + validationResultTeam.innerText = '...'; + axios.post(validationResultTeam.dataset.path, {input: team.value}) + .then(function(response) { + if (response.data.valid) { + validationResultTeam.innerHTML = ":)"; + } else { + validationResultTeam.innerHTML = ":("; + } + }) + .catch(function (error) { + validationResultTeam.innerText = 'Error: ' + error; + }); +}; + +team.onkeyup = validateTeam; +team.onchange = validateTeam; diff --git a/package.json b/package.json index b381fcaae..f5cc7824c 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dependencies": { "axios": "^0.19.0", "prop-types": "^15.7.2", - "react": "^16.11.0", - "react-dom": "^16.11.0" + "react": "^16.12.0", + "react-dom": "^16.12.0" } } diff --git a/src/Controller/PeopleController.php b/src/Controller/PeopleController.php index bb86b5f8e..9636e236a 100644 --- a/src/Controller/PeopleController.php +++ b/src/Controller/PeopleController.php @@ -37,9 +37,12 @@ public function validate(Request $request, string $element) } $students = $this->getStudents(); + $teams = $this->getTeams(); switch ($element) { case 'name': return new JsonResponse(['valid' => in_array(strtolower($input), $students)]); + case 'team': + return new JsonResponse(['valid' => in_array(strtolower($input), $teams)]); } return new JsonResponse(['error' => 'Invalid arguments'], Response::HTTP_BAD_REQUEST); @@ -212,7 +215,7 @@ private function getStorage() "Viktoras" ], "students": [ - "Karolis", + "Karolis a", "Arnas", "Evaldas", "Algirdas" @@ -242,4 +245,14 @@ private function getStudents(): array } return $students; } + + private function getTeams(): array + { + $teams = []; + $storage = json_decode($this->getStorage(), true); + foreach ($storage as $teamName => $value) { + $teams[] = strtolower($teamName); + } + return $teams; + } } diff --git a/templates/people/index.html.twig b/templates/people/index.html.twig index cd18cf87f..b62ec22c6 100644 --- a/templates/people/index.html.twig +++ b/templates/people/index.html.twig @@ -8,11 +8,17 @@
Versijuojamas JavaScript failas: ...
- + +
+ + + + {% endblock %} + {% block javascripts %}