diff --git a/src/components/TheAbout.vue b/src/components/TheAbout.vue index 2cc9a7e..b95b96e 100644 --- a/src/components/TheAbout.vue +++ b/src/components/TheAbout.vue @@ -13,55 +13,58 @@ import CommunityIcon from './icons/IconCommunity.vue' diff --git a/src/components/Todo.vue b/src/components/Todo.vue index cbf4d74..f647e7e 100644 --- a/src/components/Todo.vue +++ b/src/components/Todo.vue @@ -12,6 +12,24 @@ ToDo List + + +
- +

+ Sorry, there are no todo items{{ currentList !== 0 ? " in this list" : ""}}. +

+

You can add items or select another list.

+
+
@@ -69,7 +92,10 @@ diff --git a/src/router/index.js b/src/router/index.js index b92431f..c01a531 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -5,7 +5,7 @@ * https://basebox.tech */ -import {showError, store} from "../store"; +import {loggedIn, showError, store} from "../store"; import { createRouter, createWebHistory } from 'vue-router' import HomeView from '../views/HomeView.vue' import { objectToQueryString } from "../util/net"; @@ -41,7 +41,7 @@ router.beforeEach(async (to, from) => { // Handle OAuth callback request. // This request is coming in after the user entered her/his credentials at the IdP // login form; the IdP (e.g. Keycloak) redirects the browser to the URL of this route. - if (to.path === "/oauth-callback") { + if (to.path.startsWith("/oauth-callback")) { let queryString = objectToQueryString(to.query); console.info(`Got /oauth-callback with query string '${queryString}`); @@ -58,6 +58,11 @@ router.beforeEach(async (to, from) => { return {name: 'home'}; } + /* if no user is logged in, we redirect to the home page */ + if (!loggedIn() && to.path.length > 1 && to.path !== "/about") { + return {name: 'home'}; + } + }); export default router diff --git a/src/store.js b/src/store.js index d3b8d03..d0cb3d1 100644 --- a/src/store.js +++ b/src/store.js @@ -22,32 +22,9 @@ export const store = reactive({ fatalError: "", /* array of todo lists currently known */ - lists: [ - { id: 1, title: "Default"}, - { id: 2, title: "House" }, - { id: 3, title: "Car" }, - { id: 4, title: "Work" }, - ], + lists: [], /* known tasks */ - tasks: [ - { id: 1, completed: false, title: "Go to dentist", description: "Last time is way too long ago...", list: { id: 1 } }, - { id: 2, completed: true, title: "Change engine oil", description: "Use the good one", list: { id: 3 } }, - { id: 3, completed: false, title: "Clean windows", description: "Regualar stuff", list: { id: 1 } }, - { id: 4, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 5, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 6, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 7, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 8, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 9, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 10, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 11, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 12, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 13, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 14, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 15, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 16, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - { id: 17, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, - ], + tasks: [], }); @@ -101,43 +78,95 @@ export async function storeInit(session) { * We cannot run these requests in parallel, since the user record must exist in the database * before we can create the default list. */ - const tasks = []; - tasks.push( - gqlQuery(`mutation { + try { + console.info("NOTE: If next request fails, it is probably because the user already exists. In this case, the error is ignored."); + await gqlQuery(`mutation { createUser( username: "${session.username}", name: "${session.first_name} ${session.last_name}" ) { username } - }`)); - tasks.push(gqlQuery(`mutation { - createList( - title: "Default", - user: { username: "${session.username}" } - ) { - title + }`); + } catch(e) { + const errStr = e.toString(); + if (errStr.indexOf("already exists") === -1) { + /* this is an error */ + const e = `Failed to create user: ${errStr}`; + console.error(e); + showError(e); + return; } - }`)); + } - const allErrors = []; - - for (const task of tasks) { - try { - await task; - } catch (e) { - const errStr = e.toString(); - if (errStr.indexOf("already exists") === -1) { - /* this is an error */ - const e = `Init task failed: ${errStr}`; - console.error(e); - allErrors.push(e); + /* Load user info, lists and todos. */ + gqlQuery(`query { + getUser(username: "${store.session.username}") { + name + lists { + id + title + } + tasks { + id + title + description + completed + list { + id + } } } - } + }`).then(data => { + store.lists = data.User.lists ? data.User.lists : []; + store.tasks = data.User.tasks ? data.User.tasks : []; + /* create default list if necessary */ + if (store.lists.length === 0) { + gqlQuery(`mutation { + createList( + title: "Default", + user: { username: "${store.session.username}" } + ) { + title + } + }`); + } - if (allErrors.length) { - showError(allErrors.join("

")); - } + /* fix for basebox bug: task.list is returned as an array of List objects, where we expect just a single object */ + for (const task of store.tasks) { + if (Array.isArray(task.list)) { + task.list = task.list[0]; + } + } -} \ No newline at end of file + }).catch(err => { + const e = `Failed to load user/data: ${err.toString()}`; + console.error(e); + showError(e); + }); + + + /* Check if we already have a list named "Default". */ + + + +} + + +const testData = { + lists: [ + { id: 1, title: "Default"}, + { id: 2, title: "House" }, + { id: 3, title: "Car" }, + { id: 4, title: "Work" }, + ], + /* known tasks */ + tasks: [ + { id: 1, completed: false, title: "Go to dentist", description: "Last time is way too long ago...", list: { id: 1 } }, + { id: 2, completed: true, title: "Change engine oil", description: "Use the good one", list: { id: 3 } }, + { id: 3, completed: false, title: "Clean windows", description: "Regualar stuff", list: { id: 1 } }, + { id: 4, completed: false, title: "Oil stove hinges", description: "They are stiff and screechy", list: { id: 2 } }, + { id: 5, completed: false, title: "Do more benchmarking", description: "Find out how fast this thing really is", list: { id: 4 } }, + ], + +}; \ No newline at end of file