|
|
|
@ -10,61 +10,50 @@ function loginStatusRequest(): SocketMessage { |
|
|
|
return "LoginStatusRequest"; |
|
|
|
} |
|
|
|
|
|
|
|
// TODO: Adapt to new response format.
|
|
|
|
function handleLoginStatusResponse(dispatch: AppDispatch, outerFields: any[]) { |
|
|
|
if (outerFields.length !== 1) { |
|
|
|
console.log("LoginStatusResponse has wrong number of fields:", outerFields); |
|
|
|
function handleLoginStatusResponse(dispatch: AppDispatch, response: any) { |
|
|
|
if ("Pending" in response) { |
|
|
|
// TODO: Adapt to new response format.
|
|
|
|
const [username] = response["Pending"]; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Pending, |
|
|
|
username, |
|
|
|
}) |
|
|
|
); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
const { variant, fields } = outerFields[0]; |
|
|
|
switch (variant) { |
|
|
|
case "Pending": { |
|
|
|
const [username] = fields; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Pending, |
|
|
|
username, |
|
|
|
}) |
|
|
|
); |
|
|
|
break; |
|
|
|
} |
|
|
|
case "Success": { |
|
|
|
const [username, motd] = fields; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Success, |
|
|
|
username, |
|
|
|
motd, |
|
|
|
}) |
|
|
|
); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
case "Failure": { |
|
|
|
const [username, reason] = fields; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Failure, |
|
|
|
username, |
|
|
|
reason, |
|
|
|
}) |
|
|
|
); |
|
|
|
break; |
|
|
|
} |
|
|
|
if ("Success" in response) { |
|
|
|
const { user_name, motd } = response["Success"]; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Success, |
|
|
|
username: user_name, |
|
|
|
motd, |
|
|
|
}) |
|
|
|
); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
default: |
|
|
|
console.log("LoginStatusResponse field has wrong shape:", outerFields[0]); |
|
|
|
break; |
|
|
|
if ("Failure" in response) { |
|
|
|
const { user_name, reason } = response["Failure"]; |
|
|
|
dispatch( |
|
|
|
loginSetState({ |
|
|
|
status: LoginStatus.Failure, |
|
|
|
username: user_name, |
|
|
|
reason, |
|
|
|
}) |
|
|
|
); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
console.log("LoginStatusResponse field has wrong shape:", response); |
|
|
|
} |
|
|
|
|
|
|
|
export const loginSocketMessageMiddleware: SocketMessageMiddleware = { |
|
|
|
handleMessage: (dispatch, { variant, fields }) => { |
|
|
|
switch (variant) { |
|
|
|
case "LoginStatusResponse": |
|
|
|
handleLoginStatusResponse(dispatch, fields); |
|
|
|
break; |
|
|
|
handleMessage: (dispatch, message) => { |
|
|
|
if ("LoginStatusResponse" in message) { |
|
|
|
handleLoginStatusResponse(dispatch, message["LoginStatusResponse"]); |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
|