From 618cdf4d443bb4e4bd4741f9951619d418283371 Mon Sep 17 00:00:00 2001 From: freamon Date: Mon, 16 Dec 2024 02:47:06 +0000 Subject: [PATCH] API: demo more responses using x-api theme --- app/templates/themes/x_api/base.html | 2 +- app/templates/themes/x_api/index.html | 8 +++- app/templates/themes/x_api/js/site.js | 53 ++++++++++++++++----------- 3 files changed, 39 insertions(+), 24 deletions(-) diff --git a/app/templates/themes/x_api/base.html b/app/templates/themes/x_api/base.html index d6daa031..74ecd2a0 100644 --- a/app/templates/themes/x_api/base.html +++ b/app/templates/themes/x_api/base.html @@ -40,7 +40,7 @@ - + {{ bootstrap.load_css() }} diff --git a/app/templates/themes/x_api/index.html b/app/templates/themes/x_api/index.html index 86595d80..495c2b83 100644 --- a/app/templates/themes/x_api/index.html +++ b/app/templates/themes/x_api/index.html @@ -1,6 +1,12 @@ {% extends 'themes/' + theme() + '/base.html' %} {% block app_content %} -

GET /api/alpha/site

+

JSON
+ +
+ +

+
JSON
+ {% endblock%} diff --git a/app/templates/themes/x_api/js/site.js b/app/templates/themes/x_api/js/site.js index 7c4b1993..35bfc629 100644 --- a/app/templates/themes/x_api/js/site.js +++ b/app/templates/themes/x_api/js/site.js @@ -1,6 +1,6 @@ const url = new URL(window.location.href); export const baseUrl = `${url.protocol}//${url.host}`; -const api = baseUrl + '/api/alpha/site'; +const api_site = baseUrl + '/api/alpha/site'; let jwt = null; let session_jwt = sessionStorage.getItem('jwt'); @@ -32,7 +32,7 @@ if (jwt != null) { ''; } -fetch(api, request) +fetch(api_site, request) .then(response => response.json()) .then(data => { // head @@ -41,7 +41,6 @@ fetch(api, request) document.querySelector('#icon_32').href = data.site.icon_32; document.querySelector('#icon_16').href = data.site.icon_16; document.querySelector('#icon_shortcut').href = data.site.icon_32; - document.querySelector('#favicon').href = baseUrl + '/static/images/favicon.ico'; // navbar document.querySelector('#navbar_title').innerHTML = 'Logo' + ' ' + data.site.name; @@ -106,29 +105,39 @@ fetch(api, request) communities_item.appendChild(communities_menu) navbar.appendChild(communities_item) + const user_settings_item = document.createElement('li') + user_settings_item.className = 'nav-item' + user_settings_item.innerHTML = 'User settings'; + navbar.appendChild(user_settings_item) - - /*const login_item = document.createElement('li') - login_item.className = 'nav-item' - login_item.innerHTML = 'Log in (via API)' - ul.appendChild(login_item) - - const communities_dropdown = document.createElement('li') - communities_dropdown.className = 'nav-item dropdown' - communities_dropdown.innerHTML = '' - const communities_dropdown_ul = document.createElement('ul') - communities_dropdown_ul.className = 'dropdown-menu' - const communities_dropdown_ul_item = document.createElement('li') - communities_dropdown_ul_item.className = 'dropdown-item' - communities_dropdown_ul_item.href = '/api/alpha/communities' - communities_dropdown_ul.appendChild(communities_dropdown_ul_item) - communities_dropdown.appendChild(communities_dropdown_ul) - ul.appendChild(communities_dropdown)*/ + const logout_item = document.createElement('li') + logout_item.className = 'nav-item' + logout_item.innerHTML = 'Log out (via API)'; + navbar.appendChild(logout_item) } // site info + if (jwt != null) { + document.querySelector('#site_request').innerHTML = 'GET /api/alpha/site [LOGGED IN]' + document.querySelector('#post_list_request').innerHTML = 'GET /api/alpha/post/list?type_=Subscribed&sort=New&page=1

' + } else { + document.querySelector('#site_request').innerHTML = 'GET /api/alpha/site [LOGGED OUT]' + document.querySelector('#post_list_request').innerHTML = 'GET /api/alpha/post/list?type_=Popular&sort=Hot&page=1

' + } + document.querySelector('#site_json').textContent = JSON.stringify(data, null, 2); }) + +if (jwt != null) { + var api_postlist = baseUrl + '/api/alpha/post/list?type_=Subscribed&sort=New&page=1'; +} else { + var api_postlist = baseUrl + '/api/alpha/post/list?type_=Popular&sort=Hot&page=1'; +} + +fetch(api_postlist, request) + .then(response => response.json()) + .then(data => { + document.querySelector('#post_list_json').textContent = JSON.stringify(data, null, 2); + + })