Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 36 additions & 7 deletions src/NcSender.Client/src/features/tools/ToolsTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
>

<!-- Save Error Dialog -->
<Dialog v-if="showSaveErrorDialog" @close="showSaveErrorDialog = false" :show-header="false" size="small">
<Dialog v-if="showSaveErrorDialog" @close="showSaveErrorDialog = false" :show-header="false" size="small" :z-index="100000">
<ConfirmPanel
title="Error Saving Tool"
:message="saveErrorMessage"
Expand Down Expand Up @@ -954,11 +954,12 @@ const getToolNumberInfo = (num: number) => {
const assignedTool = tools.value.find(t => t.toolNumber === num && t.id !== editingTool.value?.id);

if (assignedTool) {
// If editing, show "Swap with", if adding new, show "Already assigned to"
// Editing swaps the two tools' slots; a new tool has no slot to give back,
// so it takes the slot and the other tool is left without one
const toolInfo = `#${assignedTool.toolId} - ${assignedTool.name}`;
return editingTool.value
? ` (Swap with: ${toolInfo})`
: ` (Already assigned to: ${toolInfo})`;
: ` (Takes it from: ${toolInfo})`;
}

return '';
Expand Down Expand Up @@ -1149,15 +1150,43 @@ const saveTool = async () => {
}
}
} else {
// Add new tool
// Add new tool. The server refuses a slot another tool holds, so that
// tool gives it up first, and gets it back if the add fails.
const holder = toolData.toolNumber !== null && toolData.toolNumber !== undefined
? tools.value.find(t => t.toolNumber === toolData.toolNumber)
: undefined;

if (holder) {
const unassignResponse = await fetch(`${api.baseUrl}/api/tools/${holder.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ ...holder, toolNumber: null })
});

if (!unassignResponse.ok) {
saveErrorMessage.value = 'Failed to unassign conflicting tool: ' + await readError(unassignResponse);
showSaveErrorDialog.value = true;
return;
}
}

const response = await fetch(`${api.baseUrl}/api/tools`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(toolData)
});
}).catch(() => null);

if (!response.ok) {
saveErrorMessage.value = 'Failed to add tool: ' + await readError(response);
if (!response?.ok) {
saveErrorMessage.value = 'Failed to add tool: ' + (response ? await readError(response) : 'the server didn\'t answer');
if (holder) {
const restored = await fetch(`${api.baseUrl}/api/tools/${holder.id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(holder)
}).then(r => r.ok, () => false);
if (!restored) saveErrorMessage.value += `. Tool #${holder.toolId} was left without a slot`;
await loadTools();
}
showSaveErrorDialog.value = true;
return;
}
Expand Down