index.ts
1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { getThunkActionCreator } from 'redux-thunk-routine';
import { plainToClass } from 'class-transformer';
import { fetchThreadContents as fetchThreadContentsRoutine, postThreadContent as postThreadContentRoutine } from './routines';
import AlertifyHelper from '../../../../helpers/AlertifyHelper';
import {threadContentsRoute, threadMessagesRoute} from '../../api-routes';
import {apiGet, apiPost} from '../../../../helpers/api';
import ThreadContent from '../../../../models/ThreadContent';
export const fetchThreadContents = getThunkActionCreator(
fetchThreadContentsRoutine,
async (threadId: number, page: number = 1) => {
try {
const { threadContents, meta } = await apiGet(
threadContentsRoute(threadId),
{ page }
);
return {collection: plainToClass(ThreadContent, threadContents), meta};
} catch (e) {
AlertifyHelper.error('Unable to fetch thread contents');
throw e;
}
}
);
export const postThreadContent = getThunkActionCreator(
postThreadContentRoutine,
async ({threadId, message}: {threadId: number, message: string}) => {
try {
// todo: append attachment ids
const attachmentIds: number[] = [];
const { threadContent } = await apiPost(threadMessagesRoute(threadId), {
message,
threadAttachments: attachmentIds
});
return plainToClass(ThreadContent, threadContent);
} catch (e) {
AlertifyHelper.error('Failed to send your message.');
throw e;
}
}
);