Skip to content

Commit b5bb95e

Browse files
committed
feat: add error handling to the share flow
1 parent 9a31105 commit b5bb95e

File tree

1 file changed

+53
-28
lines changed

1 file changed

+53
-28
lines changed

src/gui/src/UI/UIWindowShare.js

+53-28
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ async function UIWindowShare(items, recipient){
170170
recipients.forEach(perm => {
171171
//check if this user has been printed here before, important for multiple items
172172
if(!printed_users.includes(perm.user.username)){
173-
perm_list += `<div data-permission="${perm.permission}" class="item-perm-recipient-card item-prop-perm-entry" data-perm-uid="${perm.user.uid}" style="margin-bottom:5px; margin-top:5px;">`
173+
perm_list += `<div data-permission="${perm.permission}" class="item-perm-recipient-card item-prop-perm-entry" data-recipient-username="${perm.user.username}" data-perm-uid="${perm.user.uid}" data-perm-email="${perm.user.email}" style="margin-bottom:5px; margin-top:5px;">`
174174
perm_list += `${perm.user.email ?? perm.user.username}`;
175175
perm_list += `<div style="float:right;"><span class="remove-permission-link remove-permission-icon" data-recipient-username="${perm.user.username}" data-permission="${perm.permission}">✕</span></div>`;
176176
perm_list += `</div>`;
@@ -205,6 +205,22 @@ async function UIWindowShare(items, recipient){
205205
recipient_email = recipient_id;
206206
else
207207
recipient_username = recipient_id;
208+
209+
// see if the recipient is already in the list
210+
let recipient_already_in_list = false;
211+
$(el_window).find('.item-perm-recipient-card').each(function(){
212+
if((recipient_username && $(this).data('recipient-username') === recipient_username) ||(recipient_email && $(this).data('recipient-email') === recipient_email)){
213+
recipient_already_in_list = true;
214+
return false;
215+
}
216+
})
217+
218+
if(recipient_already_in_list){
219+
$(el_window).find('.error').html('This user already has access');
220+
$(el_window).find('.error').fadeIn();
221+
return;
222+
}
223+
208224
// can't share with self
209225
if(recipient_username === window.user.username){
210226
$(el_window).find('.error').html('You can\'t share with yourself');
@@ -244,33 +260,42 @@ async function UIWindowShare(items, recipient){
244260
]
245261
}),
246262
success: function(response) {
247-
// show success message
248-
$(el_window).find('.access-recipient-print').html(recipient_id);
249-
let perm_id = `fs:${items[0].uid}:${access_level}`;
250-
251-
// append recipient to list
252-
let perm_list = '';
253-
perm_list += `<div data-permission="${perm_id}" class="item-perm-recipient-card item-prop-perm-entry" style="margin-bottom:5px; margin-top:5px;">`
254-
perm_list += `${recipient_username}`;
255-
perm_list += `<div style="float:right;"><span class="remove-permission-link remove-permission-icon" data-recipient-username="${recipient_username}" data-permission="${perm_id}">✕</span></div>`;
256-
perm_list += `</div>`;
257-
258-
// reset input
259-
$(el_window).find('.error').hide();
260-
$(el_window).find('.access-recipient').val('');
261-
262-
// disable 'Give Access' button
263-
$(el_window).find('.give-access-btn').prop('disabled', true);
264-
265-
// append recipient to list
266-
$(el_window).find('.share-recipients').append(`${perm_list}`);
267-
268-
// add to contacts
269-
if(!contacts.includes(recipient_username)){
270-
contacts.push(recipient_username);
271-
puter.kv.set('contacts', JSON.stringify(contacts));
263+
if (response.status === "mixed") {
264+
response.recipients.forEach(recipient => {
265+
if (recipient.code === "user_does_not_exist") {
266+
$(el_window).find('.error').html(recipient.message);
267+
$(el_window).find('.error').fadeIn();
268+
cancelled_due_to_error = true;
269+
}
270+
});
271+
} else {
272+
// show success message
273+
$(el_window).find('.access-recipient-print').html(recipient_id);
274+
let perm_id = `fs:${items[0].uid}:${access_level}`;
275+
276+
// append recipient to list
277+
let perm_list = '';
278+
perm_list += `<div data-permission="${perm_id}" class="item-perm-recipient-card item-prop-perm-entry" style="margin-bottom:5px; margin-top:5px;">`
279+
perm_list += `${recipient_username}`;
280+
perm_list += `<div style="float:right;"><span class="remove-permission-link remove-permission-icon" data-recipient-username="${recipient_username}" data-permission="${perm_id}">✕</span></div>`;
281+
perm_list += `</div>`;
282+
283+
// reset input
284+
$(el_window).find('.error').hide();
285+
$(el_window).find('.access-recipient').val('');
286+
287+
// disable 'Give Access' button
288+
$(el_window).find('.give-access-btn').prop('disabled', true);
289+
290+
// append recipient to list
291+
$(el_window).find('.share-recipients').append(`${perm_list}`);
292+
293+
// add to contacts
294+
if(!contacts.includes(recipient_username)){
295+
contacts.push(recipient_username);
296+
puter.kv.set('contacts', JSON.stringify(contacts));
297+
}
272298
}
273-
274299
},
275300
error: function(err) {
276301
// at this point 'username_not_found' and 'shared_with_self' are the only
@@ -282,7 +307,7 @@ async function UIWindowShare(items, recipient){
282307
}
283308
// re-enable share button
284309
$(el_window).find('.give-access-btn').prop('disabled', false);
285-
310+
286311
}
287312
});
288313

0 commit comments

Comments
 (0)