aboutsummaryrefslogtreecommitdiffstats
path: root/pages/agenda.vue
blob: 7ef5f4422dc8d6fd4ec49e1afe99e7ddad42620c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<script setup lang="ts">
import Panel from '~/components/Panel.vue';
import { LucideRadio } from "lucide-vue-next";

const favouritesStore = useFavouritesStore();
const scheduleStore = useScheduleStore();
const errorStore = useErrorStore();
const config = useRuntimeConfig();

const favouriteEvents = computed(() => {
  return scheduleStore.events.filter((event) => favouritesStore.isFavourite(event));
});

const calendarStatus = ref('pending' as 'pending' | 'idle');
const calendarLink = ref('')
const calendarLinkWithPageProtocol = computed(() => {
  return window.location.protocol + '//' + calendarLink.value;
});

const calendarAction = ref(false);

useFetch(config.public.baseURL + '/calendar', {
  method: 'GET',
  server: false,
  lazy: true,
  onResponseError: ({ response }) => {
    calendarStatus.value = 'idle';
  },
  onResponse: ({ response }) => {
    if (!response.ok) {
      if (response.status !== 404) {
        errorStore.setError(response._data.message || 'An unknown error occurred');
      }
    } else if (response._data) {
      calendarLink.value = (response._data as any).data.url;
    }
    calendarStatus.value = 'idle';
  },
});

function generateCalendar() {
  calendarAction.value = true;
  useFetch(config.public.baseURL + '/calendar', {
    method: 'POST',
    server: false,
  lazy: true,
    onResponseError: ({ response }) => {
      errorStore.setError(response._data.message || 'An unknown error occurred');
      calendarAction.value = false;
    },
    onResponse: ({ response }) => {
      if (response._data) {
        calendarLink.value = (response._data as any).data.url;
      }
      calendarAction.value = false;
    },
  });
}

function deleteCalendar() {
  calendarAction.value = true;
  useFetch(config.public.baseURL + '/calendar', {
    method: 'DELETE',
    server: false,
    onResponseError: ({ response }) => {
      errorStore.setError(response._data.message || 'An unknown error occurred');
      calendarAction.value = false;
    },
    onResponse: () => {
      calendarLink.value = '';
      calendarAction.value = false;
    },
  });
}

</script>

<template>
  <Panel v-if="favouritesStore.status === 'pending'">
    <span>Updating favourites...</span>
  </Panel>

  <template v-else-if="favouriteEvents.length > 0">
    <div class="page">
      <Panel title="Agenda">
        <ul class="agenda-list">
          <li v-for="event in favouriteEvents" :key="event.id" class="agenda-item" >
            <EventListing :event="event" />
          </li>
        </ul>
      </Panel>
      <Panel>
        <template v-if="calendarStatus === 'pending'">
          <span>Fetching calendar status...</span>
        </template>
        
        <div v-else-if="calendarStatus === 'idle'" class="calendar">
          <template v-if="calendarLink">
            <span>You can add your agenda to your own calendar using the iCal link below</span>
            <Input :value="calendarLinkWithPageProtocol" readonly/>
            <Button @click="deleteCalendar" :loading="calendarAction">Delete calendar</Button>
          </template>
          <template v-else>
            <span>You do not have a calendar link yet. Use the button below to request a calendar link to subscribe to on your own calendar app.</span>
            <Button @click="generateCalendar" :loading="calendarAction">Request calendar</Button>
          </template>
        </div>
      </Panel>
    </div>
  </template>

  <Panel v-else>
    <span>You have not added any favourites yet.</span>
  </Panel>
</template>

<style scoped>
.agenda-list {
  list-style: none;
  margin: -1rem 0;
  padding: 0;
  display: grid;
}

.agenda-item {
  border-bottom: 1px solid var(--color-background-muted); 
}

.agenda-title, .calendar-title {
  margin-bottom: 1rem;
}

.agenda-item:last-child {
  border-bottom: none; 
}

.page, .calendar {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  line-height: 1.3;
}
</style>