Skip to content

Commit

Permalink
Switch to axios
Browse files Browse the repository at this point in the history
  • Loading branch information
Ziedelth committed Apr 20, 2024
1 parent 9386e45 commit 1c62bf9
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class AnimeController : HasPageableRoute() {

@Path("/{uuid}")
@Get
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun animeDetails(
@PathParam("uuid") uuid: UUID,
Expand All @@ -99,7 +99,7 @@ class AnimeController : HasPageableRoute() {

@Path("/{uuid}")
@Put
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun updateAnime(@PathParam("uuid") uuid: UUID, @BodyParam animeDto: AnimeDto): Response {
val updated = animeService.update(uuid, animeDto)
Expand All @@ -108,7 +108,7 @@ class AnimeController : HasPageableRoute() {

@Path("/{uuid}")
@Delete
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun deleteAnime(@PathParam("uuid") uuid: UUID): Response {
animeService.delete(animeService.find(uuid) ?: return Response.notFound())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import fr.shikkanime.converters.AbstractConverter
import fr.shikkanime.dtos.ConfigDto
import fr.shikkanime.services.ConfigService
import fr.shikkanime.utils.Constant
import fr.shikkanime.utils.routes.AdminSessionAuthenticated
import fr.shikkanime.utils.routes.Controller
import fr.shikkanime.utils.routes.JWTAuthenticated
import fr.shikkanime.utils.routes.Path
import fr.shikkanime.utils.routes.Response
import fr.shikkanime.utils.routes.method.Get
Expand All @@ -24,7 +24,7 @@ class ConfigController {

@Path
@Get
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun getConfigs(
@QueryParam("name") nameParam: String?,
Expand All @@ -40,7 +40,7 @@ class ConfigController {

@Path("/{uuid}")
@Put
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun updateConfig(@PathParam("uuid") uuid: UUID, @BodyParam configDto: ConfigDto): Response {
configService.update(uuid, configDto)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class EpisodeMappingController : HasPageableRoute() {

@Path("/{uuid}")
@Get
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun read(@PathParam("uuid") uuid: UUID): Response {
return Response.ok(AbstractConverter.convert(episodeMappingService.find(uuid), EpisodeMappingDto::class.java))
}

@Path("/{uuid}")
@Put
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun updateEpisode(
@PathParam("uuid") uuid: UUID,
Expand All @@ -93,7 +93,7 @@ class EpisodeMappingController : HasPageableRoute() {

@Path("/{uuid}")
@Delete
@JWTAuthenticated
@AdminSessionAuthenticated
@OpenAPI(hidden = true)
private fun deleteEpisode(@PathParam("uuid") uuid: UUID): Response {
episodeMappingService.delete(episodeMappingService.find(uuid) ?: return Response.notFound())
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/fr/shikkanime/modules/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ private suspend fun handleTemplateResponse(
val map = response.data as Map<String, Any> // NOSONAR
val modelMap = (map["model"] as Map<String, Any?>).toMutableMap() // NOSONAR
setGlobalAttributes(modelMap, controller, replacedPath, map["title"] as String?)
call.principal<TokenDto>()?.token?.let { modelMap["token"] = it }

call.respond(response.status, FreeMarkerContent(map["template"] as String, modelMap, "", response.contentType))
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/assets/js/axios.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/main/resources/assets/js/home_chart.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ document.addEventListener('DOMContentLoaded', async () => {
});

async function getMetrics() {
return await callApi('/api/metrics?hours=' + hoursElement.value);
return await axios.get('/api/metrics?hours=' + hoursElement.value)
.then(response => response.data);
}

async function setChartData() {
Expand Down
11 changes: 0 additions & 11 deletions src/main/resources/assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,4 @@ function copyToClipboard(content) {
textarea.select();
document.execCommand("copy");
document.body.removeChild(textarea);
}

async function callApi(url, options = {}) {
const {abortSignal, method = 'GET', authorization = null, body = null} = options;
const headers = {'Content-Type': 'application/json'};
if (authorization) headers['Authorization'] = 'Bearer ' + authorization;
const fetchOptions = {headers, signal: abortSignal, method};
if (method !== 'GET') fetchOptions.body = JSON.stringify(body);

return await fetch(url, fetchOptions)
.then(response => response.json());
}
1 change: 0 additions & 1 deletion src/main/resources/templates/_freemarker_implicit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
[#-- @ftlvariable name="selectedSimulcast" type="fr.shikkanime.dtos.SimulcastDto" --]
[#-- @ftlvariable name="googleSiteVerification" type="java.lang.String" --]
[#-- @ftlvariable name="seoDescription" type="java.lang.String" --]
[#-- @ftlvariable name="token" type="java.lang.String" --]
[#-- @ftlvariable name="currentSimulcast" type="fr.shikkanime.dtos.SimulcastDto" --]
[#-- @ftlvariable name="footerLinks" type="kotlin.collections.AbstractList<fr.shikkanime.entities.LinkObject>" --]
[#-- @ftlvariable name="seoLinks" type="kotlin.collections.AbstractList<fr.shikkanime.entities.enums.Link>" --]
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/admin/_layout.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</main>

<script src="/assets/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="/assets/js/axios.min.js" crossorigin="anonymous"></script>
<script src="/assets/js/main.js" crossorigin="anonymous"></script>
</body>
</html>
Expand Down
35 changes: 17 additions & 18 deletions src/main/resources/templates/admin/animes/edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,29 @@
</div>

<script>
async function getSimulcasts() {
return await callApi('/api/v1/simulcasts');
}
function getUuid() {
const url = new URL(window.location.href);
return url.pathname.split('/').pop();
}
async function getSimulcasts() {
return await axios.get('/api/v1/simulcasts')
.then(response => response.data)
.catch(() => []);
}
async function loadAnime() {
const uuid = getUuid();
return await callApi('/api/v1/animes/' + uuid, {authorization: '${token}'});
return await axios.get('/api/v1/animes/' + uuid)
.then(response => response.data)
.catch(() => ({}));
}
async function updateAnime(anime) {
const uuid = getUuid();
await callApi('/api/v1/animes/' + uuid, {
method: 'PUT',
authorization: '${token}',
body: anime
})
await axios.put('/api/v1/animes/' + uuid, anime)
.then(() => {
const toastEl = document.getElementById('successToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
Expand All @@ -234,14 +235,12 @@
async function deleteAnime() {
const uuid = getUuid();
await callApi('/api/v1/animes/' + uuid, {
method: 'DELETE',
authorization: '${token}'
}).catch(() => {
const toastEl = document.getElementById('errorToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
toastBootstrap.show();
});
await axios.delete('/api/v1/animes/' + uuid)
.catch(() => {
const toastEl = document.getElementById('errorToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
toastBootstrap.show();
});
}
function addSimulcast(simulcasts, anime, uuid) {
Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/templates/admin/animes/list.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
params = '?name=' + name;
}
return await callApi('/api/v1/animes' + params + '&page=' + (page || 1) + '&limit=7');
return await axios.get('/api/v1/animes' + params + '&page=' + (page || 1) + '&limit=7')
.then(response => response.data)
.catch(() => []);
}
</script>
</@navigation.display>
10 changes: 4 additions & 6 deletions src/main/resources/templates/admin/configs.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,13 @@
params = '?name=' + name;
}
return await callApi('/api/config' + params, {authorization: '${token}'});
return await axios.get('/api/config' + params)
.then(response => response.data)
.catch(() => []);
}
async function updateConfig(config) {
await callApi('/api/config/' + config.uuid, {
method: 'PUT',
authorization: '${token}',
body: config
})
await axios.put('/api/config/' + config.uuid, config)
.then(() => {
const toastEl = document.getElementById('successToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/templates/admin/dashboard.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@

<script>
document.addEventListener('DOMContentLoaded', function () {
callApi('/api/v1/animes?simulcast=${simulcast.uuid}')
.then(data => {
document.getElementById('simulcast-${simulcast.uuid}').innerText = data.total;
axios.get('/api/v1/animes?simulcast=${simulcast.uuid}')
.then(response => {
document.getElementById('simulcast-${simulcast.uuid}').innerText = response.data.total;
});
});
</script>
Expand Down
29 changes: 15 additions & 14 deletions src/main/resources/templates/admin/episodes/edit.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -217,17 +217,20 @@
async function loadEpisode() {
const uuid = getUuid();
return await callApi('/api/v1/episode-mappings/' + uuid, {authorization: '${token}'});
return axios.get('/api/v1/episode-mappings/' + uuid)
.then(response => response.data)
.catch(() => {
const toastEl = document.getElementById('errorToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
toastBootstrap.show();
});
}
async function updateEpisode(episode) {
const uuid = getUuid();
await callApi('/api/v1/episode-mappings/' + uuid, {
method: 'PUT',
authorization: '${token}',
body: episode
})
await axios.put('/api/v1/episode-mappings/' + uuid, episode)
.then(() => {
const toastEl = document.getElementById('successToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
Expand All @@ -243,14 +246,12 @@
async function deleteEpisode() {
const uuid = getUuid();
await callApi('/api/v1/episode-mappings/' + uuid, {
method: 'DELETE',
authorization: '${token}'
}).catch(() => {
const toastEl = document.getElementById('errorToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
toastBootstrap.show();
});
await axios.delete('/api/v1/episode-mappings/' + uuid)
.catch(() => {
const toastEl = document.getElementById('errorToast');
const toastBootstrap = bootstrap.Toast.getOrCreateInstance(toastEl)
toastBootstrap.show();
});
}
</script>
</@navigation.display>
3 changes: 2 additions & 1 deletion src/main/resources/templates/admin/episodes/list.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@
params = '?anime=' + anime;
}
return await callApi('/api/v1/episode-mappings' + params + '&page=' + (page || 1) + '&limit=9');
return await axios.get('/api/v1/episode-mappings' + params + '&page=' + (page || 1) + '&limit=9')
.then(response => response.data);
}
</script>
</@navigation.display>
1 change: 0 additions & 1 deletion src/main/resources/templates/site/_layout.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
<#nested 0>

<script src="/assets/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
<script src="/assets/js/main.js" crossorigin="anonymous"></script>
</body>
</html>
</#macro>
11 changes: 10 additions & 1 deletion src/main/resources/templates/site/search.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</div>
</div>

<script src="${baseUrl}/assets/js/axios.min.js"></script>
<script>
let abortController = null;
Expand All @@ -72,7 +73,15 @@
return [];
}
return await callApi('/api/v1/animes?name=' + trimmedQuery + '&limit=12', abortController.signal);
return axios.get('/api/v1/animes?name=' + trimmedQuery + '&limit=12', {signal: abortController.signal})
.then(response => response.data)
.catch(error => {
if (error.name === 'AbortError') {
return [];
}
throw error;
});
}
</script>
</@navigation.display>

0 comments on commit 1c62bf9

Please sign in to comment.