-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathCheckInPanel.vue
More file actions
207 lines (182 loc) · 5.69 KB
/
CheckInPanel.vue
File metadata and controls
207 lines (182 loc) · 5.69 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
<template>
<div class="flex flex-col bg-white rounded w-full py-6 px-4 border-none">
<h2 class="text-lg font-bold text-gray-900">
{{ __("Hey, {0} 👋", [employee?.data?.first_name]) }}
</h2>
<template v-if="settings.data?.allow_employee_checkin_from_mobile_app">
<div class="font-medium text-sm text-gray-500 mt-1.5" v-if="lastLog">
<span>{{ __("Last {0} was at {1}", [__(lastLogType), formatTimestamp(lastLog.time)]) }}</span>
<span class="whitespace-pre"> · </span>
<router-link :to="{ name: 'EmployeeCheckinListView' }" v-slot="{ navigate }">
<span @click="navigate" class="underline">View List</span>
</router-link>
</div>
<Button
class="mt-4 mb-1 drop-shadow-sm py-5 text-base"
id="open-checkin-modal"
@click="handleEmployeeCheckin"
>
<template #prefix>
<FeatherIcon
:name="nextAction.action === 'IN' ? 'arrow-right-circle' : 'arrow-left-circle'"
class="w-4"
/>
</template>
{{ nextAction.label }}
</Button>
</template>
<div v-else class="font-medium text-sm text-gray-500 mt-1.5">
{{ dayjs().format("ddd, D MMMM, YYYY") }}
</div>
</div>
<ion-modal
v-if="settings.data?.allow_employee_checkin_from_mobile_app"
ref="modal"
trigger="open-checkin-modal"
:initial-breakpoint="1"
:breakpoints="[0, 1]"
>
<div class="h-120 w-full flex flex-col items-center justify-center gap-5 p-4 mb-5">
<div class="flex flex-col gap-1.5 mt-2 items-center justify-center">
<div class="font-bold text-xl">
{{ dayjs(checkinTimestamp).format("hh:mm:ss a") }}
</div>
<div class="font-medium text-gray-500 text-sm">
{{ dayjs().format("D MMM, YYYY") }}
</div>
</div>
<template v-if="settings.data?.allow_geolocation_tracking">
<span v-if="locationStatus" class="font-medium text-gray-500 text-sm">
{{ locationStatus }}
</span>
<div class="rounded border-4 translate-z-0 block overflow-hidden w-full h-170">
<iframe
width="100%"
height="170"
frameborder="0"
scrolling="no"
marginheight="0"
marginwidth="0"
style="border: 0"
:src="`https://maps.google.com/maps?q=${latitude},${longitude}&hl=en&z=15&output=embed`"
>
</iframe>
</div>
</template>
<Button :loading="checkins.insert.loading" variant="solid" class="w-full py-5 text-sm disabled:bg-gray-700" @click="submitLog(nextAction.action)">
{{ __("Confirm {0}", [nextAction.label]) }}
</Button>
</div>
</ion-modal>
</template>
<script setup>
import { createListResource, toast, FeatherIcon } from "frappe-ui"
import { computed, inject, ref, onMounted, onBeforeUnmount } from "vue"
import { IonModal, modalController } from "@ionic/vue"
import { formatTimestamp } from "@/utils/formatters"
import { settings } from "@/data/settings"
const DOCTYPE = "Employee Checkin"
const socket = inject("$socket")
const employee = inject("$employee")
const dayjs = inject("$dayjs")
const __ = inject("$translate")
const checkinTimestamp = ref(null)
const latitude = ref(0)
const longitude = ref(0)
const locationStatus = ref("")
const checkins = createListResource({
doctype: DOCTYPE,
fields: ["name", "employee", "employee_name", "log_type", "time", "device_id"],
filters: {
employee: employee.data.name,
},
orderBy: "time desc",
})
checkins.reload()
const lastLog = computed(() => {
if (checkins.list.loading || !checkins.data) return {}
return checkins.data[0]
})
const lastLogType = computed(() => {
return lastLog?.value?.log_type === "IN" ? "check-in" : "check-out"
})
const nextAction = computed(() => {
return lastLog?.value?.log_type === "IN"
? { action: "OUT", label: __("Check Out") }
: { action: "IN", label: __("Check In") }
})
function handleLocationSuccess(position) {
latitude.value = position.coords.latitude
longitude.value = position.coords.longitude
locationStatus.value = [
__("Latitude: {0}°", [Number(latitude.value).toFixed(5)]),
__("Longitude: {0}°", [Number(longitude.value).toFixed(5)]),
].join(", ")
}
function handleLocationError(error) {
locationStatus.value = "Unable to retrieve your location"
if (error) locationStatus.value += `: ERROR(${error.code}): ${error.message}`
}
const fetchLocation = () => {
if (!navigator.geolocation) {
locationStatus.value = __("Geolocation is not supported by your current browser")
} else {
locationStatus.value = __("Locating...")
navigator.geolocation.getCurrentPosition(handleLocationSuccess, handleLocationError)
}
}
const handleEmployeeCheckin = () => {
checkinTimestamp.value = dayjs().format("YYYY-MM-DD HH:mm:ss")
if (settings.data?.allow_geolocation_tracking) {
fetchLocation()
}
}
const submitLog = (logType) => {
const actionLabel = logType === "IN" ? __("Check-in") : __("Check-out")
checkins.insert.submit(
{
employee: employee.data.name,
log_type: logType,
time: checkinTimestamp.value,
latitude: latitude.value,
longitude: longitude.value,
},
{
onSuccess() {
modalController.dismiss()
toast({
title: __("Success"),
text: __("{0} successful!", [actionLabel]),
icon: "check-circle",
position: "bottom-center",
iconClasses: "text-green-500",
})
},
onError(error) {
let messages = error.messages || []
for (const message of messages) {
toast({
title: __("Error"),
text: message || __("{0} failed!", [actionLabel]),
icon: "alert-circle",
position: "bottom-center",
iconClasses: "text-red-500",
})
}
},
}
)
}
onMounted(() => {
socket.emit("doctype_subscribe", DOCTYPE)
socket.on("list_update", (data) => {
if (data.doctype == DOCTYPE) {
checkins.reload()
}
})
})
onBeforeUnmount(() => {
socket.emit("doctype_unsubscribe", DOCTYPE)
socket.off("list_update")
})
</script>