From aab4fbb2e15d9fea526400e33c8304bf3efba086 Mon Sep 17 00:00:00 2001
From: Terry Truong <terry06890@gmail.com>
Date: Mon, 17 Oct 2022 15:44:35 +1100
Subject: Display event data from server

Add queryServer()
Add image display
Add conversions from JSON responses to HistDate and HistEvent
---
 src/App.vue | 37 +++++++++++--------------------------
 1 file changed, 11 insertions(+), 26 deletions(-)

(limited to 'src/App.vue')

diff --git a/src/App.vue b/src/App.vue
index 24da714..9ac8e3f 100644
--- a/src/App.vue
+++ b/src/App.vue
@@ -38,7 +38,7 @@ import PlusIcon from './components/icon/PlusIcon.vue';
 import SettingsIcon from './components/icon/SettingsIcon.vue';
 import HelpIcon from './components/icon/HelpIcon.vue';
 // Other
-import {HistDate, TimelineState, HistEvent, getUnitDiff, MONTH_SCALE, DAY_SCALE, stepDate} from './lib';
+import {HistDate, TimelineState, HistEvent, queryServer, HistEventJson, jsonToHistEvent} from './lib';
 import {useStore} from './store';
 
 // Refs
@@ -100,34 +100,19 @@ function onTimelineRemove(idx: number){
 
 // Event data
 const eventMap: Ref<Map<number, HistEvent>> = ref(new Map()); // Maps event IDs to HistEvents
-let nextEventId = 0; // For generating placeholder events
-function onEventReq(startDate: HistDate, endDate: HistDate){
-	// Get number of existing events in range
-	let numExisting = 0;
-	for (let event of eventMap.value.values()){
-		if (!event.start.isEarlier(startDate) && !endDate.isEarlier(event.start)){
-			numExisting += 1;
-		}
+async function onEventReq(startDate: HistDate, endDate: HistDate){
+	// Get events from server
+	let urlParams = new URLSearchParams({type: 'events', range: `${startDate}.${endDate}`, limit: '10'});
+	let responseObj: HistEventJson[] = await queryServer(urlParams);
+	if (responseObj == null){
+		return;
 	}
-	// Possibly add new events
-	let tempScale = 1;
-	let numUnits = getUnitDiff(startDate, endDate, tempScale);
-	if (numUnits < 2){
-		tempScale = MONTH_SCALE;
-		numUnits = getUnitDiff(startDate, endDate, tempScale);
-		if (numUnits < 2){
-			tempScale = DAY_SCALE;
-			numUnits = getUnitDiff(startDate, endDate, tempScale);
+	// Add to map
+	for (let eventObj of responseObj){
+		if (!eventMap.value.has(eventObj.id)){
+			eventMap.value.set(eventObj.id, jsonToHistEvent(eventObj));
 		}
 	}
-	for (let i = 0; i < 3 - numExisting; i++){
-		let start = startDate.clone();
-		let steps = Math.floor(Math.random() * (numUnits + 1));
-		stepDate(start, tempScale, {count: steps, inplace: true});
-		let event = {id: nextEventId, title: `Event ${nextEventId}`, start, startUpper: null, end: null, endUpper: null};
-		eventMap.value.set(event.id, event);
-		nextEventId += 1;
-	}
 }
 
 // For resize handling
-- 
cgit v1.2.3

