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 () => {
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 { 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<Props, State, {}> {
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<Props, State, {}> {
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<Props, State, {}> {
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<Props, State, {}> {
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);
});

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 { 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<Props, 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) => {
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);
});
};

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 { 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<Props, State, {}> {
.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(() => {

View file

@ -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<boolean> => {
addSharing = async (): Promise<string> => {
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<boolean> => {
deleteSharing = async (dirPath: string): Promise<string> => {
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);
// 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<boolean> => {
listSharings = async (): Promise<string> => {
const resp = await this.filesClient.listSharings();
this.props.sharingsInfo.sharings =
resp.status === 200
? List<string>(resp.data.sharingDirs)
: this.props.sharingsInfo.sharings;
return resp.status === 200;
return resp.status === 200 ? "" : "server.fail";
};
refreshUploadings = async (): Promise<boolean> => {
// 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<string> => {
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<string, UploadEntry>([]);
@ -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<void> => {
@ -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<string>, check its status
]);
};
initStateForAuthedUser = async (): Promise<any> => {
// 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<string>, check its status
};
initStateForAdmin = async (): Promise<any> => {

View file

@ -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);