3
3
4
4
// Type for our form data
5
5
export interface FeedbackFormData {
6
- email ?: string ;
7
- message : string ;
6
+ email ?: string ;
7
+ message : string ;
8
8
}
9
9
10
10
export async function submitFeedback ( data : FeedbackFormData ) {
11
- const databaseId = process . env . NOTION_DATABASE_ID ;
12
- const notionApiKey = process . env . NOTION_API_KEY ;
11
+ const databaseId = process . env . NOTION_DATABASE_ID ;
12
+ const notionApiKey = process . env . NOTION_API_KEY ;
13
13
14
- if ( ! databaseId ) {
15
- throw new Error ( "NOTION_DATABASE_ID environment variable is not set" ) ;
16
- }
14
+ if ( ! databaseId ) {
15
+ throw new Error ( "NOTION_DATABASE_ID environment variable is not set" ) ;
16
+ }
17
17
18
- if ( ! notionApiKey ) {
19
- throw new Error ( "NOTION_API_KEY environment variable is not set" ) ;
20
- }
18
+ if ( ! notionApiKey ) {
19
+ throw new Error ( "NOTION_API_KEY environment variable is not set" ) ;
20
+ }
21
21
22
- try {
23
- const response = await fetch ( 'https://api.notion.com/v1/pages' , {
24
- method : 'POST' ,
25
- headers : {
26
- 'Authorization' : `Bearer ${ notionApiKey } ` ,
27
- 'Content-Type' : 'application/json' ,
28
- 'Notion-Version' : '2022-06-28' ,
29
- } ,
30
- body : JSON . stringify ( {
31
- parent : {
32
- database_id : databaseId ,
22
+ try {
23
+ const response = await fetch ( "https://api.notion.com/v1/pages" , {
24
+ method : "POST" ,
25
+ headers : {
26
+ Authorization : `Bearer ${ notionApiKey } ` ,
27
+ "Content-Type" : "application/json" ,
28
+ "Notion-Version" : "2022-06-28" ,
29
+ } ,
30
+ body : JSON . stringify ( {
31
+ parent : {
32
+ database_id : databaseId ,
33
+ } ,
34
+ properties : {
35
+ Email : {
36
+ title : [
37
+ {
38
+ text : {
39
+ content : data . email || "Anonymous" ,
33
40
} ,
34
- properties : {
35
- "Email" : {
36
- title : [
37
- {
38
- text : {
39
- content : data . email || "Anonymous"
40
- }
41
- }
42
- ]
43
- } ,
44
- "Feedback" : {
45
- rich_text : [
46
- {
47
- text : {
48
- content : data . message
49
- }
50
- }
51
- ]
52
- } ,
53
- "Date" : {
54
- date : {
55
- start : new Date ( ) . toISOString ( )
56
- }
57
- }
58
- }
59
- } ) ,
60
- } ) ;
61
-
62
- if ( ! response . ok ) {
63
- const errorData = await response . json ( ) ;
64
- return { success : false , message : `Notion returned an API error: ${ errorData . message || response . statusText } ` } ;
65
- }
41
+ } ,
42
+ ] ,
43
+ } ,
44
+ Feedback : {
45
+ rich_text : [
46
+ {
47
+ text : {
48
+ content : data . message ,
49
+ } ,
50
+ } ,
51
+ ] ,
52
+ } ,
53
+ Date : {
54
+ date : {
55
+ start : new Date ( ) . toISOString ( ) ,
56
+ } ,
57
+ } ,
58
+ } ,
59
+ } ) ,
60
+ } ) ;
66
61
67
- return { success : true , message : "Feedback submitted successfully." }
68
- } catch ( error ) {
69
- return { success : false , message : `Encountered an internal error: ${ error } ` } ;
62
+ if ( ! response . ok ) {
63
+ const errorData = await response . json ( ) ;
64
+ return {
65
+ success : false ,
66
+ message : `Notion returned an API error: ${ errorData . message || response . statusText } ` ,
67
+ } ;
70
68
}
71
- }
69
+
70
+ return { success : true , message : "Feedback submitted successfully." } ;
71
+ } catch ( error ) {
72
+ return {
73
+ success : false ,
74
+ message : `Encountered an internal error: ${ error } ` ,
75
+ } ;
76
+ }
77
+ }
0 commit comments