Fix redirect to login if no session. Cleanup client error handling.

This commit is contained in:
Chen-I Lim 2021-01-14 09:34:08 -08:00
parent ef390d9653
commit d3eed6308a
3 changed files with 51 additions and 34 deletions

View file

@ -23,7 +23,7 @@ import BoardPage from './pages/boardPage'
type State = {
language: string,
user: IUser|null,
user?: IUser
initialLoad: boolean,
}
@ -32,13 +32,12 @@ export default class App extends React.PureComponent<unknown, State> {
super(props)
this.state = {
language: getCurrentLanguage(),
user: null,
initialLoad: false,
}
}
public componentDidMount(): void {
client.getMe().then((user: IUser|null) => {
client.getMe().then((user?: IUser) => {
this.setState({user, initialLoad: true})
})
}

View file

@ -32,15 +32,16 @@ class OctoClient {
headers: this.headers(),
body,
})
if (response.status === 200) {
const responseJson = (await response.json() || {}) as {token?: string}
this.token = responseJson.token
if (responseJson.token !== '') {
localStorage.setItem('sessionId', this.token || '')
return true
}
if (response.status !== 200) {
return false
}
const responseJson = (await response.json() || {}) as {token?: string}
this.token = responseJson.token
if (responseJson.token !== '') {
localStorage.setItem('sessionId', this.token || '')
return true
}
return false
}
@ -66,10 +67,13 @@ class OctoClient {
}
}
async getMe(): Promise<IUser|null> {
async getMe(): Promise<IUser | undefined> {
const path = '/api/v1/users/me'
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const user = (await response.json()) as IUser || null
if (response.status !== 200) {
return undefined
}
const user = (await response.json()) as IUser
return user
}
@ -79,6 +83,9 @@ class OctoClient {
path += `&read_token=${this.readToken}`
}
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
if (response.status !== 200) {
return []
}
const blocks = (await response.json() || []) as IMutableBlock[]
this.fixBlocks(blocks)
return blocks
@ -87,6 +94,9 @@ class OctoClient {
async exportFullArchive(): Promise<IBlock[]> {
const path = '/api/v1/blocks/export'
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
if (response.status !== 200) {
return []
}
const blocks = (await response.json() || []) as IMutableBlock[]
this.fixBlocks(blocks)
return blocks
@ -198,17 +208,19 @@ class OctoClient {
},
body: formData,
})
if (response.status === 200) {
try {
const text = await response.text()
Utils.log(`uploadFile response: ${text}`)
const json = JSON.parse(text)
if (response.status !== 200) {
return undefined
}
// const json = await response.json()
return json.url
} catch (e) {
Utils.logError(`uploadFile json ERROR: ${e}`)
}
try {
const text = await response.text()
Utils.log(`uploadFile response: ${text}`)
const json = JSON.parse(text)
// const json = await response.json()
return json.url
} catch (e) {
Utils.logError(`uploadFile json ERROR: ${e}`)
}
} catch (e) {
Utils.logError(`uploadFile ERROR: ${e}`)
@ -219,10 +231,13 @@ class OctoClient {
// Sharing
async getSharing(rootId: string): Promise<ISharing> {
async getSharing(rootId: string): Promise<ISharing | undefined> {
const path = `/api/v1/sharing/${rootId}`
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const sharing = (await response.json()) as ISharing || null
if (response.status !== 200) {
return undefined
}
const sharing = (await response.json()) as ISharing
return sharing
}
@ -237,19 +252,22 @@ class OctoClient {
body,
},
)
if (response.status === 200) {
return true
if (response.status !== 200) {
return false
}
return false
return true
}
// Workspace
async getWorkspace(): Promise<IWorkspace> {
async getWorkspace(): Promise<IWorkspace | undefined> {
const path = '/api/v1/workspace'
const response = await fetch(this.serverUrl + path, {headers: this.headers()})
const workspace = (await response.json()) as IWorkspace || null
if (response.status !== 200) {
return undefined
}
const workspace = (await response.json()) as IWorkspace
return workspace
}
@ -259,11 +277,11 @@ class OctoClient {
method: 'POST',
headers: this.headers(),
})
if (response.status === 200) {
return true
if (response.status !== 200) {
return false
}
return false
return true
}
}

View file

@ -3,7 +3,7 @@
import React from 'react'
const UserContext = React.createContext(null as IUser|null)
const UserContext = React.createContext(undefined as IUser|undefined)
interface IUser {
id: string,