[ABANDONED] React/Redux front end for the Flexor social network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

133 lines
3.8 KiB

import { Action } from 'redux'
import { apiFetch } from 'src/api'
import { setEntities } from 'src/actions/entities'
import { listSet, listAppend } from 'src/actions/lists'
import { startRequest, finishRequest } from 'src/actions/requests'
import { objectToQuerystring } from 'src/utils'
import { normalize } from 'src/utils/normalization'
import { AppThunkAction, Entity, RequestKey, EntityType, User, Post, EntityListKey } from 'src/types'
interface CreatePostResponse {
id: string
}
interface CreatePostOptions {
visible: boolean
text?: string
cover?: string
data?: object
parent?: string
}
export const createPost = (options: CreatePostOptions): AppThunkAction<string> => {
return async dispatch => {
const { visible, text, cover, data, parent } = options
dispatch(startRequest(RequestKey.CreatePost))
try {
const post = await apiFetch<CreatePostResponse>({
path: `/api/post`,
method: 'post',
body: {
visible,
text,
cover,
data,
parent,
},
})
dispatch(finishRequest(RequestKey.CreatePost, true))
return post.id
} catch (err) {
dispatch(finishRequest(RequestKey.CreatePost, false))
throw err
}
}
}
export const fetchPost = (id: string): AppThunkAction => {
return async dispatch => {
dispatch(startRequest(RequestKey.FetchPost))
try {
const post = await apiFetch<Entity>({
path: `/api/post/${id}`
})
const posts = normalize([post], EntityType.Post)
dispatch(setEntities(posts.entities))
dispatch(finishRequest(RequestKey.FetchPost, true))
} catch (err) {
dispatch(finishRequest(RequestKey.FetchPost, false))
throw err
}
}
}
interface TimelineResponse {
posts: Entity[]
continuation?: string
}
export const fetchTimeline = (continuation?: string): AppThunkAction => async dispatch => {
dispatch(startRequest(RequestKey.FetchTimeline))
try {
const response = await apiFetch<TimelineResponse>({
path: `/api/timeline?${objectToQuerystring({ continuation })}`,
})
const posts = normalize(response.posts, EntityType.Group)
dispatch(setEntities(posts.entities))
if (continuation) {
dispatch(listAppend(EntityListKey.Timeline, posts.keys, response.continuation))
} else {
dispatch(listSet(EntityListKey.Timeline, posts.keys, response.continuation))
}
dispatch(finishRequest(RequestKey.FetchTimeline, true))
} catch (err) {
dispatch(finishRequest(RequestKey.FetchTimeline, false))
throw err
}
}
interface UserPostsResponse {
user: User
posts: Post[]
continuation?: string
}
export const fetchUserPosts = (id: string, continuation?: string): AppThunkAction => async dispatch => {
dispatch(startRequest(RequestKey.FetchUserPosts))
try {
const response = await apiFetch<UserPostsResponse>({
path: `/api/user/${id}/posts`,
})
const posts = normalize(response.posts.map(p => ({
...p,
user: response.user,
})), EntityType.Post)
dispatch(setEntities(posts.entities))
if (continuation) {
dispatch(listAppend(`posts:${id}`, posts.keys, response.continuation))
} else {
dispatch(listSet(`posts:${id}`, posts.keys, response.continuation))
}
dispatch(finishRequest(RequestKey.FetchUserPosts, true))
} catch (err) {
dispatch(finishRequest(RequestKey.FetchUserPosts, false))
throw err
}
}