From 7509544b00914d2ef8a79c89baf75d014e5c13d2 Mon Sep 17 00:00:00 2001
From: Jonas Leder <jonas.leder@jobrouter.com>
Date: Tue, 8 Mar 2022 10:04:11 +0100
Subject: [PATCH] load skills from graphql

---
 js/customElements/skills.js    | 33 ++++++++++++++++++++-------------
 public/API/queries/queries.php |  6 ++++++
 public/API/queries/skills.php  | 23 +++++++++++++++++++++++
 public/API/skills.php          | 24 ------------------------
 4 files changed, 49 insertions(+), 37 deletions(-)
 create mode 100644 public/API/queries/skills.php
 delete mode 100644 public/API/skills.php

diff --git a/js/customElements/skills.js b/js/customElements/skills.js
index 65561b9..1f5a1d2 100644
--- a/js/customElements/skills.js
+++ b/js/customElements/skills.js
@@ -1,19 +1,26 @@
 class Skill extends HTMLElement {
     constructor() {
         super();
-        let xhr = new XMLHttpRequest();
-        xhr.onreadystatechange = () => {
-            if (xhr.readyState == 4 && xhr.status == 200) {
-                JSON.parse(xhr.responseText).forEach(skill => {
-                    const image = document.createElement("img");
-                    image.classList.add("skills");
-                    image.src = "/API/getFile.php?filename=" + skill;
-                    this.appendChild(image);
-                });
-            }
-        }
-        xhr.open("GET", "/API/skills.php");
-        xhr.send();
+        this.getSkills();
+    }
+
+    async getSkills(){
+        var graphql = JSON.stringify({
+            query: "query {\r\n  skills\r\n}"
+          })
+          var requestOptions = {
+            method: 'POST',
+            body: graphql,
+          };
+
+        let skills = (await (await fetch("/API/graphql.php", requestOptions)).json()).data.skills;
+        skills.forEach(skill => {
+            const image = document.createElement("img");
+            image.classList.add("skills");
+            image.src = "/API/getFile.php?filename=" + skill;
+            this.appendChild(image);
+        });
+
     }
 }
 
diff --git a/public/API/queries/queries.php b/public/API/queries/queries.php
index cc147c0..5b89f41 100644
--- a/public/API/queries/queries.php
+++ b/public/API/queries/queries.php
@@ -2,6 +2,8 @@
 use GraphQL\Type\Definition\ObjectType;
 use GraphQL\Type\Definition\Type;
 
+require "./queries/skills.php";
+
 $queryType = new ObjectType([
     'name' => 'Query',
     'fields' => [
@@ -9,5 +11,9 @@ $queryType = new ObjectType([
             'type' => Type::string(),
             'resolve' => fn ($rootValue, $args) => $sitekey,
         ],
+        'skills' => [
+            'type' => Type::listOf(Type::string()),
+            'resolve' => fn ($rootValue, $args) => getSkills(),
+        ],
     ],
 ]);
diff --git a/public/API/queries/skills.php b/public/API/queries/skills.php
new file mode 100644
index 0000000..bb57bc8
--- /dev/null
+++ b/public/API/queries/skills.php
@@ -0,0 +1,23 @@
+<?php
+
+function getSkills() {
+        include("./lib/config.php");
+        $s3Client = new Aws\S3\S3Client([
+                'version' => 'latest',
+                'region'  => 'us-east-1',
+                'endpoint' => $S3Server,
+                'use_path_style_endpoint' => true,
+                'credentials' => [
+                        'key'    => $S3AccessKey,
+                        'secret' => $S3SecretKey,
+                ],
+        ]);
+
+        $result = $s3Client->ListObjects(['Bucket' => $S3BucketName, 'Delimiter'=>'/', 'Prefix' => 'skills/']);
+
+        $response = [];
+        foreach ($result["Contents"] as $skill){
+                array_push($response, $skill["Key"]);
+        }
+        return $response;
+}
\ No newline at end of file
diff --git a/public/API/skills.php b/public/API/skills.php
deleted file mode 100644
index 28116e7..0000000
--- a/public/API/skills.php
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-include("./lib/config.php");
-require 'vendor/autoload.php';
-
-$s3Client = new Aws\S3\S3Client([
-        'version' => 'latest',
-        'region'  => 'us-east-1',
-        'endpoint' => $S3Server,
-        'use_path_style_endpoint' => true,
-        'credentials' => [
-                'key'    => $S3AccessKey,
-                'secret' => $S3SecretKey,
-            ],
-]);
-
-$result = $s3Client->ListObjects(['Bucket' => $S3BucketName, 'Delimiter'=>'/', 'Prefix' => 'skills/']);
-
-$response = [];
-foreach ($result["Contents"] as $skill){
-        array_push($response, $skill["Key"]);
-}
-
-header("Content-Type: application/json");
-echo json_encode($response);
\ No newline at end of file
-- 
GitLab