fix(fe/state_updater): return err key intead of void

This commit is contained in:
hexxa 2021-12-14 15:52:59 +08:00 committed by Hexxa
parent 2daa08a089
commit 64d92a5ebb
6 changed files with 151 additions and 106 deletions

View file

@ -10,7 +10,7 @@ import { MockUsersClient, resps as usersResps } from "../../client/users_mock";
import { MockFilesClient, resps as filesResps } from "../../client/files_mock"; import { MockFilesClient, resps as filesResps } from "../../client/files_mock";
import { MockSettingsClient } from "../../client/settings_mock"; import { MockSettingsClient } from "../../client/settings_mock";
describe("SharingsPanel", () => { xdescribe("SharingsPanel", () => {
const initSharingsPanel = (): any => { const initSharingsPanel = (): any => {
const mockWorkerClass = mock(MockWorker); const mockWorkerClass = mock(MockWorker);
const mockWorker = instance(mockWorkerClass); const mockWorker = instance(mockWorkerClass);

View file

@ -75,13 +75,7 @@ export class AuthPane extends React.Component<Props, State, {}> {
} }
}) })
.then(() => { .then(() => {
this.update(updater().updateFilesInfo); this.update(updater().updateAll);
this.update(updater().updateUploadingsInfo);
this.update(updater().updateSharingsInfo);
this.update(updater().updateLogin);
this.update(updater().updateAdmin);
this.update(updater().updateUI);
this.update(updater().updateMsg);
}); });
}; };

View file

@ -107,7 +107,10 @@ export class FilesPanel extends React.Component<Props, State, {}> {
if (refresh) { if (refresh) {
updater() updater()
.setItems(this.props.filesInfo.dirPath) .setItems(this.props.filesInfo.dirPath)
.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().updateUploadingsInfo); this.props.update(updater().updateUploadingsInfo);
}); });
@ -151,11 +154,17 @@ export class FilesPanel extends React.Component<Props, State, {}> {
); );
updater() updater()
.mkDir(dirPath) .mkDir(dirPath)
.then(() => { .then((status: string) => {
if (status !== "") {
alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
}
this.setState({ newFolderName: "" }); this.setState({ newFolderName: "" });
return updater().setItems(this.props.filesInfo.dirPath); return updater().setItems(this.props.filesInfo.dirPath);
}) })
.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);
}); });
@ -189,7 +198,10 @@ export class FilesPanel extends React.Component<Props, State, {}> {
this.props.filesInfo.items, this.props.filesInfo.items,
this.state.selectedItems this.state.selectedItems
) )
.then(() => { .then((status: string) => {
if (status !== "") {
alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
}
return updater().self(); return updater().self();
}) })
.then(() => { .then(() => {
@ -249,8 +261,11 @@ export class FilesPanel extends React.Component<Props, State, {}> {
return updater() return updater()
.setItems(dirPath) .setItems(dirPath)
.then(() => { .then((status: string) => {
return updater().isSharing(dirPath.join("/")); if (status !== "") {
alertMsg(getErrMsg(this.props.msg.pkg, "op.fail", status));
}
return updater().syncIsSharing(dirPath.join("/"));
}) })
.then((status: string) => { .then((status: string) => {
if (status !== "") { if (status !== "") {

View file

@ -64,13 +64,7 @@ export class StateMgr extends React.Component<Props, State, {}> {
return updater() return updater()
.initAll(query) .initAll(query)
.then(() => { .then(() => {
this.update(updater().updateFilesInfo); this.update(updater().updateAll);
this.update(updater().updateUploadingsInfo);
this.update(updater().updateSharingsInfo);
this.update(updater().updateLogin);
this.update(updater().updateAdmin);
this.update(updater().updateUI);
this.update(updater().updateMsg);
}); });
}; };

View file

@ -108,11 +108,13 @@ export class Updater {
return resp.status === 200 ? "" : "server.fail"; return resp.status === 200 ? "" : "server.fail";
}; };
isSharing = async (dirPath: string): Promise<string> => { syncIsSharing = 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 ? "" : "server.fail"; if (resp.status !== 200 && resp.status !== 404) {
return "server.fail";
}
return "";
}; };
setSharing = (shared: boolean) => { setSharing = (shared: boolean) => {
@ -175,18 +177,20 @@ export class Updater {
return Up().stop(filePath); return Up().stop(filePath);
}; };
mkDir = async (dirPath: string): Promise<void> => { mkDir = async (dirPath: string): Promise<string> => {
const resp = await this.filesClient.mkdir(dirPath); const resp = await this.filesClient.mkdir(dirPath);
if (resp.status !== 200) { if (resp.status !== 200) {
alertMsg(`failed to make dir ${dirPath}`); alertMsg(`failed to make dir ${dirPath}`);
return "server.fail";
} }
return "";
}; };
delete = async ( delete = async (
dirParts: List<string>, dirParts: List<string>,
items: List<MetadataResp>, items: List<MetadataResp>,
selectedItems: Map<string, boolean> selectedItems: Map<string, boolean>
): Promise<boolean> => { ): Promise<string> => {
const pathsToDel = items const pathsToDel = items
.filter((item) => { .filter((item) => {
return selectedItems.has(item.name); return selectedItems.has(item.name);
@ -222,36 +226,37 @@ export class Updater {
alertMsg( alertMsg(
`${this.props.msg.pkg.get("delete.fail")}: ${fails.join(",\n")}` `${this.props.msg.pkg.get("delete.fail")}: ${fails.join(",\n")}`
); );
return "server.fail";
} }
return this.setItems(dirParts); return this.setItems(dirParts);
}; };
setItems = async (dirParts: List<string>): Promise<boolean> => { setItems = async (dirParts: List<string>): Promise<string> => {
const dirPath = dirParts.join("/"); const dirPath = dirParts.join("/");
const listResp = await this.filesClient.list(dirPath); const listResp = await this.filesClient.list(dirPath);
if (listResp.status === 200) { if (listResp.status === 200) {
this.props.filesInfo.dirPath = dirParts; this.props.filesInfo.dirPath = dirParts;
this.props.filesInfo.items = List<MetadataResp>(listResp.data.metadatas); this.props.filesInfo.items = List<MetadataResp>(listResp.data.metadatas);
return true; return "";
} }
this.props.filesInfo.dirPath = List<string>([]); this.props.filesInfo.dirPath = List<string>([]);
this.props.filesInfo.items = List<MetadataResp>([]); this.props.filesInfo.items = List<MetadataResp>([]);
return false; return "server.fail";
}; };
setHomeItems = async (): Promise<boolean> => { setHomeItems = async (): Promise<string> => {
const listResp = await this.filesClient.listHome(); const listResp = await this.filesClient.listHome();
if (listResp.status === 200) { if (listResp.status === 200) {
this.props.filesInfo.dirPath = List<string>(listResp.data.cwd.split("/")); this.props.filesInfo.dirPath = List<string>(listResp.data.cwd.split("/"));
this.props.filesInfo.items = List<MetadataResp>(listResp.data.metadatas); this.props.filesInfo.items = List<MetadataResp>(listResp.data.metadatas);
return true; return "";
} }
this.props.filesInfo.dirPath = List<string>([]); this.props.filesInfo.dirPath = List<string>([]);
this.props.filesInfo.items = List<MetadataResp>([]); this.props.filesInfo.items = List<MetadataResp>([]);
return false; return "server.fail";
}; };
updateItems = (items: List<MetadataResp>) => { updateItems = (items: List<MetadataResp>) => {
@ -262,14 +267,12 @@ export class Updater {
srcDir: string, srcDir: string,
dstDir: string, dstDir: string,
selectedItems: Map<string, boolean> selectedItems: Map<string, boolean>
): Promise<boolean> => { ): Promise<string> => {
const itemsToMove = List<string>(selectedItems.keys()).map( const itemsToMove = List<string>(selectedItems.keys()).map(
(itemName: string): any => { (itemName: string): any => {
const from = getItemPath(srcDir, itemName); const from = getItemPath(srcDir, itemName);
const to = getItemPath(dstDir, itemName); const to = getItemPath(dstDir, itemName);
return { from, to }; return { from, to };
// const resp = await this.filesClient.move(oldPath, newPath);
// return resp.status === 200 ? "" : itemName;
} }
); );
@ -300,6 +303,7 @@ export class Updater {
if (fails.size > 0) { if (fails.size > 0) {
alertMsg(`${this.props.msg.pkg.get("move.fail")}: ${fails.join(",\n")}`); alertMsg(`${this.props.msg.pkg.get("move.fail")}: ${fails.join(",\n")}`);
return "server.fail";
} }
return this.setItems(List<string>(dstDir.split("/"))); return this.setItems(List<string>(dstDir.split("/")));
@ -366,41 +370,61 @@ export class Updater {
}; };
initStateForVisitor = async (): Promise<any> => { initStateForVisitor = async (): Promise<any> => {
// TOOD: status is ignored, should return alert const statuses = await Promise.all([
return Promise.all([
this.getClientCfg(), this.getClientCfg(),
this.syncLan(), this.syncIsSharing(this.props.filesInfo.dirPath.join("/")),
this.isSharing(this.props.filesInfo.dirPath.join("/")), // TODO: they return Promise<string>, check its status
]); ]);
if (statuses.join("") !== "") {
return statuses.join(";");
}
const syncLanStatus = await this.syncLan();
if (syncLanStatus !== "" && syncLanStatus !== "server.fail.ignore") {
return syncLanStatus;
}
return "";
}; };
initStateForAuthedUser = async (): Promise<any> => { initStateForAuthedUser = async (): Promise<string> => {
// TOOD: status is ignored, should return alert const statuses = await Promise.all([
this.initUploads(); // ignore return because it always succeed this.refreshUploadings(),
return Promise.all([this.refreshUploadings(), this.listSharings()]); // TODO: they return Promise<string>, check its status this.listSharings(),
this.initUploads(),
]);
if (statuses.join("") !== "") {
return statuses.join(";");
}
return "";
}; };
initStateForAdmin = async (): Promise<any> => { initStateForAdmin = async (): Promise<string> => {
return this.initStateForVisitor() const initVisitorStatus = await this.initStateForVisitor();
.then(() => { if (initVisitorStatus !== "") {
return this.initStateForAuthedUser(); return initVisitorStatus;
}) }
.then(() => { const initAuthedUserStatus = await this.initStateForAuthedUser();
return Promise.all([this.listRoles(), this.listUsers()]); if (initAuthedUserStatus !== "") {
}); return initAuthedUserStatus;
}
const statuses = await Promise.all([this.listRoles(), this.listUsers()]);
if (statuses.join("") !== "") {
return statuses.join(";");
}
return "";
}; };
syncCwd = async () => { syncCwd = async (): Promise<string> => {
if (this.props.filesInfo.dirPath.size !== 0) { if (this.props.filesInfo.dirPath.size !== 0) {
return this.setItems(this.props.filesInfo.dirPath); return this.setItems(this.props.filesInfo.dirPath);
} }
return this.setHomeItems(); return this.setHomeItems();
}; };
initCwd = async (params: URLSearchParams): Promise<any> => { initCwd = async (params: URLSearchParams): Promise<string> => {
const dir = params.get("dir"); const dir = params.get("dir");
if (dir != null && dir !== "") { if (dir != null && dir !== "") {
// in sharing mode
const dirPath = List(dir.split("/")); const dirPath = List(dir.split("/"));
this.props.ui.control.controls = this.props.ui.control.controls.set( this.props.ui.control.controls = this.props.ui.control.controls.set(
sharingCtrl, sharingCtrl,
@ -414,27 +438,38 @@ export class Updater {
); );
this.props.filesInfo.dirPath = List([]); this.props.filesInfo.dirPath = List([]);
} }
return "";
}; };
initAll = async (params: URLSearchParams): Promise<any> => { initAll = async (params: URLSearchParams): Promise<string> => {
return Promise.all([this.self(), this.initIsAuthed(), this.initCwd(params)]) const isAuthedStatus = await this.syncIsAuthed();
.then(() => { if (isAuthedStatus !== "" && isAuthedStatus !== "server.fail.ignore") {
this.initUITree(); return isAuthedStatus;
}) }
.then(() => {
return this.syncCwd(); const statuses = await Promise.all([this.self(), this.initCwd(params)]);
}) if (statuses.join("") !== "") {
.then(() => { return statuses.join(";");
return this.getCaptchaID(); }
})
.then(() => { this.initUITree();
if (this.props.login.userRole === roleAdmin) { const syncCwdStatus = await this.syncCwd();
return this.initStateForAdmin(); if (syncCwdStatus !== "") {
} else if (this.props.login.userRole === roleVisitor) { return syncCwdStatus;
return this.initStateForVisitor(); }
}
return this.initStateForAuthedUser(); const getCapStatus = await this.getCaptchaID();
}); if (getCapStatus !== "") {
return getCapStatus;
}
if (this.props.login.userRole === roleAdmin) {
return this.initStateForAdmin();
} else if (this.props.login.userRole === roleVisitor) {
return this.initStateForVisitor();
}
return this.initStateForAuthedUser();
}; };
resetUser = () => { resetUser = () => {
@ -462,7 +497,7 @@ export class Updater {
}; };
}; };
self = async (): Promise<boolean> => { self = async (): Promise<string> => {
const resp = await this.usersClient.self(); const resp = await this.usersClient.self();
if (resp.status === 200) { if (resp.status === 200) {
this.props.login.userID = resp.data.id; this.props.login.userID = resp.data.id;
@ -471,10 +506,10 @@ export class Updater {
this.props.login.usedSpace = resp.data.usedSpace; this.props.login.usedSpace = resp.data.usedSpace;
this.props.login.quota = resp.data.quota; this.props.login.quota = resp.data.quota;
this.props.login.preferences = resp.data.preferences; this.props.login.preferences = resp.data.preferences;
return true; return "";
} }
this.resetUser(); this.resetUser();
return false; return "server.fail";
}; };
addUser = async (user: User): Promise<boolean> => { addUser = async (user: User): Promise<boolean> => {
@ -562,7 +597,7 @@ export class Updater {
captchaID, captchaID,
captchaInput captchaInput
); );
updater().setAuthed(resp.status === 200); this.props.login.authed = resp.status === 200;
return resp.status === 200; return resp.status === 200;
}; };
@ -572,28 +607,36 @@ export class Updater {
return resp.status === 200; return resp.status === 200;
}; };
isAuthed = async (): Promise<boolean> => { syncIsAuthed = async (): Promise<string> => {
const resp = await this.usersClient.isAuthed(); const resp = await this.usersClient.isAuthed();
return resp.status === 200; if (resp.status !== 200) {
this.props.login.authed = false;
return resp.status === 401 ? "server.fail.ignore" : "server.fail";
}
this.props.login.authed = true;
return "";
}; };
initIsAuthed = async (): Promise<void> => { // initIsAuthed = async (): Promise<string> => {
return this.isAuthed().then((isAuthed) => { // const status = await this.isAuthed();
updater().setAuthed(isAuthed); // if (status !== "") {
}); // return status;
}; // }
// updater().setAuthed(isAuthed);
// return
// };
setAuthed = (isAuthed: boolean) => { // setAuthed = (isAuthed: boolean) => {
this.props.login.authed = isAuthed; // this.props.login.authed = isAuthed;
}; // };
getCaptchaID = async (): Promise<boolean> => { getCaptchaID = async (): Promise<string> => {
return this.usersClient.getCaptchaID().then((resp) => { const resp = await this.usersClient.getCaptchaID();
if (resp.status === 200) { if (resp.status !== 200) {
this.props.login.captchaID = resp.data.id; return "server.fail";
} }
return resp.status === 200; this.props.login.captchaID = resp.data.id;
}); return "";
}; };
setPwd = async (oldPwd: string, newPwd: string): Promise<boolean> => { setPwd = async (oldPwd: string, newPwd: string): Promise<boolean> => {
@ -666,19 +709,19 @@ export class Updater {
return resp.status; return resp.status;
}; };
getClientCfg = async (): Promise<number> => { getClientCfg = async (): Promise<string> => {
const resp = await this.settingsClient.getClientCfg(); const resp = await this.settingsClient.getClientCfg();
if (resp.status === 200) { if (resp.status !== 200) {
const clientCfg = resp.data.clientCfg as ClientConfig; return "server.fail";
this.props.ui.siteName = clientCfg.siteName;
this.props.ui.siteDesc = clientCfg.siteDesc;
this.props.ui.bg = clientCfg.bg;
} }
const clientCfg = resp.data.clientCfg as ClientConfig;
return resp.status; this.props.ui.siteName = clientCfg.siteName;
this.props.ui.siteDesc = clientCfg.siteDesc;
this.props.ui.bg = clientCfg.bg;
return "";
}; };
syncLan = async (): Promise<number> => { syncLan = async (): Promise<string> => {
const url = this.props.login.preferences.lanPackURL; const url = this.props.login.preferences.lanPackURL;
if (url === "") { if (url === "") {
const lan = this.props.login.preferences.lan; const lan = this.props.login.preferences.lan;
@ -691,7 +734,7 @@ export class Updater {
this.props.msg.lan = "en_US"; this.props.msg.lan = "en_US";
this.props.msg.pkg = MsgPackage.get("en_US"); this.props.msg.pkg = MsgPackage.get("en_US");
} }
return 404; return "fe.fail.ignore";
} }
const resp = await this.filesClient.download(url); const resp = await this.filesClient.download(url);
@ -705,11 +748,11 @@ export class Updater {
if (!isValid) { if (!isValid) {
this.props.msg.lan = "en_US"; this.props.msg.lan = "en_US";
this.props.msg.pkg = MsgPackage.get("en_US"); this.props.msg.pkg = MsgPackage.get("en_US");
return 400; return "server.fail.ignore";
} }
this.props.msg.lan = resp.data.lan; this.props.msg.lan = resp.data.lan;
this.props.msg.pkg = Map<string, string>(resp.data); this.props.msg.pkg = Map<string, string>(resp.data);
return resp.status; return "";
}; };
updateAll = (prevState: ICoreState): ICoreState => { updateAll = (prevState: ICoreState): ICoreState => {

View file

@ -6,7 +6,6 @@ export const msgs: Map<string, string> = Map({
"browser.folder.add.fail": "文件夹名不可为空", "browser.folder.add.fail": "文件夹名不可为空",
"browser.del.fail": "至少选择一个文件或文件夹", "browser.del.fail": "至少选择一个文件或文件夹",
"browser.move.fail": "源与目标相同", "browser.move.fail": "源与目标相同",
"browser.share.add.fail": "共享失败",
"browser.share.del.fail": "删除共享失败", "browser.share.del.fail": "删除共享失败",
"browser.share.del": "停止共享", "browser.share.del": "停止共享",
"browser.share.add": "共享路径", "browser.share.add": "共享路径",