fix(fe/state_updater): return status instead of boolean

This commit is contained in:
hexxa 2021-12-13 15:28:54 +08:00 committed by Hexxa
parent e23fc0a2d9
commit 2daa08a089
6 changed files with 59 additions and 93 deletions

View file

@ -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 () => { test("delete sharing", async () => {
const { sharingsPanel, usersCl, filesCl } = initSharingsPanel(); const { sharingsPanel, usersCl, filesCl } = initSharingsPanel();

View file

@ -9,6 +9,7 @@ import { RiFile2Fill } from "@react-icons/all-files/ri/RiFile2Fill";
import { RiFileList2Fill } from "@react-icons/all-files/ri/RiFileList2Fill"; import { RiFileList2Fill } from "@react-icons/all-files/ri/RiFileList2Fill";
import { alertMsg, confirmMsg } from "../common/env"; import { alertMsg, confirmMsg } from "../common/env";
import { getErrMsg } from "../common/utils";
import { updater } from "./state_updater"; import { updater } from "./state_updater";
import { ICoreState, MsgProps, UIProps } from "./core_state"; import { ICoreState, MsgProps, UIProps } from "./core_state";
import { LoginProps } from "./pane_login"; import { LoginProps } from "./pane_login";
@ -129,7 +130,7 @@ export class FilesPanel extends React.Component<Props, State, {}> {
const status = updater().addUploads(fileList); const status = updater().addUploads(fileList);
if (status !== "") { 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); this.props.update(updater().updateUploadingsInfo);
}; };
@ -248,13 +249,13 @@ export class FilesPanel extends React.Component<Props, State, {}> {
return updater() return updater()
.setItems(dirPath) .setItems(dirPath)
.then(() => {
return updater().listSharings();
})
.then(() => { .then(() => {
return updater().isSharing(dirPath.join("/")); 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().updateFilesInfo);
this.props.update(updater().updateSharingsInfo); this.props.update(updater().updateSharingsInfo);
}); });
@ -305,15 +306,19 @@ export class FilesPanel extends React.Component<Props, State, {}> {
addSharing = async () => { addSharing = async () => {
return updater() return updater()
.addSharing() .addSharing()
.then((ok) => { .then((status: string) => {
if (!ok) { if (status !== "") {
alertMsg(this.props.msg.pkg.get("browser.share.add.fail")); alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
return "";
} else { } else {
updater().setSharing(true); updater().setSharing(true);
return updater().listSharings(); 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().updateSharingsInfo);
this.props.update(updater().updateFilesInfo); this.props.update(updater().updateFilesInfo);
}); });
@ -322,15 +327,19 @@ export class FilesPanel extends React.Component<Props, State, {}> {
deleteSharing = async (dirPath: string) => { deleteSharing = async (dirPath: string) => {
return updater() return updater()
.deleteSharing(dirPath) .deleteSharing(dirPath)
.then((ok) => { .then((status) => {
if (!ok) { if (status !== "") {
alertMsg(this.props.msg.pkg.get("browser.share.del.fail")); alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
return "";
} else { } else {
updater().setSharing(false); updater().setSharing(false);
return updater().listSharings(); 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().updateSharingsInfo);
this.props.update(updater().updateFilesInfo); this.props.update(updater().updateFilesInfo);
}); });

View file

@ -5,6 +5,7 @@ import { RiShareBoxLine } from "@react-icons/all-files/ri/RiShareBoxLine";
import { RiFolderSharedFill } from "@react-icons/all-files/ri/RiFolderSharedFill"; import { RiFolderSharedFill } from "@react-icons/all-files/ri/RiFolderSharedFill";
import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine"; import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine";
import { getErrMsg } from "../common/utils";
import { alertMsg, confirmMsg } from "../common/env"; import { alertMsg, confirmMsg } from "../common/env";
import { updater } from "./state_updater"; import { updater } from "./state_updater";
import { ICoreState, MsgProps, UIProps } from "./core_state"; import { ICoreState, MsgProps, UIProps } from "./core_state";
@ -33,48 +34,28 @@ export class SharingsPanel extends React.Component<Props, State, {}> {
this.state = {}; 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) => { deleteSharing = async (dirPath: string) => {
return updater() return updater()
.deleteSharing(dirPath) .deleteSharing(dirPath)
.then((ok) => { .then((status: string) => {
if (!ok) { if (status !== "") {
alertMsg(this.props.msg.pkg.get("browser.share.del.fail")); alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
} else { } else {
updater().setSharing(false); updater().setSharing(false);
return this.listSharings(); return this.listSharings();
} }
})
.then(() => {
this.props.update(updater().updateFilesInfo);
this.props.update(updater().updateFilesInfo);
}); });
}; };
listSharings = async () => { listSharings = async () => {
return updater() return updater()
.listSharings() .listSharings()
.then((ok) => { .then((status: string) => {
if (ok) { if (status !== "") {
this.props.update(updater().updateFilesInfo); alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
this.props.update(updater().updateSharingsInfo);
} }
this.props.update(updater().updateFilesInfo);
this.props.update(updater().updateSharingsInfo);
}); });
}; };

View file

@ -7,6 +7,7 @@ import { RiUploadCloudLine } from "@react-icons/all-files/ri/RiUploadCloudLine";
import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine"; import { RiEmotionSadLine } from "@react-icons/all-files/ri/RiEmotionSadLine";
import { alertMsg } from "../common/env"; import { alertMsg } from "../common/env";
import { getErrMsg } from "../common/utils";
import { updater } from "./state_updater"; import { updater } from "./state_updater";
import { ICoreState, MsgProps, UIProps } from "./core_state"; import { ICoreState, MsgProps, UIProps } from "./core_state";
import { LoginProps } from "./pane_login"; import { LoginProps } from "./pane_login";
@ -41,14 +42,15 @@ export class UploadingsPanel extends React.Component<Props, State, {}> {
.then((status: string) => { .then((status: string) => {
if (status !== "") { if (status !== "") {
alertMsg( alertMsg(
`${this.props.msg.pkg.get( getErrMsg(this.props.msg.pkg, "browser.upload.del.fail", status)
"browser.upload.del.fail"
)}: ${this.props.msg.pkg.get(status)}`
); );
} }
return updater().refreshUploadings(); return updater().refreshUploadings();
}) })
.then(() => { .then((status: string) => {
if (status !== "") {
alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
}
return updater().self(); return updater().self();
}) })
.then(() => { .then(() => {

View file

@ -70,7 +70,7 @@ export class Updater {
file.name file.name
); );
const status = Up().add(file, filePath); const status = Up().add(file, filePath);
if (status !== ""){ if (status !== "") {
return status; return status;
} }
}); });
@ -97,44 +97,45 @@ export class Updater {
); );
}; };
addSharing = async (): Promise<boolean> => { addSharing = async (): Promise<string> => {
const dirPath = this.props.filesInfo.dirPath.join("/"); const dirPath = this.props.filesInfo.dirPath.join("/");
const resp = await this.filesClient.addSharing(dirPath); const resp = await this.filesClient.addSharing(dirPath);
return resp.status === 200; return resp.status === 200 ? "" : "server.fail";
}; };
deleteSharing = async (dirPath: string): Promise<boolean> => { deleteSharing = async (dirPath: string): Promise<string> => {
const resp = await this.filesClient.deleteSharing(dirPath); const resp = await this.filesClient.deleteSharing(dirPath);
return resp.status === 200; return resp.status === 200 ? "" : "server.fail";
}; };
isSharing = async (dirPath: string): Promise<boolean> => { isSharing = async (dirPath: string): Promise<string> => {
const resp = await this.filesClient.isSharing(dirPath); const resp = await this.filesClient.isSharing(dirPath);
// TODO: differentiate 404 and error
this.props.filesInfo.isSharing = resp.status === 200; 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) => { setSharing = (shared: boolean) => {
this.props.filesInfo.isSharing = shared; this.props.filesInfo.isSharing = shared;
}; };
listSharings = async (): Promise<boolean> => { listSharings = async (): Promise<string> => {
const resp = await this.filesClient.listSharings(); const resp = await this.filesClient.listSharings();
this.props.sharingsInfo.sharings = this.props.sharingsInfo.sharings =
resp.status === 200 resp.status === 200
? List<string>(resp.data.sharingDirs) ? List<string>(resp.data.sharingDirs)
: this.props.sharingsInfo.sharings; : this.props.sharingsInfo.sharings;
return resp.status === 200; return resp.status === 200 ? "" : "server.fail";
}; };
refreshUploadings = async (): Promise<boolean> => { // this function gets information from server and merge them with local information
// this function get information from server and merge them with local information // because some information (error) can only be detected from local
// because some information (error) can only be detected from local refreshUploadings = async (): Promise<string> => {
const luResp = await this.filesClient.listUploadings(); const luResp = await this.filesClient.listUploadings();
if (luResp.status !== 200) { if (luResp.status !== 200) {
// TODO: i18n // TODO: log error
console.error(luResp.data); console.error(luResp.data);
return false; return "server.fail";
} }
let localUploads = Map<string, UploadEntry>([]); let localUploads = Map<string, UploadEntry>([]);
@ -167,11 +168,11 @@ export class Updater {
}); });
this.props.uploadingsInfo.uploadings = updatedUploads; this.props.uploadingsInfo.uploadings = updatedUploads;
return true; return "";
}; };
stopUploading = (filePath: string) => { stopUploading = (filePath: string): string => {
Up().stop(filePath); return Up().stop(filePath);
}; };
mkDir = async (dirPath: string): Promise<void> => { mkDir = async (dirPath: string): Promise<void> => {
@ -369,14 +370,14 @@ export class Updater {
return Promise.all([ return Promise.all([
this.getClientCfg(), this.getClientCfg(),
this.syncLan(), this.syncLan(),
this.isSharing(this.props.filesInfo.dirPath.join("/")), this.isSharing(this.props.filesInfo.dirPath.join("/")), // TODO: they return Promise<string>, check its status
]); ]);
}; };
initStateForAuthedUser = async (): Promise<any> => { initStateForAuthedUser = async (): Promise<any> => {
// TOOD: status is ignored, should return alert // TOOD: status is ignored, should return alert
this.initUploads(); // ignore return because it always succeed 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<string>, check its status
}; };
initStateForAdmin = async (): Promise<any> => { initStateForAdmin = async (): Promise<any> => {

View file

@ -141,7 +141,7 @@ export class UploadMgr {
state: UploadState.Ready, state: UploadState.Ready,
}); });
} else { } else {
status = "op.fail"; status = "uploadMgr.err";
} }
} }
@ -159,7 +159,7 @@ export class UploadMgr {
state: UploadState.Stopped, state: UploadState.Stopped,
}); });
} else { } else {
status = "op.fail"; status = "uploadMgr.err";
} }
this.statusCb(this.infos.toMap(), false); this.statusCb(this.infos.toMap(), false);