Skip to content

Commit

Permalink
Merge pull request #400 from NDLANO/create_flag_arena
Browse files Browse the repository at this point in the history
Arena flag posts
  • Loading branch information
katrinewi authored Nov 30, 2023
2 parents 13731e5 + e56ba86 commit f10a221
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/api/arenaApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
*
*/

import { GraphQLError } from 'graphql';
import {
GQLArenaCategory,
GQLArenaNotification,
GQLArenaPost,
GQLArenaTopic,
GQLArenaUser,
GQLMutationNewArenaTopicArgs,
GQLMutationNewFlagArgs,
GQLMutationReplyToTopicArgs,
GQLQueryArenaCategoryArgs,
GQLQueryArenaTopicArgs,
Expand All @@ -38,6 +40,7 @@ const toArenaPost = (post: any, mainPid?: any): GQLArenaPost => ({
timestamp: post.timestampISO,
isMainPost: post.isMainPost ?? post.pid === mainPid,
user: toArenaUser(post.user),
flagId: post.flagId,
});

const toTopic = (topic: any): GQLArenaTopic => {
Expand Down Expand Up @@ -93,6 +96,7 @@ const toNotification = (notification: any): GQLArenaNotification => ({
postId: notification.pid,
notificationId: notification.nid,
});

export const fetchCsrfTokenForSession = async (
context: Context,
): Promise<{ cookie: string; 'x-csrf-token': string }> => {
Expand Down Expand Up @@ -225,3 +229,29 @@ export const replyToTopic = async (
const resolved = await resolveJson(response);
return toArenaPost(resolved.response, undefined);
};

export const newFlag = async (
{ type, id, reason }: GQLMutationNewFlagArgs,
context: Context,
): Promise<number> => {
const csrfHeaders = await fetchCsrfTokenForSession(context);
const response = await fetch(`/groups/api/v3/flags`, context, {
method: 'POST',
headers: {
'content-type': 'application/json',
...csrfHeaders,
},
body: JSON.stringify({
id: id,
type: type,
reason: reason,
}),
});
const { status, ok } = response;
const jsonResponse = await response.json();

if (ok) return id;
throw new GraphQLError(jsonResponse.status.message, {
extensions: { status },
});
};
6 changes: 5 additions & 1 deletion src/resolvers/arenaResolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
fetchArenaNotifications,
newTopic,
replyToTopic,
newFlag,
} from '../api/arenaApi';
import {
GQLArenaCategory,
Expand Down Expand Up @@ -97,7 +98,7 @@ export const Query: Pick<

export const Mutations: Pick<
GQLMutationResolvers,
'newArenaTopic' | 'replyToTopic' | 'markNotificationAsRead'
'newArenaTopic' | 'replyToTopic' | 'markNotificationAsRead' | 'newFlag'
> = {
async newArenaTopic(
_: any,
Expand All @@ -123,4 +124,7 @@ export const Mutations: Pick<
);
return params.topicIds;
},
async newFlag(_: any, params, context: ContextWithLoaders): Promise<number> {
return newFlag(params, context);
},
};
2 changes: 2 additions & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,7 @@ export const typeDefs = gql`
timestamp: String!
isMainPost: Boolean!
user: ArenaUser!
flagId: Int
}
type ArenaBreadcrumb {
Expand Down Expand Up @@ -1443,6 +1444,7 @@ export const typeDefs = gql`
content: String!
): ArenaTopic!
replyToTopic(topicId: Int!, content: String!): ArenaPost!
newFlag(type: String!, id: Int!, reason: String!): Int!
}
`;

Expand Down
11 changes: 11 additions & 0 deletions src/types/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type GQLArenaNotification = {
export type GQLArenaPost = {
__typename?: 'ArenaPost';
content: Scalars['String'];
flagId?: Maybe<Scalars['Int']>;
id: Scalars['Int'];
isMainPost: Scalars['Boolean'];
timestamp: Scalars['String'];
Expand Down Expand Up @@ -900,6 +901,7 @@ export type GQLMutation = {
deletePersonalData: Scalars['Boolean'];
markNotificationAsRead: Array<Scalars['Int']>;
newArenaTopic: GQLArenaTopic;
newFlag: Scalars['Int'];
replyToTopic: GQLArenaPost;
sortFolders: GQLSortResult;
sortResources: GQLSortResult;
Expand Down Expand Up @@ -957,6 +959,13 @@ export type GQLMutationNewArenaTopicArgs = {
};


export type GQLMutationNewFlagArgs = {
id: Scalars['Int'];
reason: Scalars['String'];
type: Scalars['String'];
};


export type GQLMutationReplyToTopicArgs = {
content: Scalars['String'];
topicId: Scalars['Int'];
Expand Down Expand Up @@ -2288,6 +2297,7 @@ export type GQLArenaNotificationResolvers<ContextType = any, ParentType extends

export type GQLArenaPostResolvers<ContextType = any, ParentType extends GQLResolversParentTypes['ArenaPost'] = GQLResolversParentTypes['ArenaPost']> = {
content?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
flagId?: Resolver<Maybe<GQLResolversTypes['Int']>, ParentType, ContextType>;
id?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType>;
isMainPost?: Resolver<GQLResolversTypes['Boolean'], ParentType, ContextType>;
timestamp?: Resolver<GQLResolversTypes['String'], ParentType, ContextType>;
Expand Down Expand Up @@ -3113,6 +3123,7 @@ export type GQLMutationResolvers<ContextType = any, ParentType extends GQLResolv
deletePersonalData?: Resolver<GQLResolversTypes['Boolean'], ParentType, ContextType>;
markNotificationAsRead?: Resolver<Array<GQLResolversTypes['Int']>, ParentType, ContextType, RequireFields<GQLMutationMarkNotificationAsReadArgs, 'topicIds'>>;
newArenaTopic?: Resolver<GQLResolversTypes['ArenaTopic'], ParentType, ContextType, RequireFields<GQLMutationNewArenaTopicArgs, 'categoryId' | 'content' | 'title'>>;
newFlag?: Resolver<GQLResolversTypes['Int'], ParentType, ContextType, RequireFields<GQLMutationNewFlagArgs, 'id' | 'reason' | 'type'>>;
replyToTopic?: Resolver<GQLResolversTypes['ArenaPost'], ParentType, ContextType, RequireFields<GQLMutationReplyToTopicArgs, 'content' | 'topicId'>>;
sortFolders?: Resolver<GQLResolversTypes['SortResult'], ParentType, ContextType, RequireFields<GQLMutationSortFoldersArgs, 'sortedIds'>>;
sortResources?: Resolver<GQLResolversTypes['SortResult'], ParentType, ContextType, RequireFields<GQLMutationSortResourcesArgs, 'parentId' | 'sortedIds'>>;
Expand Down

0 comments on commit f10a221

Please sign in to comment.