Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/ZJU-CC98/CC98-PWA into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Deturium committed Nov 1, 2018
2 parents c2c9551 + ddadeba commit f34a250
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/model/message/detail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { GET, POST } from '@/utils/fetch'
import { IMessageContent } from '@cc98/api'
import { Container } from '@cc98/state'
import reverse from 'lodash-es/reverse'
import uniqBy from 'lodash-es/uniqBy'
import global from '../global'
import user from '../user'

Expand All @@ -15,7 +14,7 @@ interface IMap<T> {
}

interface State {
messages: IMap<IMessageContent[]>
messages: IMap<IMessageContent[] | undefined>
isEnd: boolean
isLoading: boolean
id: string
Expand Down Expand Up @@ -58,14 +57,14 @@ export class Detail extends Container<State> {

const res = await GET<IMessageContent[]>(`message/user/${this.state.id}`, {
params: {
from: `${this.state.messages[this.state.id].length}`,
from: `${(this.state.messages[this.state.id] || []).length}`,
size: '20',
},
})

res.fail().succeed(data => {
this.put(state => {
state.messages[this.state.id] = [...reverse(data), ...state.messages[this.state.id]]
state.messages[this.state.id] = [...reverse(data), ...state.messages[this.state.id] || []]
state.isLoading = false
if (data.length < 20) state.isEnd = true
})
Expand All @@ -85,7 +84,9 @@ export class Detail extends Container<State> {
res.fail().succeed(messages => {
this.put(state => {
if (messages[0] && this.state.messages[this.state.id]) {
state.messages[this.state.id].push(messages[0])
state.messages[this.state.id]!.push(messages[0])
} else if (messages[0]) {
state.messages[this.state.id]
}
})
})
Expand All @@ -101,14 +102,19 @@ export class Detail extends Container<State> {

res.fail().succeed(() => {
this.put(state => {
state.messages[this.state.id].push({
const newMessage = {
content,
id: messageId,
senderId: global.state.myInfo!.id,
receiverId: parseInt(this.state.id, 10),
time: new Date(Date.now()).toUTCString(),
isRead: true,
})
}
if (this.state.messages[this.state.id]) {
state.messages[this.state.id]!.push(newMessage)
} else {
state.messages[this.state.id] = [newMessage]
}
messageId -= 1
})
})
Expand Down

0 comments on commit f34a250

Please sign in to comment.