|
@@ -1,8 +1,11 @@
|
|
|
<script lang="ts">
|
|
<script lang="ts">
|
|
|
- import type { AppointmentInputs, AppointmentList, WeekdayAppointment } from '$lib/dbdata';
|
|
|
|
|
|
|
+ import { api_host, authToken } from '$lib';
|
|
|
|
|
+ import type { AppointmentInputs, AppointmentList, AppointmentTimes, WeekdayAppointment } from '$lib/dbdata';
|
|
|
|
|
+ import { getTimeSlots } from '$lib/funcs';
|
|
|
import CreatorBaseInputs from '$lib/modules/CreatorBaseInputs.svelte';
|
|
import CreatorBaseInputs from '$lib/modules/CreatorBaseInputs.svelte';
|
|
|
import CreatorInputs from '$lib/modules/CreatorInputs.svelte';
|
|
import CreatorInputs from '$lib/modules/CreatorInputs.svelte';
|
|
|
import CreatorTimeInputs from '$lib/modules/CreatorTimeInputs.svelte';
|
|
import CreatorTimeInputs from '$lib/modules/CreatorTimeInputs.svelte';
|
|
|
|
|
+ import moment from 'moment';
|
|
|
import { onMount, tick } from 'svelte';
|
|
import { onMount, tick } from 'svelte';
|
|
|
|
|
|
|
|
let creatorInputs: AppointmentInputs[] = [{ name: '', type: 'text' }];
|
|
let creatorInputs: AppointmentInputs[] = [{ name: '', type: 'text' }];
|
|
@@ -11,25 +14,45 @@
|
|
|
let selectedWeek: number = 202502;
|
|
let selectedWeek: number = 202502;
|
|
|
let previousWeek: number = selectedWeek;
|
|
let previousWeek: number = selectedWeek;
|
|
|
let duration: number = 15;
|
|
let duration: number = 15;
|
|
|
|
|
+ let dueDate: Date = new Date();
|
|
|
|
|
+ let description: string = "test";
|
|
|
let renderTimeSelection: boolean = false;
|
|
let renderTimeSelection: boolean = false;
|
|
|
|
|
|
|
|
- $: console.log(appointmentList);
|
|
|
|
|
-
|
|
|
|
|
let appointmentList: AppointmentList[] = [
|
|
let appointmentList: AppointmentList[] = [
|
|
|
{
|
|
{
|
|
|
week: selectedWeek,
|
|
week: selectedWeek,
|
|
|
- appointments: [
|
|
|
|
|
- { name: 'Montag', active: false, times: [] },
|
|
|
|
|
- { name: 'Dienstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Mittwoch', active: false, times: [] },
|
|
|
|
|
- { name: 'Donnerstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Freitag', active: false, times: [] },
|
|
|
|
|
- { name: 'Samstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Sonntag', active: false, times: [] }
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ appointments: generateAppointments(splitYearAndWeek(selectedWeek).week, splitYearAndWeek(selectedWeek).year),
|
|
|
}
|
|
}
|
|
|
];
|
|
];
|
|
|
|
|
|
|
|
|
|
+ function splitYearAndWeek(value: number): { year: number, week: number } {
|
|
|
|
|
+ const year = Math.floor(value / 100); // Extracts the first 4 digits (year)
|
|
|
|
|
+ const week = value % 100; // Extracts the last 2 digits (week number)
|
|
|
|
|
+ return { year, week };
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Generates a JSON structure for a given calendar week with actual dates.
|
|
|
|
|
+ * @param weekNumber The calendar week number (1-53).
|
|
|
|
|
+ * @param year The year for which the week should be calculated.
|
|
|
|
|
+ * @returns An array of objects representing the days of the given week.
|
|
|
|
|
+ */
|
|
|
|
|
+ export function generateAppointments(weekNumber: number, year: number): WeekdayAppointment[] {
|
|
|
|
|
+ const weekdays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag', 'Sonntag'];
|
|
|
|
|
+
|
|
|
|
|
+ // Get the first day of the given calendar week (Monday-based week system)
|
|
|
|
|
+ const startOfWeek = moment().year(year).isoWeek(weekNumber).startOf('isoWeek');
|
|
|
|
|
+
|
|
|
|
|
+ const appointments = weekdays.map((day, index) => ({
|
|
|
|
|
+ name: day,
|
|
|
|
|
+ date: startOfWeek.clone().add(index, 'days').format('YYYY-MM-DD'),
|
|
|
|
|
+ active: false,
|
|
|
|
|
+ times: []
|
|
|
|
|
+ }));
|
|
|
|
|
+
|
|
|
|
|
+ return appointments;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
async function changeWeek() {
|
|
async function changeWeek() {
|
|
|
await tick();
|
|
await tick();
|
|
|
console.log("changed");
|
|
console.log("changed");
|
|
@@ -48,15 +71,7 @@
|
|
|
if (ix === -1) {
|
|
if (ix === -1) {
|
|
|
const tmp = {
|
|
const tmp = {
|
|
|
week: selectedWeek,
|
|
week: selectedWeek,
|
|
|
- appointments: [
|
|
|
|
|
- { name: 'Montag', active: false, times: [] },
|
|
|
|
|
- { name: 'Dienstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Mittwoch', active: false, times: [] },
|
|
|
|
|
- { name: 'Donnerstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Freitag', active: false, times: [] },
|
|
|
|
|
- { name: 'Samstag', active: false, times: [] },
|
|
|
|
|
- { name: 'Sonntag', active: false, times: [] }
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+ appointments: generateAppointments(splitYearAndWeek(selectedWeek).week, splitYearAndWeek(selectedWeek).year)
|
|
|
}
|
|
}
|
|
|
appointmentList = [... appointmentList, tmp];
|
|
appointmentList = [... appointmentList, tmp];
|
|
|
selectedWeekAppointments = tmp.appointments;
|
|
selectedWeekAppointments = tmp.appointments;
|
|
@@ -92,6 +107,52 @@
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ function convertAppointmentList() {
|
|
|
|
|
+ let times: AppointmentTimes[] = [];
|
|
|
|
|
+
|
|
|
|
|
+ appointmentList.forEach(entry => {
|
|
|
|
|
+ entry.appointments.filter(element => element.active !== false).forEach(day => {
|
|
|
|
|
+ day.times.forEach(time => {
|
|
|
|
|
+ // if (!time.start || !time.end)
|
|
|
|
|
+ times = [... times, ...getTimeSlots(day.date, time.start, time.end, duration)];
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return times;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ async function saveAppointment() {
|
|
|
|
|
+ if (!topic || !description || !dueDate || !location || !duration || !creatorInputs || !appointmentList) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ const convertedTimes: AppointmentTimes[] = convertAppointmentList();
|
|
|
|
|
+ console.log(convertedTimes);
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ const response = await fetch(`${api_host}/api/users/create`, {
|
|
|
|
|
+ method: "POST",
|
|
|
|
|
+ headers: {
|
|
|
|
|
+ "Content-Type": "application/json",
|
|
|
|
|
+ "Authorization": "Bearer " + $authToken
|
|
|
|
|
+ },
|
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
|
+ title: topic,
|
|
|
|
|
+ description: description,
|
|
|
|
|
+ dueDate: dueDate,
|
|
|
|
|
+ place: location,
|
|
|
|
|
+ duration: duration,
|
|
|
|
|
+ times: convertedTimes,
|
|
|
|
|
+ inputs: creatorInputs
|
|
|
|
|
+ })
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ } catch (error) {
|
|
|
|
|
+ console.log(error);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
onMount(() => {
|
|
onMount(() => {
|
|
|
// implement editor if query id param was detected
|
|
// implement editor if query id param was detected
|
|
|
});
|
|
});
|
|
@@ -106,7 +167,7 @@
|
|
|
|
|
|
|
|
{#if selectedWeekAppointments}
|
|
{#if selectedWeekAppointments}
|
|
|
{#key renderTimeSelection}
|
|
{#key renderTimeSelection}
|
|
|
- <CreatorTimeInputs bind:weekdays={selectedWeekAppointments} bind:selectedWeek />
|
|
|
|
|
|
|
+ <CreatorTimeInputs bind:weekdays={selectedWeekAppointments} />
|
|
|
{/key}
|
|
{/key}
|
|
|
{/if}
|
|
{/if}
|
|
|
|
|
|
|
@@ -115,8 +176,7 @@
|
|
|
<CreatorInputs bind:creatorInputs />
|
|
<CreatorInputs bind:creatorInputs />
|
|
|
|
|
|
|
|
<div class="grid">
|
|
<div class="grid">
|
|
|
- <button class="top-margin s6 large red">Loeschen</button>
|
|
|
|
|
- <button class="top-margin s6 large">Speichern</button>
|
|
|
|
|
|
|
+ <button on:click={saveAppointment} class="top-margin s12 large">Speichern</button>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
<style>
|
|
<style>
|