Update the view after a post is created
parent
0aaa406b17
commit
236c908058
|
@ -102,6 +102,7 @@ export class FeedViewModel {
|
|||
params: GetFeedView.QueryParams
|
||||
_loadPromise: Promise<void> | undefined
|
||||
_loadMorePromise: Promise<void> | undefined
|
||||
_loadLatestPromise: Promise<void> | undefined
|
||||
_updatePromise: Promise<void> | undefined
|
||||
|
||||
// data
|
||||
|
@ -118,6 +119,7 @@ export class FeedViewModel {
|
|||
params: false,
|
||||
_loadPromise: false,
|
||||
_loadMorePromise: false,
|
||||
_loadLatestPromise: false,
|
||||
_updatePromise: false,
|
||||
},
|
||||
{autoBind: true},
|
||||
|
@ -180,6 +182,19 @@ export class FeedViewModel {
|
|||
this._loadMorePromise = undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Load more posts to the start of the feed
|
||||
*/
|
||||
async loadLatest() {
|
||||
if (this._loadLatestPromise) {
|
||||
return this._loadLatestPromise
|
||||
}
|
||||
await this._pendingWork()
|
||||
this._loadLatestPromise = this._loadLatest()
|
||||
await this._loadLatestPromise
|
||||
this._loadLatestPromise = undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* Update content in-place
|
||||
*/
|
||||
|
@ -219,6 +234,9 @@ export class FeedViewModel {
|
|||
if (this._loadMorePromise) {
|
||||
await this._loadMorePromise
|
||||
}
|
||||
if (this._loadLatestPromise) {
|
||||
await this._loadLatestPromise
|
||||
}
|
||||
if (this._updatePromise) {
|
||||
await this._updatePromise
|
||||
}
|
||||
|
@ -235,6 +253,17 @@ export class FeedViewModel {
|
|||
}
|
||||
}
|
||||
|
||||
private async _loadLatest() {
|
||||
this._xLoading()
|
||||
try {
|
||||
const res = await this.rootStore.api.todo.social.getFeed(this.params)
|
||||
this._prependAll(res)
|
||||
this._xIdle()
|
||||
} catch (e: any) {
|
||||
this._xIdle(`Failed to load feed: ${e.toString()}`)
|
||||
}
|
||||
}
|
||||
|
||||
private async _loadMore() {
|
||||
this._xLoading()
|
||||
try {
|
||||
|
@ -298,6 +327,23 @@ export class FeedViewModel {
|
|||
this.feed.push(new FeedViewItemModel(this.rootStore, `item-${keyId}`, item))
|
||||
}
|
||||
|
||||
private _prependAll(res: GetFeedView.Response) {
|
||||
let counter = this.feed.length
|
||||
for (const item of res.data.feed) {
|
||||
if (this.feed.find(item2 => item2.uri === item.uri)) {
|
||||
return // stop here - we've hit a post we already ahve
|
||||
}
|
||||
this._prepend(counter++, item)
|
||||
}
|
||||
}
|
||||
|
||||
private _prepend(keyId: number, item: GetFeedView.FeedItem) {
|
||||
// TODO: validate .record
|
||||
this.feed.unshift(
|
||||
new FeedViewItemModel(this.rootStore, `item-${keyId}`, item),
|
||||
)
|
||||
}
|
||||
|
||||
private _updateAll(res: GetFeedView.Response) {
|
||||
for (const item of res.data.feed) {
|
||||
const existingItem = this.feed.find(
|
||||
|
|
|
@ -34,11 +34,19 @@ export class SharePostModel {
|
|||
}
|
||||
}
|
||||
|
||||
export interface ComposePostModelOpts {
|
||||
replyTo?: string
|
||||
onPost?: () => void
|
||||
}
|
||||
export class ComposePostModel {
|
||||
name = 'compose-post'
|
||||
replyTo?: string
|
||||
onPost?: () => void
|
||||
|
||||
constructor(public replyTo?: string) {
|
||||
constructor(opts?: ComposePostModelOpts) {
|
||||
makeAutoObservable(this)
|
||||
this.replyTo = opts?.replyTo
|
||||
this.onPost = opts?.onPost
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,13 @@ const WARNING_TEXT_LENGTH = 200
|
|||
const DANGER_TEXT_LENGTH = 255
|
||||
export const snapPoints = ['100%']
|
||||
|
||||
export function Component({replyTo}: {replyTo?: string}) {
|
||||
export function Component({
|
||||
replyTo,
|
||||
onPost,
|
||||
}: {
|
||||
replyTo?: string
|
||||
onPost?: () => void
|
||||
}) {
|
||||
const store = useStores()
|
||||
const [error, setError] = useState('')
|
||||
const [text, setText] = useState('')
|
||||
|
@ -72,6 +78,7 @@ export function Component({replyTo}: {replyTo?: string}) {
|
|||
)
|
||||
return
|
||||
}
|
||||
onPost?.()
|
||||
store.shell.closeModal()
|
||||
Toast.show(`Your ${replyTo ? 'reply' : 'post'} has been published`, {
|
||||
duration: Toast.durations.LONG,
|
||||
|
|
|
@ -70,7 +70,11 @@ export const PostThread = observer(function PostThread({uri}: {uri: string}) {
|
|||
// =
|
||||
const posts = view.thread ? Array.from(flattenThread(view.thread)) : []
|
||||
const renderItem = ({item}: {item: PostThreadViewPostModel}) => (
|
||||
<PostThreadItem item={item} onPressShare={onPressShare} />
|
||||
<PostThreadItem
|
||||
item={item}
|
||||
onPressShare={onPressShare}
|
||||
onPostReply={onRefresh}
|
||||
/>
|
||||
)
|
||||
return (
|
||||
<FlatList
|
||||
|
|
|
@ -20,9 +20,11 @@ const PARENT_REPLY_LINE_LENGTH = 8
|
|||
export const PostThreadItem = observer(function PostThreadItem({
|
||||
item,
|
||||
onPressShare,
|
||||
onPostReply,
|
||||
}: {
|
||||
item: PostThreadViewPostModel
|
||||
onPressShare: (_uri: string) => void
|
||||
onPostReply: () => void
|
||||
}) {
|
||||
const store = useStores()
|
||||
const record = item.record as unknown as PostType.Record
|
||||
|
@ -47,7 +49,9 @@ export const PostThreadItem = observer(function PostThreadItem({
|
|||
const repostsTitle = 'Reposts of this post'
|
||||
|
||||
const onPressReply = () => {
|
||||
store.shell.openModal(new ComposePostModel(item.uri))
|
||||
store.shell.openModal(
|
||||
new ComposePostModel({replyTo: item.uri, onPost: onPostReply}),
|
||||
)
|
||||
}
|
||||
const onPressToggleRepost = () => {
|
||||
item
|
||||
|
|
|
@ -72,7 +72,7 @@ export const Post = observer(function Post({uri}: {uri: string}) {
|
|||
replyHref = `/profile/${urip.hostname}/post/${urip.recordKey}`
|
||||
}
|
||||
const onPressReply = () => {
|
||||
store.shell.openModal(new ComposePostModel(item.uri))
|
||||
store.shell.openModal(new ComposePostModel({replyTo: item.uri}))
|
||||
}
|
||||
const onPressToggleRepost = () => {
|
||||
item
|
||||
|
|
|
@ -40,7 +40,7 @@ export const FeedItem = observer(function FeedItem({
|
|||
}, [record.reply])
|
||||
|
||||
const onPressReply = () => {
|
||||
store.shell.openModal(new ComposePostModel(item.uri))
|
||||
store.shell.openModal(new ComposePostModel({replyTo: item.uri}))
|
||||
}
|
||||
const onPressToggleRepost = () => {
|
||||
item
|
||||
|
|
|
@ -31,7 +31,10 @@ export const Home = observer(function Home({visible}: ScreenParams) {
|
|||
}, [visible, store])
|
||||
|
||||
const onComposePress = () => {
|
||||
store.shell.openModal(new ComposePostModel())
|
||||
store.shell.openModal(new ComposePostModel({onPost: onCreatePost}))
|
||||
}
|
||||
const onCreatePost = () => {
|
||||
feedView?.loadLatest()
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
Loading…
Reference in New Issue