Skip to content

Commit 913636f

Browse files
committed
fix: UI credentials
1 parent 41f08fc commit 913636f

File tree

1 file changed

+43
-22
lines changed

1 file changed

+43
-22
lines changed

ui/src/app/app.tsx

+43-22
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
import styles from "./app.module.css";
33
import sharedStyles from "../shared.module.css"
44

5-
import { AuthType, createClient, FileStat, ResponseDataDetailed } from "webdav";
5+
import { AuthType, createClient, FileStat, ResponseDataDetailed, WebDAVClient } from "webdav";
66
import React from "react";
77
import ProgressBar from "components/progress_bar";
88
import Settings, { SettingsObject } from "./settings";
99

10-
const client = createClient("http://localhost:7086/webdav", {
11-
authType: AuthType.Password,
12-
username: "flydav",
13-
password: "pass-for-testing" // sha256: 8c024a1ccc39abc26d05bacc4ab64b78ad4c4378e67df31975d5303ca2258a22
14-
});
10+
1511

1612
const dirname = (path: String) => {
1713
let ret = path.replace(/\\/g, '/').replace(/\/[^/]*$/, '');
@@ -36,33 +32,55 @@ const App = (): JSX.Element => {
3632
const [path, setPath] = React.useState<string>("/");
3733
const [pathUncommitted, setPathUncommitted] = React.useState<string>("/");
3834
const [files, setFiles] = React.useState<FileStatExtended[]>([]);
39-
const [loading, setLoading] = React.useState<boolean>(false);
35+
const [loading, setLoading] = React.useState<string>("Loading");
4036
const [downloadProgress, setDownloadProgress] = React.useState(0);
4137
const [showSettingsModal, setShowSettingsModal] = React.useState(false);
38+
const [refreshFlag, setRefreshFlag] = React.useState(false);
4239

4340
const [settingsData, setSettingsData] = React.useState<SettingsObject>({
44-
url: "http://localhost:7086/webdav",
45-
username: "flydav",
46-
password: "pass-for-testing"
41+
url: "",
42+
username: "",
43+
password: ""
4744
})
4845

46+
const [client, setClient] = React.useState<WebDAVClient | null>(null)
47+
4948
// try load settings from localStorage
5049
React.useEffect(() => {
51-
const settings = localStorage.getItem("settings")
52-
if (settings) {
53-
setSettingsData(JSON.parse(settings))
50+
const settingsStr = localStorage.getItem("settings")
51+
if (settingsStr) {
52+
let settings = JSON.parse(settingsStr)
53+
setSettingsData(settings)
54+
console.log("loaded settings from localStorage: ", settingsData)
55+
} else {
56+
console.log("no settings in localStorage")
57+
setShowSettingsModal(true)
5458
}
5559
}, [])
5660

5761
React.useEffect(() => {
58-
setLoading(true);
62+
if (settingsData.url == "") return;
63+
setClient(createClient(settingsData.url, {
64+
username: settingsData.username,
65+
password: settingsData.password,
66+
authType: AuthType.Password
67+
}))
68+
}, [settingsData])
69+
70+
React.useEffect(() => {
71+
setRefreshFlag(false);
72+
setLoading("Loading");
5973
setFiles([])
6074
pathUncommitted != path && setPathUncommitted(path);
61-
client.getDirectoryContents(path).then((files: FileStat[] | ResponseDataDetailed<FileStat[]>) => {
75+
console.log("client: ", client);
76+
77+
client?.getDirectoryContents(path).then((files: FileStat[] | ResponseDataDetailed<FileStat[]>) => {
6278
// if is ResponseDataDetailed, unwrap
6379
if (!Array.isArray(files)) {
6480
alert("Error: unexpected response type")
6581
}
82+
console.log("files: ", files);
83+
6684
let filesUnwrapped = files as FileStatExtended[];
6785

6886
filesUnwrapped.map(f => {
@@ -72,17 +90,17 @@ const App = (): JSX.Element => {
7290
setFiles(filesUnwrapped);
7391
}).catch(r => {
7492
console.log(r);
75-
if(r.status == 404) {
76-
alert("not found");
93+
if (r.status == 404) {
7794
setPath("/")
95+
setLoading("Not found. Check settings.");
7896
}
7997
}).finally(() => {
80-
setLoading(false);
98+
setLoading("");
8199
});
82-
}, [path]);
100+
}, [path, client, refreshFlag]);
83101

84102
async function handleClickFile(file: FileStat) {
85-
const buff: Buffer = await client.getFileContents(file.filename, {
103+
const buff: Buffer = await client?.getFileContents(file.filename, {
86104
format: "binary",
87105
onDownloadProgress: e => {
88106
setDownloadProgress(e.loaded / e.total * 100)
@@ -142,6 +160,9 @@ const App = (): JSX.Element => {
142160
<button className={sharedStyles.buttonPrimary} onClick={() => setPath(
143161
pathUncommitted
144162
)}>Go</button>
163+
<span className="mr-2"></span>
164+
<button className={sharedStyles.buttonDefault} onClick={() => { setRefreshFlag(true) }} >Refresh</button>
165+
145166
</div>
146167
</section>
147168
<section className="p-4">
@@ -157,7 +178,7 @@ const App = (): JSX.Element => {
157178
{
158179
files.filter(file => file.type == "directory").map((file: FileStatExtended, idx, arr) => {
159180
return (
160-
<div className={styles.fileListItem}>
181+
<div className={styles.fileListItem} key={file.filename}>
161182
<div className={styles.fileListCell}>
162183
<svg className={styles.fileListIcon} xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="#e79f2d" viewBox="0 0 16 16">
163184
<path d="M9.828 3h3.982a2 2 0 0 1 1.992 2.181l-.637 7A2 2 0 0 1 13.174 14H2.825a2 2 0 0 1-1.991-1.819l-.637-7a1.99 1.99 0 0 1 .342-1.31L.5 3a2 2 0 0 1 2-2h3.672a2 2 0 0 1 1.414.586l.828.828A2 2 0 0 0 9.828 3zm-8.322.12C1.72 3.042 1.95 3 2.19 3h5.396l-.707-.707A1 1 0 0 0 6.172 2H2.5a1 1 0 0 0-1 .981l.006.139z" />
@@ -174,7 +195,7 @@ const App = (): JSX.Element => {
174195
{
175196
files.filter(file => file.type == "file").map((file: FileStat, idx, arr) => {
176197
return (
177-
<div className={styles.fileListItem}>
198+
<div className={styles.fileListItem} key={file.filename}>
178199
<div className={styles.fileListCell}>
179200
<svg className={styles.fileListIcon} xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" viewBox="0 0 16 16">
180201
<path d="M4 0a2 2 0 0 0-2 2v12a2 2 0 0 0 2 2h8a2 2 0 0 0 2-2V2a2 2 0 0 0-2-2H4zm0 1h8a1 1 0 0 1 1 1v12a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V2a1 1 0 0 1 1-1z" />

0 commit comments

Comments
 (0)