feat: import #831

Merged
Sainan merged 39 commits from import into main 2025-01-20 03:19:32 -08:00
3 changed files with 25 additions and 0 deletions
Showing only changes of commit e7d655aa58 - Show all commits

View File

@ -34,6 +34,9 @@ webuiRouter.get("/webui/settings", (_req, res) => {
webuiRouter.get("/webui/cheats", (_req, res) => { webuiRouter.get("/webui/cheats", (_req, res) => {
res.sendFile(path.join(rootDir, "static/webui/index.html")); res.sendFile(path.join(rootDir, "static/webui/index.html"));
}); });
webuiRouter.get("/webui/import", (_req, res) => {
res.sendFile(path.join(rootDir, "static/webui/index.html"));
});
// Serve static files // Serve static files
webuiRouter.use("/webui", express.static(path.join(rootDir, "static/webui"))); webuiRouter.use("/webui", express.static(path.join(rootDir, "static/webui")));

View File

@ -64,6 +64,9 @@
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/webui/cheats" data-bs-dismiss="offcanvas" data-bs-target="#sidebar">Cheats</a> <a class="nav-link" href="/webui/cheats" data-bs-dismiss="offcanvas" data-bs-target="#sidebar">Cheats</a>
</li> </li>
<li class="nav-item">
<a class="nav-link" href="/webui/import" data-bs-dismiss="offcanvas" data-bs-target="#sidebar">Import</a>
</li>
</ul> </ul>
</div> </div>
</div> </div>
@ -492,6 +495,11 @@
</div> </div>
</div> </div>
</div> </div>
<div data-route="/webui/import" data-title="Import | OpenWF WebUI">
<p>You can provide a full or partial inventory response (client respresentation) here. All fields that are supported by the importer <b>will be overwritten</b> in your account.</p>
<textarea class="form-control" id="import-inventory"></textarea>
<button class="btn btn-primary mt-3" onclick="doImport();">Submit</button>
</div>
</div> </div>
</div> </div>
<datalist id="datalist-Suits"></datalist> <datalist id="datalist-Suits"></datalist>

View File

@ -1083,3 +1083,17 @@ function doPopArchonCrystalUpgrade(type) {
}); });
}); });
} }
function doImport() {
coderabbitai[bot] commented 2025-01-19 23:54:40 -08:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Add error handling and user feedback.

The import function needs several improvements for robustness and user experience:

  1. Add try-catch for JSON parsing
  2. Add error handling for failed requests
  3. Add loading state indication
  4. Add success/failure feedback

Here's a suggested implementation:

 function doImport() {
+    const importButton = document.querySelector('button[onclick="doImport();"]');
+    const originalText = importButton.textContent;
+    importButton.disabled = true;
+    importButton.textContent = 'Importing...';
+
+    let inventory;
+    try {
+        inventory = JSON.parse($("#import-inventory").val());
+    } catch (e) {
+        alert('Invalid JSON format');
+        importButton.disabled = false;
+        importButton.textContent = originalText;
+        return;
+    }
+
     revalidateAuthz(() => {
         $.post({
             url: "/custom/import?" + window.authz,
             contentType: "text/plain",
             data: JSON.stringify({
-                inventory: JSON.parse($("#import-inventory").val())
+                inventory
             })
-        }).then(function () {
+        }).then(function() {
+            alert('Import successful!');
             updateInventory();
+        }).fail(function(error) {
+            alert('Import failed: ' + error.responseText);
+        }).always(function() {
+            importButton.disabled = false;
+            importButton.textContent = originalText;
         });
     });
 }

Committable suggestion skipped: line range outside the PR's diff.

🧰 Tools
🪛 eslint

[error] 1092-1092: 'doImport' is defined but never used.

(@typescript-eslint/no-unused-vars)


[error] 1094-1094: '$' is not defined.

(no-undef)


[error] 1098-1098: '$' is not defined.

(no-undef)

_:hammer_and_wrench: Refactor suggestion_ **Add error handling and user feedback.** The import function needs several improvements for robustness and user experience: 1. Add try-catch for JSON parsing 2. Add error handling for failed requests 3. Add loading state indication 4. Add success/failure feedback Here's a suggested implementation: ```diff function doImport() { + const importButton = document.querySelector('button[onclick="doImport();"]'); + const originalText = importButton.textContent; + importButton.disabled = true; + importButton.textContent = 'Importing...'; + + let inventory; + try { + inventory = JSON.parse($("#import-inventory").val()); + } catch (e) { + alert('Invalid JSON format'); + importButton.disabled = false; + importButton.textContent = originalText; + return; + } + revalidateAuthz(() => { $.post({ url: "/custom/import?" + window.authz, contentType: "text/plain", data: JSON.stringify({ - inventory: JSON.parse($("#import-inventory").val()) + inventory }) - }).then(function () { + }).then(function() { + alert('Import successful!'); updateInventory(); + }).fail(function(error) { + alert('Import failed: ' + error.responseText); + }).always(function() { + importButton.disabled = false; + importButton.textContent = originalText; }); }); } ``` > Committable suggestion skipped: line range outside the PR's diff. <details> <summary>🧰 Tools</summary> <details> <summary>🪛 eslint</summary> [error] 1092-1092: 'doImport' is defined but never used. (@typescript-eslint/no-unused-vars) --- [error] 1094-1094: '$' is not defined. (no-undef) --- [error] 1098-1098: '$' is not defined. (no-undef) </details> </details> <!-- This is an auto-generated comment by CodeRabbit -->
revalidateAuthz(() => {
$.post({
url: "/custom/import?" + window.authz,
contentType: "text/plain",
data: JSON.stringify({
inventory: JSON.parse($("#import-inventory").val())
})
}).then(function() {
updateInventory();
});
});
}