Implemented basic api getting and deletion

This commit is contained in:
Nicolai Ort 2021-08-16 16:47:15 +02:00
parent 91572bd24e
commit ca09aa0fd5
Signed by: niggl
GPG Key ID: 13AFA55AF62F269F
3 changed files with 72 additions and 41 deletions

View File

@ -25,6 +25,7 @@
}, },
"type": "module", "type": "module",
"dependencies": { "dependencies": {
"@sveltejs/adapter-static": "^1.0.0-next.16" "@sveltejs/adapter-static": "^1.0.0-next.16",
"axios": "^0.21.1"
} }
} }

18
src/lib/Apiclient.js Normal file
View File

@ -0,0 +1,18 @@
import axios from 'axios';
const config = {
username: "niggl",
password: "9VEBc596T7tiPB7mNJukfeH9LfGzrrJN"
}
export default class Apiclient {
static async getUrls() {
return (await axios.get("https://kauft.es/api?showVisits=true", {
auth: config
})).data
}
static async deleteUrl(shortcode) {
return (await axios.delete(`https://kauft.es/api/${shortcode}`, {
auth: config
})).status
}
}

View File

@ -1,46 +1,58 @@
<script> <script>
import Apiclient from '$lib/Apiclient';
$: urls = [];
let urlQuery = Apiclient.getUrls().then((res) => {
console.log('here');
urls = res;
});
function deleteUrl(shortcode) {
Apiclient.deleteUrl(shortcode).then((res) => {
urls = urls.filter((url) => url.shortcode != shortcode);
});
}
</script> </script>
<h2 class="text-3xl font-bold text-gray-800 dark:text-gray-100 pb-6">Manage all links</h2> <h2 class="text-3xl font-bold text-gray-800 dark:text-gray-100 pb-6">Manage all links</h2>
<div class="rounded-xl"> <div class="rounded-xl">
<table class="min-w-full divide-y divide-gray-200"> <table class="min-w-full divide-y divide-gray-200">
<thead class="bg-gray-50 dark:bg-gray-900 text-gray-800 dark:text-gray-100"> <thead class="bg-gray-50 dark:bg-gray-900 text-gray-800 dark:text-gray-100">
<tr> <tr>
<th <th scope="col" class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
scope="col" Shortcode
class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider" </th>
> <th scope="col" class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
Shortcode Target
</th> </th>
<th <th scope="col" class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider">
scope="col" Visits
class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider" </th>
> <th scope="col" class="relative px-6 py-3">
Target <span class="sr-only">Edit</span>
</th> </th>
<th </tr>
scope="col" </thead>
class="px-6 py-3 text-left text-xs font-medium uppercase tracking-wider" <tbody class="bg-white dark:bg-gray-700 divide-y divide-gray-200 dark:text-gray-100" x-max="1">
> {#each urls as url}
Visits <tr>
</th> <td
<th scope="col" class="relative px-6 py-3"> class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-gray-200"
<span class="sr-only">Edit</span> >
</th> {url.shortcode}
</tr> </td>
</thead> <td class="px-6 py-4 whitespace-nowrap text-sm">
<tbody class="bg-white dark:bg-gray-700 divide-y divide-gray-200 dark:text-gray-100" x-max="1"> <a href={url.target}>{url.target}</a>
<tr> </td>
<td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900 dark:text-gray-200"> TODO </td> <td class="px-6 py-4 whitespace-nowrap text-sm"> {url.visits} </td>
<td class="px-6 py-4 whitespace-nowrap text-sm"> <td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium">
<a href="https://sthsth">https://sthsth</a> <button
</td> on:click={deleteUrl(url.shortcode)}
<td class="px-6 py-4 whitespace-nowrap text-sm"> 69 </td> class="text-indigo-600 dark:text-red-600 hover:text-indigo-900 dark:hover:text-red-900"
<td class="px-6 py-4 whitespace-nowrap text-right text-sm font-medium"> >Delete</button
<a href="#" class="text-indigo-600 dark:text-red-600 hover:text-indigo-900 dark:hover:text-red-900">Delete</a> >
</td> </td>
</tr> </tr>
</tbody> {/each}
</table> </tbody>
</table>
</div> </div>