forked from Stigmatoz/web-activity-time-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListWithTime.vue
More file actions
218 lines (200 loc) · 6.1 KB
/
ListWithTime.vue
File metadata and controls
218 lines (200 loc) · 6.1 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<template>
<ul readonly class="url-list">
<li v-for="(limit, i) of list" :key="i">
<div>
<img src="../assets/icons/delete.png" height="16" @click="deleteFromList(limit.domain)" />
<img
src="../assets/icons/edit.svg"
height="16"
@click="editItemFromList(limit.domain, limit.time)"
/>
<Favicon :type="TypeOfUrl.WebSite" :favicon="getFavicon(limit.domain)" />
<span>{{ limit.domain }}</span>
<div>
<p class="time-value d-inline-block" v-if="!completelyBlocked(limit.time)">
{{ t('limit.message') }} : {{ getTime(limit.time) }}
</p>
<p class="blocked" v-if="completelyBlocked(limit.time)">
{{ t('completelyBlocked.message') }}
</p>
</div>
</div>
</li>
</ul>
<div class="limits-time-block mt-20">
<input
type="text"
:disabled="isEdit"
class="d-inline-block"
:placeholder="t('enterWebsite.message')"
v-model="newWebsiteForList"
/>
<VueDatePicker v-model="time" time-picker class="date-picker height" />
<input
type="button"
class="d-inline-block small-btn"
:value="!isEdit ? t('addWebsite.message') : t('save.message')"
:disabled="isDisabledSaving"
@click="isEdit ? editItem() : addToList()"
/>
</div>
<div class="mt-10" v-if="showCompletelyBlockValue">
<label class="block-checkbox">
<input
type="checkbox"
class="filled-in"
id="viewTimeInBadge"
v-model="isCheckedCompletelyBlocked"
@change="completelyBlock"
/>
<span>{{ t('completelyBlocked.description') }}</span>
</label>
</div>
</template>
<script lang="ts">
export default {
name: 'ListWithTime',
};
</script>
<script lang="ts" setup>
import Favicon from './Favicon.vue';
import { getFavicon } from '../utils/favicon';
import { useNotification } from '@kyvg/vue3-notification';
import { useI18n } from 'vue-i18n';
import { injecStorage } from '../storage/inject-storage';
import { Time } from '../utils/time';
import { computed, onMounted, ref } from 'vue';
import { ListWithTime, TypeOfUrl } from '../utils/enums';
import { StorageParams } from '../storage/storage-params';
import { isDomainEquals } from '../utils/common';
import { extractHostname } from '../utils/extract-hostname';
import { convertHHMMToSeconds, convertSecondsToHHMM } from '../utils/converter';
import { Restriction } from '../entity/restriction';
import { BaseTimeList } from '../entity/baseTimeList';
import { Notifications } from '../entity/notification';
const { t } = useI18n();
const props = defineProps<{
type: ListWithTime;
}>();
const notification = useNotification();
const settingsStorage = injecStorage();
const list = ref<BaseTimeList[]>();
const isEdit = ref<boolean>();
const time = ref<Time>({
hours: 0,
minutes: 30,
});
const newWebsiteForList = ref<string>();
const storageParam = ref<StorageParams>();
const isCheckedCompletelyBlocked = computed(
() => time.value != undefined && time.value.hours == 0 && time.value.minutes == 0,
);
const showCompletelyBlockValue = computed(
() =>
props.type == ListWithTime.Limits &&
newWebsiteForList.value != undefined &&
newWebsiteForList.value != '',
);
onMounted(async () => {
switch (props.type) {
case ListWithTime.Limits:
list.value = Object.values(
await settingsStorage.getValue(StorageParams.RESTRICTION_LIST, []),
) as Restriction[];
storageParam.value = StorageParams.RESTRICTION_LIST;
break;
case ListWithTime.Notifications:
list.value = Object.values(
await settingsStorage.getValue(StorageParams.NOTIFICATION_LIST, []),
) as Notifications[];
storageParam.value = StorageParams.NOTIFICATION_LIST;
break;
}
});
function addToList() {
const existingItem = list.value?.find(x =>
isDomainEquals(extractHostname(x.domain), extractHostname(newWebsiteForList.value!)),
);
if (existingItem !== undefined) {
notification.notify({
title: 'You have already added this site',
type: 'error',
});
} else {
const newLimit = new Restriction(
extractHostname(newWebsiteForList.value!),
time.value.hours,
time.value.minutes,
);
list.value?.push(newLimit);
save(list.value);
newWebsiteForList.value = '';
}
}
function completelyBlock() {
time.value.hours = 0;
time.value.minutes = 0;
}
function completelyBlocked(time: number) {
return props.type == ListWithTime.Limits && time == 0;
}
function getTime(time: number) {
const timeObj = convertSecondsToHHMM(time);
return `${timeObj.hours}:${timeObj.minutes < 10 ? '0' + timeObj.minutes : timeObj.minutes}`;
}
const isDisabledSaving = computed(() => {
if (
props.type == ListWithTime.Notifications &&
time.value?.hours == 0 &&
time.value?.minutes == 0
)
return true;
return (
newWebsiteForList.value == '' || newWebsiteForList.value == undefined || time.value == null
);
});
function deleteFromList(url: string) {
list.value = list.value!.filter(x => x.domain != url);
save(list.value);
newWebsiteForList.value = '';
isEdit.value = false;
}
function editItemFromList(url: string, timeForUrl: number) {
isEdit.value = true;
newWebsiteForList.value = url;
const timeObj = convertSecondsToHHMM(timeForUrl);
time.value.hours = timeObj.hours;
time.value.minutes = timeObj.minutes;
}
function editItem() {
const existingItem = list.value?.find(x =>
isDomainEquals(extractHostname(x.domain), extractHostname(newWebsiteForList.value!)),
);
if (existingItem != undefined) {
existingItem.time = convertHHMMToSeconds(time.value.hours, time.value.minutes);
save(list.value);
newWebsiteForList.value = '';
isEdit.value = false;
}
}
async function save(value: any) {
if (value != undefined) await settingsStorage.saveValue(storageParam.value!, value);
}
</script>
<style scoped>
.limits-time-block {
display: flex;
justify-content: start;
}
.limits-time-block .date-picker {
width: 120px;
margin: 0 15px;
}
.blocked {
display: inline-block;
font-size: 13px;
color: gray;
margin-left: 55px;
margin-top: 5px;
}
</style>