Skip to content

Commit 30285f0

Browse files
author
Sebastian Thulin
committed
fix: allow the user to submit form.
1 parent a895398 commit 30285f0

4 files changed

Lines changed: 95 additions & 32 deletions

File tree

source/js/init.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ class Form {
5353
steps,
5454
new StepNavigator(
5555
steps,
56-
new Submit(this.form)
56+
new Submit(
57+
this.form,
58+
modularityFrontendFormData
59+
),
5760
),
5861
new StepUIManager(
5962
steps,

source/js/steps/submit/submit.ts

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1+
interface SubmitInterface {
2+
submit(event: Event): void | Promise<void>;
3+
}
14
class Submit implements SubmitInterface {
25
constructor(
3-
private form: HTMLFormElement
4-
) {
5-
}
6-
7-
public submit(): void {
8-
console.log("submiting");
6+
private form: HTMLFormElement,
7+
private modularityFrontendFormData: ModularityFrontendFormData
8+
) {}
9+
10+
11+
public async submit(): Promise<void> {
12+
console.log("Submitting form...");
13+
console.log(this.modularityFrontendFormData.apiRoutes);
14+
const url = this.modularityFrontendFormData.apiRoutes?.submitForm;
15+
16+
if (!url) {
17+
console.error("Submit URL is not defined.");
18+
return;
19+
}
20+
21+
try {
22+
const response = await fetch(url, {
23+
method: "POST",
24+
body: new FormData(this.form),
25+
});
26+
27+
if (!response.ok) {
28+
throw new Error(`Response status: ${response.status}`);
29+
}
30+
31+
const json = await response.json();
32+
console.log("Form submitted successfully:", json);
33+
} catch (error: any) {
34+
console.error("Form submission failed:", error?.message || error);
35+
}
936
}
1037
}
11-
12-
export default Submit;
38+
export default Submit;

source/php/Api/Submit/Post.php

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ class Post extends RestApiEndpoint
1313
{
1414
public const NAMESPACE = 'modularity-frontend-form/v1';
1515
public const ROUTE = 'submit/post';
16-
public const KEY = 'submit-post';
16+
public const KEY = 'submitForm';
17+
18+
public function __construct(
19+
private \WpService\WpService $wpService
20+
) {}
1721

1822
/**
1923
* Registers a REST route
@@ -28,23 +32,10 @@ public function handleRegisterRestRoute(): bool
2832
'permission_callback' => array($this, 'permissionCallback'),
2933
'args' => [
3034
'module-id' => [
31-
'description' => __('The module id that the request originates from', 'municipio'),
32-
'type' => 'integer',
33-
'format' => 'uri',
34-
'required' => true
35-
],
36-
'data' => [
37-
'description' => __('Description.', 'municipio'),
38-
'type' => 'string',
39-
'required' => false,
40-
'default' => null
41-
],
42-
'return' => [
43-
'description' => __('Return', 'municipio'),
44-
'type' => 'string',
45-
'enum' => ['html', 'src', 'id'],
46-
'required' => false,
47-
'default' => 'html'
35+
'description' => __('The module id that the request originates from', 'municipio'),
36+
'type' => 'integer',
37+
'format' => 'uri',
38+
'required' => false
4839
]
4940
]
5041
));
@@ -61,18 +52,30 @@ public function handleRegisterRestRoute(): bool
6152
public function handleRequest(WP_REST_Request $request): WP_REST_Response
6253
{
6354
$params = $request->get_json_params();
64-
$a = $params['url'] ?? null;
55+
$a = $params['url'] ?? null;
56+
57+
$insert = $this->insertPost();
6558

66-
/*if (is_wp_error($a)) {
59+
if (is_wp_error($insert)) {
6760
$error = new WP_Error(
6861
$a->get_error_code(),
6962
$a->get_error_message(),
7063
array('status' => WP_Http::BAD_REQUEST)
7164
);
7265
return rest_ensure_response($error);
73-
}*/
66+
} elseif (is_numeric($insert)) {
67+
return rest_ensure_response([
68+
'status' => 'success',
69+
'message' => __('Post created successfully', 'municipio'),
70+
'postId' => $insert,
71+
]);
72+
}
7473

75-
return rest_ensure_response($a);
74+
return rest_ensure_response(new WP_Error(
75+
502,
76+
__('Unexpected result from post creation', 'municipio'),
77+
array('status' => WP_Http::BAD_REQUEST)
78+
));
7679
}
7780

7881
/**
@@ -84,4 +87,35 @@ public function permissionCallback(): bool
8487
{
8588
return true; //May be changed to check for specific capabilities
8689
}
90+
91+
/**
92+
* Handles the request to insert a post
93+
*
94+
* @param int|null $moduleID The module ID
95+
* @param array|null $fieldMeta The field meta data
96+
*
97+
* @return WP_Error|int The result of the post insertion
98+
*/
99+
private function insertPost($moduleID = null, $fieldMeta = null): WP_Error|int {
100+
101+
$result = $this->wpService->wpInsertPost([
102+
'post_title' => 'Test post',
103+
'post_type' => 'post',
104+
'post_status' => 'publish',
105+
'meta_input' => [
106+
'module_id' => $moduleID,
107+
'field_meta' => $fieldMeta,
108+
],
109+
]);
110+
111+
if (is_wp_error($result)) {
112+
return $result;
113+
}
114+
115+
if(is_numeric($result)) {
116+
return $result;
117+
}
118+
119+
throw new \Exception('Unexpected result from post creation: ' . print_r($result, true));
120+
}
87121
}

source/php/App.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function registerModule()
6060
public function registerApi()
6161
{
6262
$restEndpoints = [
63-
new Api\Submit\Post()
63+
new Api\Submit\Post($this->wpService),
6464
];
6565

6666
$this->wpService->applyFilters(

0 commit comments

Comments
 (0)