From 339c6957fe92c4ec1809159f09c55201d3794c18 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Fri, 6 May 2022 18:21:16 +0200 Subject: chore: add a Contact page --- src/services/graphql/api.ts | 26 +++++++++++++++++++++----- src/services/graphql/contact.mutation.ts | 25 +++++++++++++++++++++++++ src/services/graphql/contact.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 src/services/graphql/contact.mutation.ts create mode 100644 src/services/graphql/contact.ts (limited to 'src/services/graphql') diff --git a/src/services/graphql/api.ts b/src/services/graphql/api.ts index b0e8d3a..9811d86 100644 --- a/src/services/graphql/api.ts +++ b/src/services/graphql/api.ts @@ -7,6 +7,7 @@ import { totalArticlesQuery, } from './articles.query'; import { commentsQuery } from './comments.query'; +import { sendMailMutation } from './contact.mutation'; import { thematicBySlugQuery, thematicsListQuery, @@ -18,6 +19,8 @@ import { topicsSlugQuery, } from './topics.query'; +export type Mutations = typeof sendMailMutation; + export type Queries = | typeof articlesQuery | typeof articleBySlugQuery @@ -44,6 +47,10 @@ export type CommentsResponse = { comments: T[]; }; +export type SendMailResponse = { + sendEmail: T; +}; + export type ThematicResponse = { thematic: T; }; @@ -85,15 +92,16 @@ export type NodesResponse = { }; export type ResponseMap = { - [articleBySlugQuery]: ArticleResponse>; + [articleBySlugQuery]: ArticleResponse; [articlesCardQuery]: ArticlesResponse>; [articlesQuery]: ArticlesResponse>; [articlesSlugQuery]: ArticlesResponse>; [commentsQuery]: CommentsResponse>; - [thematicBySlugQuery]: ThematicResponse>; + [sendMailMutation]: SendMailResponse; + [thematicBySlugQuery]: ThematicResponse; [thematicsListQuery]: ThematicsResponse>; [thematicsSlugQuery]: ThematicsResponse>; - [topicBySlugQuery]: TopicResponse>; + [topicBySlugQuery]: TopicResponse; [topicsListQuery]: TopicsResponse>; [topicsSlugQuery]: TopicsResponse>; [totalArticlesQuery]: ArticlesResponse; @@ -133,12 +141,20 @@ export type ByContentIdVar = { contentId: number; }; +export type sendMailVars = { + body: string; + clientMutationId: string; + replyTo: string; + subject: string; +}; + export type VariablesMap = { [articleBySlugQuery]: BySlugVar; [articlesCardQuery]: EdgesVars; [articlesQuery]: EdgesVars; [articlesSlugQuery]: EdgesVars; [commentsQuery]: ByContentIdVar; + [sendMailMutation]: sendMailVars; [thematicBySlugQuery]: BySlugVar; [thematicsListQuery]: EdgesVars; [thematicsSlugQuery]: EdgesVars; @@ -148,7 +164,7 @@ export type VariablesMap = { [totalArticlesQuery]: null; }; -export type FetchAPIProps = { +export type FetchAPIProps = { /** * A GraphQL API URL. */ @@ -170,7 +186,7 @@ export type FetchAPIProps = { * @param {Queries} obj.query - A GraphQL query. * @param {object} [obj.variables] - The query variables. */ -export async function fetchAPI({ +export async function fetchAPI({ api, query, variables, diff --git a/src/services/graphql/contact.mutation.ts b/src/services/graphql/contact.mutation.ts new file mode 100644 index 0000000..b82fc07 --- /dev/null +++ b/src/services/graphql/contact.mutation.ts @@ -0,0 +1,25 @@ +/** + * Send mail mutation. + */ +export const sendMailMutation = `mutation SendEmail( + $subject: String! + $body: String! + $replyTo: String! + $clientMutationId: String! +) { + sendEmail( + input: { + clientMutationId: $clientMutationId + body: $body + replyTo: $replyTo + subject: $subject + } + ) { + clientMutationId + message + sent + origin + replyTo + to + } +}`; diff --git a/src/services/graphql/contact.ts b/src/services/graphql/contact.ts new file mode 100644 index 0000000..fca718f --- /dev/null +++ b/src/services/graphql/contact.ts @@ -0,0 +1,26 @@ +import { fetchAPI, getAPIUrl, sendMailVars } from './api'; +import { sendMailMutation } from './contact.mutation'; + +export type SentEmail = { + clientMutationId: string; + message: string; + origin: string; + replyTo: string; + sent: boolean; +}; + +/** + * Send an email using GraphQL API. + * + * @param {sendMailVars} data - The mail data. + * @returns {Promise} The mutation response. + */ +export const sendMail = async (data: sendMailVars): Promise => { + const response = await fetchAPI({ + api: getAPIUrl(), + query: sendMailMutation, + variables: { ...data }, + }); + + return response.sendEmail; +}; -- cgit v1.2.3