Skip to content

Commit

Permalink
Type announcement, article, comment and company actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarnakken committed Aug 26, 2023
1 parent 7814e68 commit 6996a21
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 140 deletions.
29 changes: 18 additions & 11 deletions app/actions/AnnouncementsActions.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { stopSubmit } from 'redux-form';
import callAPI from 'app/actions/callAPI';
import { announcementsSchema } from 'app/reducers';
import type { Thunk } from 'app/types';
import type { AppDispatch } from 'app/store/createStore';
import type { ID } from 'app/store/models';
import type {
DetailedAnnouncement,
ListAnnouncement,
} from 'app/store/models/Announcement';
import { Announcements } from './ActionTypes';

export function fetchAll(): Thunk<any> {
return callAPI({
export function fetchAll() {
return callAPI<ListAnnouncement[]>({
types: Announcements.FETCH_ALL,
endpoint: '/announcements/',
schema: [announcementsSchema],
Expand All @@ -15,6 +20,7 @@ export function fetchAll(): Thunk<any> {
propagateError: true,
});
}

export function createAnnouncement({
message,
users,
Expand All @@ -23,11 +29,10 @@ export function createAnnouncement({
meetings,
fromGroup,
send,
}: Record<string, any>): /*AnnouncementModel*/
Thunk<any> {
return (dispatch) =>
}: Record<string, any>) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
callAPI<DetailedAnnouncement>({
types: Announcements.CREATE,
endpoint: '/announcements/',
method: 'POST',
Expand All @@ -46,7 +51,7 @@ Thunk<any> {
})
)
.then((action) => {
if (send && action && action.payload) {
if (send && action?.payload?.result) {
dispatch(sendAnnouncement(action.payload.result));
}
})
Expand All @@ -55,8 +60,9 @@ Thunk<any> {
dispatch(stopSubmit('AnnouncementsCreate', errors));
});
}
export function sendAnnouncement(announcementId: number): Thunk<any> {
return callAPI({

export function sendAnnouncement(announcementId: ID) {
return callAPI<{ status: string }>({
types: Announcements.SEND,
endpoint: `/announcements/${announcementId}/send/`,
method: 'POST',
Expand All @@ -66,7 +72,8 @@ export function sendAnnouncement(announcementId: number): Thunk<any> {
},
});
}
export function deleteAnnouncement(id: number): Thunk<any> {

export function deleteAnnouncement(id: ID) {
return callAPI({
types: Announcements.DELETE,
endpoint: `/announcements/${id}/`,
Expand Down
34 changes: 19 additions & 15 deletions app/actions/ArticleActions.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { push } from 'connected-react-router';
import callAPI from 'app/actions/callAPI';
import { articleSchema } from 'app/reducers';
import type { AppDispatch } from 'app/store/createStore';
import type { ID } from 'app/store/models';
import type { ArticleEntity, Thunk } from 'app/types';
import type { DetailedArticle } from 'app/store/models/Article';
import type { ArticleEntity } from 'app/types';
import { Article } from './ActionTypes';

export function fetchArticle(articleId: ID): Thunk<Promise<void>> {
return callAPI({
export function fetchArticle(articleId: ID) {
return callAPI<DetailedArticle>({
types: Article.FETCH,
endpoint: `/articles/${articleId}/`,
schema: articleSchema,
Expand All @@ -16,10 +18,11 @@ export function fetchArticle(articleId: ID): Thunk<Promise<void>> {
propagateError: true,
});
}
export function createArticle({ id, ...data }: ArticleEntity): Thunk<any> {
return (dispatch) =>

export function createArticle({ id, ...data }: ArticleEntity) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
callAPI<DetailedArticle>({
types: Article.CREATE,
endpoint: '/articles/',
method: 'POST',
Expand All @@ -29,11 +32,10 @@ export function createArticle({ id, ...data }: ArticleEntity): Thunk<any> {
errorMessage: 'Opprettelse av artikkel feilet',
},
})
).then((res) =>
dispatch(push(`/articles/${(res as any).payload.result}/`))
);
).then((res) => dispatch(push(`/articles/${res?.payload?.result}/`)));
}
export function deleteArticle(id: number): Thunk<any> {

export function deleteArticle(id: ID) {
return callAPI({
types: Article.DELETE,
endpoint: `/articles/${id}/`,
Expand All @@ -44,8 +46,9 @@ export function deleteArticle(id: number): Thunk<any> {
},
});
}
export function editArticle({ id, ...data }: ArticleEntity): Thunk<any> {
return (dispatch) =>

export function editArticle({ id, ...data }: ArticleEntity) {
return (dispatch: AppDispatch) =>
dispatch(
callAPI({
types: Article.EDIT,
Expand All @@ -57,16 +60,17 @@ export function editArticle({ id, ...data }: ArticleEntity): Thunk<any> {
errorMessage: 'Endring av artikkel feilet',
},
})
).then((res) => dispatch(push(`/articles/${id}/`)));
).then(() => dispatch(push(`/articles/${id}/`)));
}

export function fetchAll({
query,
next = false,
}: {
query?: Record<string, any>;
next?: boolean;
} = {}): Thunk<any> {
return callAPI({
} = {}) {
return callAPI<DetailedArticle[]>({
types: Article.FETCH,
endpoint: '/articles/',
schema: [articleSchema],
Expand Down
16 changes: 9 additions & 7 deletions app/actions/CommentActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@ import callAPI from 'app/actions/callAPI';
import { commentSchema } from 'app/reducers';
import type { ID } from 'app/store/models';
import type CommentType from 'app/store/models/Comment';
import type { Thunk } from 'app/types';
import type { ContentTarget } from 'app/store/utils/contentTarget';
import { Comment } from './ActionTypes';

export function addComment({
text,
contentTarget,
parent,
}: CommentType): Thunk<Promise<Record<string, any> | null | undefined>> {
return callAPI({
}: {
text: string;
contentTarget: ContentTarget;
parent?: ID;
}) {
return callAPI<CommentType>({
types: Comment.ADD,
endpoint: '/comments/',
method: 'POST',
Expand All @@ -30,10 +34,8 @@ export function addComment({
schema: commentSchema,
});
}
export function deleteComment(
commentId: ID,
contentTarget: string
): Thunk<any> {

export function deleteComment(commentId: ID, contentTarget: ContentTarget) {
return callAPI({
types: Comment.DELETE,
endpoint: `/comments/${commentId}/`,
Expand Down
Loading

0 comments on commit 6996a21

Please sign in to comment.