-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
191 lines (171 loc) · 5.26 KB
/
scripts.js
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
// FUNCTIONS
const formatDate = (date) => {
const [year, month, day] = date.slice(0, 10).split('-');
const newDate = `${month}-${day}-${year}`;
return newDate;
};
const setupFolders = () => {
fetch('/api/v1/folders')
.then(response => response.json())
.then(folders => folders.map((folder) => {
$('#folder-dropdown').prepend(`
<option value='${folder.id}'>${folder.folderName}</option>
`);
$('.folder-pane').prepend(`
<div class='folder-header-with-sort'>
<h4 class='view-folder-name'>${folder.folderName}</h4>
<div class='sort-controls'>
<p class='sorting-title hidden'>Sort by:</p>
<p class='sorting-direction hidden' onclick='setupLinks("newest")'>newest</p>
<p class='sorting-direction hidden' onclick='setupLinks("oldest")'>oldest</p>
</div>
</div>
<table class='folder-links-${folder.id} hidden'></table>
`);
},
));
};
const setupLinks = (order) => {
fetch('/api/v1/links')
.then(response => response.json())
.then((links) => {
const sortedLinks = links.sort((a, b) => {
if (order === 'newest') {
return b.id - a.id;
}
return a.id - b.id;
});
$('table').text('');
sortedLinks.map((link) => {
$(`.folder-links-${link.folderID}`).prepend(`
<tr>
<td class='view-link-title'>${link.linkLabel}</td>
<td class='view-link-shortUrl' onclick='redirect(${link.id})'>${link.linkShort}</td>
<td class='view-link-date'>${formatDate(link.created_at)}</td>
</tr>
`);
});
});
};
const validURL = (url) => {
const space = url.includes(' ');
const backslash = url.includes('\\');
const dot = url.includes('.');
return (!space && !backslash && dot);
};
const displayStatusMsg = (message, location) => {
$(`.${location}-status-msg`).text(message);
};
const emptyFolderForm = () => {
$('.new-folder-input').val('');
$('.folder-status-msg').text('');
};
const emptyLinkForm = () => {
$('.new-link-label').val('');
$('.new-link-long').val('');
$('.link-status-msg').text('');
};
const updateFolderDOM = (folderName, folderID) => {
$('#folder-dropdown').prepend(`
<option value='${folderID}'>${folderName}</option>
`);
$('.folder-pane').prepend(`
<div class='folder-header-with-sort'>
<h4 class='view-folder-name'>${folderName}</h4>
<div class='sort-controls'>
<p class='sorting-title hidden'>Sort by:</p>
<p class='sorting-direction hidden' onclick='setupLinks("newest")'>newest</p>
<p class='sorting-direction hidden' onclick='setupLinks("oldest")'>oldest</p>
</div>
</div>
<table class='folder-links-${folderID} hidden'></table>
`);
};
const updateLinkDOM = (link) => {
$(`.folder-links-${link.folderID}`).prepend(`
<tr>
<td class='view-link-title'>${link.linkLabel}</td>
<td class='view-link-shortUrl' onclick='redirect(${link.id})'>${link.linkShort}</td>
<td class='view-link-date'>${formatDate(link.created_at)}</td>
</tr>
`);
};
const createFolder = () => {
const folderName = $('.new-folder-input').val();
fetch('/api/v1/folders', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
folderName,
}),
})
.then(response => response.json())
.then((folder) => {
if (folder.error) {
$('.new-folder-input').focus();
throw new Error('Folder already exists');
}
$('.new-link-label').focus();
emptyFolderForm();
displayStatusMsg(`'${folderName}' created successfully`, 'folder');
updateFolderDOM(folderName, folder.id);
})
.catch(error => displayStatusMsg(error, 'folder'));
};
const createLink = () => {
const linkLabel = $('.new-link-label').val();
const folderID = $('#folder-dropdown').val();
let linkLong = $('.new-link-long').val();
if (validURL(linkLong)) {
if (!linkLong.startsWith('http')) linkLong = `http://${linkLong}`;
fetch('/api/v1/links', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
linkLabel,
linkLong,
folderID,
}),
})
.then(response => response.json())
.then((link) => {
$('.new-link-label').focus();
if (link.error) {
throw new Error('Link already exists');
}
emptyLinkForm();
displayStatusMsg(`'${linkLabel}' saved successfully`, 'link');
updateLinkDOM(link);
})
.catch(error => displayStatusMsg(error, 'link'));
} else {
displayStatusMsg('Not a valid URL', 'link');
$('.new-link-long').focus();
}
};
const redirect = (linkID) => {
fetch(`/api/v1/links/${linkID}`)
.then(response => response.json())
.then(link => window.open(link));
};
// SETUP
setupFolders();
setupLinks('newest');
// EVENT LISTENERS
$('.create-folder-form').on('submit', (e) => {
e.preventDefault();
createFolder();
});
$('.shorten-url-form').on('submit', (e) => {
e.preventDefault();
createLink();
});
$('.folder-pane').on('click', '.folder-header-with-sort', (e) => {
const folder = e.target;
$(folder).find('p').toggleClass('hidden');
$(folder).next('table').toggleClass('hidden');
});