diff --git a/src/client/web/src/components/__test__/panel_sharings.test.tsx b/src/client/web/src/components/__test__/panel_sharings.test.tsx index 3457936..b147a5c 100644 --- a/src/client/web/src/components/__test__/panel_sharings.test.tsx +++ b/src/client/web/src/components/__test__/panel_sharings.test.tsx @@ -39,33 +39,6 @@ describe("SharingsPanel", () => { }; }; - test("add sharing", async () => { - const { sharingsPanel, usersCl, filesCl } = initSharingsPanel(); - - const newSharings = [ - "mock_sharingfolder1", - "mock_sharingfolder2", - "newSharing", - ]; - - filesCl.setMock({ - ...filesResps, - listSharingsMockResp: { - status: 200, - statusText: "", - data: { - sharingDirs: newSharings, - }, - }, - }); - - await sharingsPanel.addSharing(); - - // TODO: check addSharing's input - expect(updater().props.filesInfo.isSharing).toEqual(true); - expect(updater().props.sharingsInfo.sharings).toEqual(List(newSharings)); - }); - test("delete sharing", async () => { const { sharingsPanel, usersCl, filesCl } = initSharingsPanel(); diff --git a/src/client/web/src/components/panel_files.tsx b/src/client/web/src/components/panel_files.tsx index 42610a2..7fa4e74 100644 --- a/src/client/web/src/components/panel_files.tsx +++ b/src/client/web/src/components/panel_files.tsx @@ -9,6 +9,7 @@ import { RiFile2Fill } from "@react-icons/all-files/ri/RiFile2Fill"; import { RiFileList2Fill } from "@react-icons/all-files/ri/RiFileList2Fill"; import { alertMsg, confirmMsg } from "../common/env"; +import { getErrMsg } from "../common/utils"; import { updater } from "./state_updater"; import { ICoreState, MsgProps, UIProps } from "./core_state"; import { LoginProps } from "./pane_login"; @@ -129,7 +130,7 @@ export class FilesPanel extends React.Component { const status = updater().addUploads(fileList); if (status !== "") { - alertMsg(this.props.msg.pkg.get("upload.add.fail")); + alertMsg(getErrMsg(this.props.msg.pkg, "upload.add.fail", status)); } this.props.update(updater().updateUploadingsInfo); }; @@ -248,13 +249,13 @@ export class FilesPanel extends React.Component { return updater() .setItems(dirPath) - .then(() => { - return updater().listSharings(); - }) .then(() => { return updater().isSharing(dirPath.join("/")); }) - .then(() => { + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + } this.props.update(updater().updateFilesInfo); this.props.update(updater().updateSharingsInfo); }); @@ -305,15 +306,19 @@ export class FilesPanel extends React.Component { addSharing = async () => { return updater() .addSharing() - .then((ok) => { - if (!ok) { - alertMsg(this.props.msg.pkg.get("browser.share.add.fail")); + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + return ""; } else { updater().setSharing(true); return updater().listSharings(); } }) - .then(() => { + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + } this.props.update(updater().updateSharingsInfo); this.props.update(updater().updateFilesInfo); }); @@ -322,15 +327,19 @@ export class FilesPanel extends React.Component { deleteSharing = async (dirPath: string) => { return updater() .deleteSharing(dirPath) - .then((ok) => { - if (!ok) { - alertMsg(this.props.msg.pkg.get("browser.share.del.fail")); + .then((status) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + return ""; } else { updater().setSharing(false); return updater().listSharings(); } }) - .then(() => { + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + } this.props.update(updater().updateSharingsInfo); this.props.update(updater().updateFilesInfo); }); diff --git a/src/client/web/src/components/panel_sharings.tsx b/src/client/web/src/components/panel_sharings.tsx index a44fe65..d9db0da 100644 --- a/src/client/web/src/components/panel_sharings.tsx +++ b/src/client/web/src/components/panel_sharings.tsx @@ -5,6 +5,7 @@ import { RiShareBoxLine } from "@react-icons/all-files/ri/RiShareBoxLine"; import { RiFolderSharedFill } from "@react-icons/all-files/ri/RiFolderSharedFill"; import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine"; +import { getErrMsg } from "../common/utils"; import { alertMsg, confirmMsg } from "../common/env"; import { updater } from "./state_updater"; import { ICoreState, MsgProps, UIProps } from "./core_state"; @@ -33,48 +34,28 @@ export class SharingsPanel extends React.Component { this.state = {}; } - addSharing = async () => { - return updater() - .addSharing() - .then((ok) => { - if (!ok) { - alertMsg(this.props.msg.pkg.get("browser.share.add.fail")); - } else { - updater().setSharing(true); - return this.listSharings(); - } - }) - .then(() => { - this.props.update(updater().updateFilesInfo); - this.props.update(updater().updateSharingsInfo); - }); - }; - deleteSharing = async (dirPath: string) => { return updater() .deleteSharing(dirPath) - .then((ok) => { - if (!ok) { - alertMsg(this.props.msg.pkg.get("browser.share.del.fail")); + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); } else { updater().setSharing(false); return this.listSharings(); } - }) - .then(() => { - this.props.update(updater().updateFilesInfo); - this.props.update(updater().updateFilesInfo); }); }; listSharings = async () => { return updater() .listSharings() - .then((ok) => { - if (ok) { - this.props.update(updater().updateFilesInfo); - this.props.update(updater().updateSharingsInfo); + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); } + this.props.update(updater().updateFilesInfo); + this.props.update(updater().updateSharingsInfo); }); }; diff --git a/src/client/web/src/components/panel_uploadings.tsx b/src/client/web/src/components/panel_uploadings.tsx index 5475498..348fdf4 100644 --- a/src/client/web/src/components/panel_uploadings.tsx +++ b/src/client/web/src/components/panel_uploadings.tsx @@ -7,6 +7,7 @@ import { RiUploadCloudLine } from "@react-icons/all-files/ri/RiUploadCloudLine"; import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine"; import { alertMsg } from "../common/env"; +import { getErrMsg } from "../common/utils"; import { updater } from "./state_updater"; import { ICoreState, MsgProps, UIProps } from "./core_state"; import { LoginProps } from "./pane_login"; @@ -41,14 +42,15 @@ export class UploadingsPanel extends React.Component { .then((status: string) => { if (status !== "") { alertMsg( - `${this.props.msg.pkg.get( - "browser.upload.del.fail" - )}: ${this.props.msg.pkg.get(status)}` + getErrMsg(this.props.msg.pkg, "browser.upload.del.fail", status) ); } return updater().refreshUploadings(); }) - .then(() => { + .then((status: string) => { + if (status !== "") { + alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status)); + } return updater().self(); }) .then(() => { diff --git a/src/client/web/src/components/state_updater.ts b/src/client/web/src/components/state_updater.ts index 7adea2e..304a1b4 100644 --- a/src/client/web/src/components/state_updater.ts +++ b/src/client/web/src/components/state_updater.ts @@ -70,7 +70,7 @@ export class Updater { file.name ); const status = Up().add(file, filePath); - if (status !== ""){ + if (status !== "") { return status; } }); @@ -97,44 +97,45 @@ export class Updater { ); }; - addSharing = async (): Promise => { + addSharing = async (): Promise => { const dirPath = this.props.filesInfo.dirPath.join("/"); const resp = await this.filesClient.addSharing(dirPath); - return resp.status === 200; + return resp.status === 200 ? "" : "server.fail"; }; - deleteSharing = async (dirPath: string): Promise => { + deleteSharing = async (dirPath: string): Promise => { const resp = await this.filesClient.deleteSharing(dirPath); - return resp.status === 200; + return resp.status === 200 ? "" : "server.fail"; }; - isSharing = async (dirPath: string): Promise => { + isSharing = async (dirPath: string): Promise => { const resp = await this.filesClient.isSharing(dirPath); + // TODO: differentiate 404 and error this.props.filesInfo.isSharing = resp.status === 200; - return resp.status === 200; // TODO: differentiate 404 and error + return resp.status === 200 ? "" : "server.fail"; }; setSharing = (shared: boolean) => { this.props.filesInfo.isSharing = shared; }; - listSharings = async (): Promise => { + listSharings = async (): Promise => { const resp = await this.filesClient.listSharings(); this.props.sharingsInfo.sharings = resp.status === 200 ? List(resp.data.sharingDirs) : this.props.sharingsInfo.sharings; - return resp.status === 200; + return resp.status === 200 ? "" : "server.fail"; }; - refreshUploadings = async (): Promise => { - // this function get information from server and merge them with local information - // because some information (error) can only be detected from local + // this function gets information from server and merge them with local information + // because some information (error) can only be detected from local + refreshUploadings = async (): Promise => { const luResp = await this.filesClient.listUploadings(); if (luResp.status !== 200) { - // TODO: i18n + // TODO: log error console.error(luResp.data); - return false; + return "server.fail"; } let localUploads = Map([]); @@ -167,11 +168,11 @@ export class Updater { }); this.props.uploadingsInfo.uploadings = updatedUploads; - return true; + return ""; }; - stopUploading = (filePath: string) => { - Up().stop(filePath); + stopUploading = (filePath: string): string => { + return Up().stop(filePath); }; mkDir = async (dirPath: string): Promise => { @@ -369,14 +370,14 @@ export class Updater { return Promise.all([ this.getClientCfg(), this.syncLan(), - this.isSharing(this.props.filesInfo.dirPath.join("/")), + this.isSharing(this.props.filesInfo.dirPath.join("/")), // TODO: they return Promise, check its status ]); }; initStateForAuthedUser = async (): Promise => { // TOOD: status is ignored, should return alert this.initUploads(); // ignore return because it always succeed - return Promise.all([this.refreshUploadings(), this.listSharings()]); + return Promise.all([this.refreshUploadings(), this.listSharings()]); // TODO: they return Promise, check its status }; initStateForAdmin = async (): Promise => { diff --git a/src/client/web/src/worker/upload_mgr.ts b/src/client/web/src/worker/upload_mgr.ts index c4af6db..cbc6de1 100644 --- a/src/client/web/src/worker/upload_mgr.ts +++ b/src/client/web/src/worker/upload_mgr.ts @@ -141,7 +141,7 @@ export class UploadMgr { state: UploadState.Ready, }); } else { - status = "op.fail"; + status = "uploadMgr.err"; } } @@ -159,7 +159,7 @@ export class UploadMgr { state: UploadState.Stopped, }); } else { - status = "op.fail"; + status = "uploadMgr.err"; } this.statusCb(this.infos.toMap(), false);