Skip to content

Commit 2d03a8c

Browse files
committed
Export to csv from tabs by days
1 parent 42bd301 commit 2d03a8c

File tree

5 files changed

+93
-29
lines changed

5 files changed

+93
-29
lines changed

src/assets/css/general.css

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,31 @@ select{
4444
}
4545
.w-80{
4646
width: 80%;
47+
}
48+
input[type="button"]{
49+
background: #428bff;
50+
color: #fff;
51+
border-radius: 3px;
52+
height: 35px;
53+
line-height: 35px;
54+
padding: 0 20px;
55+
border: 0;
56+
font-size: 14px;
57+
font-weight: 500;
58+
cursor: pointer;
59+
min-width: 80px;
60+
text-align: center;
61+
width: 200px;
62+
}
63+
64+
input[type="button"]:hover {
65+
background: #5c9dfe;
66+
text-decoration: none;
67+
}
68+
69+
70+
input[type="button"][disabled]{
71+
border: 1px solid #999999;
72+
background-color: #cccccc;
73+
color: #666666;
4774
}

src/assets/css/settings.css

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -185,34 +185,6 @@ label{
185185
z-index: 0
186186
}
187187

188-
input[type="button"]{
189-
background: #428bff;
190-
color: #fff;
191-
border-radius: 3px;
192-
height: 35px;
193-
line-height: 35px;
194-
padding: 0 20px;
195-
border: 0;
196-
font-size: 14px;
197-
font-weight: 500;
198-
cursor: pointer;
199-
min-width: 80px;
200-
text-align: center;
201-
width: 200px;
202-
}
203-
204-
input[type="button"]:hover {
205-
background: #5c9dfe;
206-
text-decoration: none;
207-
}
208-
209-
210-
input[type="button"][disabled]{
211-
border: 1px solid #999999;
212-
background-color: #cccccc;
213-
color: #666666;
214-
}
215-
216188
.hidden{
217189
display: none !important;
218190
}

src/components/ByDays.vue

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
<span @click="presetDateRange(range)">{{ label }}</span>
3838
</template></VueDatePicker
3939
>
40+
<input type="button" value="Export to CSV" @click="exportToCsv()" />
4041
</div>
4142
<div class="stats-block block">
4243
<div class="header">Average time on selected days</div>
@@ -77,6 +78,8 @@ import { TabListByDays } from '../dto/tabListSummary';
7778
import { useTabListByDays } from '../compositions/tab-list-by-days';
7879
import { convertSummaryTimeToString } from '../utils/converter';
7980
import { ranges, ThisWeekRange } from '../utils/date';
81+
import { useImportToCsvWithData } from '../compositions/toCsv';
82+
import { useFile, FileType } from '../compositions/loadFile';
8083
8184
const tabsByDays = ref<TabListByDays>();
8285
const isLoading = ref<boolean>();
@@ -113,6 +116,17 @@ onMounted(async () => {
113116
const dateTo = selectedDate.value?.[1] as Date;
114117
await loadList(dateFrom, dateTo);
115118
});
119+
120+
async function exportToCsv() {
121+
const dateFrom = selectedDate.value?.[0] as Date;
122+
const dateTo = selectedDate.value?.[1] as Date;
123+
const csv = await useImportToCsvWithData(tabsByDays.value?.days);
124+
useFile(
125+
csv,
126+
FileType.CSV,
127+
`websites_${dateFrom.toLocaleDateString()}-${dateTo.toLocaleDateString()}.csv`,
128+
);
129+
}
116130
</script>
117131

118132
<style scoped>
@@ -139,7 +153,9 @@ onMounted(async () => {
139153
color: rgb(59, 59, 59);
140154
}
141155
.date-block {
142-
margin: 0 20px 0 20px;
156+
display: flex;
157+
justify-content: space-between;
158+
margin: 0 25px;
143159
}
144160
.date-block .date-picker {
145161
width: 250px;

src/compositions/loadFile.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export enum FileType {
2+
CSV = 'text/csv',
3+
}
4+
5+
export function useFile(data: string, type: FileType, fileName: string) {
6+
const file = new Blob([data], { type: type });
7+
let downloadLink;
8+
downloadLink = document.createElement('a');
9+
downloadLink.download = fileName;
10+
downloadLink.href = window.URL.createObjectURL(file);
11+
downloadLink.style.display = 'none';
12+
document.body.appendChild(downloadLink);
13+
downloadLink.click();
14+
}

src/compositions/toCsv.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { DayTabs } from '../dto/tabListSummary';
2+
import { useTabListByDays } from './tab-list-by-days';
3+
4+
const CSV_HEADER = 'date,website,time(sec),sessions\r\n';
5+
6+
export async function useImportToCsvWithData(days: DayTabs[] | undefined): Promise<string> {
7+
return getCsv(days);
8+
}
9+
10+
export async function useImportToCsv(dateFrom: Date, dateTo: Date): Promise<string> {
11+
const summary = await useTabListByDays(dateFrom, dateTo);
12+
if (summary == null) return CSV_HEADER;
13+
14+
return getCsv(summary.days);
15+
}
16+
17+
function getCsv(days: DayTabs[] | undefined) {
18+
let str = CSV_HEADER;
19+
20+
if (days != undefined && days.length > 0) {
21+
days.forEach(day => {
22+
let newLine = '';
23+
day.tabs.forEach(tab => {
24+
newLine += day.day + ',';
25+
newLine += tab.url + ',';
26+
newLine += tab.summaryTime + ',';
27+
newLine += tab.sessions;
28+
newLine += '\r\n';
29+
});
30+
str += newLine + '\r\n';
31+
});
32+
}
33+
34+
return str;
35+
}

0 commit comments

Comments
 (0)