16
16
{% endfor %}
17
17
18
18
<fieldset style =" padding-top : 10px ;" class =" form-check" >
19
- <button type =" submit" class =" btn btn-primary" id =" updateButton " >Update </button >
19
+ <button type =" submit" class =" btn btn-primary" id =" createButton " >Create </button >
20
20
</fieldset >
21
21
22
22
</form >
27
27
{% block scripts %}
28
28
<script >
29
29
30
+ var apiKey = " {{ request.user.api_key }}" ;
31
+ function getLookup (formName , fieldName , el ) {
32
+
33
+ // Access the selected option
34
+ var selectedOption = el .options [el .selectedIndex ];
35
+
36
+ // Get the 'data-hidden' attribute from the selected option
37
+ var documentData = selectedOption .getAttribute (' data-hidden' );
38
+ var documentId = selectedOption .value ;
39
+ console .log (documentData);
40
+
41
+ const contentField = document .getElementById (` content_${ fieldName} ` );
42
+
43
+ // Clear previous contents
44
+ contentField .innerHTML = ' ' ;
45
+
46
+ if (documentData) {
47
+ let dataObj;
48
+ try {
49
+ dataObj = JSON .parse (documentData);
50
+ } catch (error) {
51
+ console .error (" Parsing error:" , error);
52
+ return ; // Stop execution if JSON is invalid.
53
+ }
54
+
55
+ const listGroup = document .createElement (" div" );
56
+ listGroup .className = ' list-group' ;
57
+
58
+ Object .keys (dataObj).forEach (function (key ) {
59
+ const listItem = document .createElement (" a" );
60
+ listItem .className = ' list-group-item list-group-item-action' ;
61
+ listItem .innerHTML = ` <strong >${ key} :</strong > ${ dataObj[key]} ` ;
62
+ listGroup .appendChild (listItem);
63
+ });
64
+
65
+ contentField .appendChild (listGroup);
66
+
67
+ let oldLink = document .getElementById (' lookup-link-' + fieldName);
68
+ if (oldLink) {
69
+ oldLink .parentNode .removeChild (oldLink);
70
+ }
71
+
72
+ // Create an anchor element with the appropriate href
73
+ let link = document .createElement (' a' );
74
+ link .href = " /ui/form/read_one/" + formName + " /" + documentId;
75
+ link .innerHTML = " Open Full Record for this Form" ;
76
+ link .className = " btn btn-primary d-flex justify-content-center my-3" ;
77
+ link .title = " Click here to view more details about this form." ;
78
+ link .target = " _blank" ;
79
+ link .setAttribute (" aria-label" , " View full details for this form" );
80
+ link .id = ' lookup-link-' + fieldName; // Add this line to set the id
81
+
82
+ contentField .parentNode .insertBefore (link, contentField);
83
+
84
+
85
+ }
86
+ }
87
+
88
+
89
+ function generateLookup (formName , fieldName , displayFields ) {
90
+
91
+ function fetchData (formName ) {
92
+ return new Promise ((resolve , reject ) => {
93
+ $ .ajax ({
94
+ url: ` /api/form/read_all/${ formName} ?flatten=true&exclude_journal=true&stringify_output=true&sort_by_last_edited=true&newest_first=true` ,
95
+ type: " GET" ,
96
+ dataType: ' json' ,
97
+ beforeSend : function (xhr ){xhr .setRequestHeader (' X-API-KEY' , apiKey);},
98
+ success : function (formData ) {
99
+ resolve (formData .documents );
100
+ },
101
+ error : function (xhr , status , error ) {
102
+ reject (error);
103
+ }
104
+ });
105
+ });
106
+ }
107
+
108
+ // Fetch data for the single form
109
+ fetchData (formName).then (documents => {
110
+
111
+ // First add an empty option at the top of the list
112
+ $ (' #' + fieldName).append ($ (' <option>' , {
113
+ value: " " ,
114
+ ' data-hidden' : " " ,
115
+ text: " "
116
+ }));
117
+
118
+
119
+ var trimmedData = [];
120
+ // Process documents
121
+ documents .forEach (form => {
122
+ // Initialize an empty string for each form
123
+ let concatenatedFields = ' ' ;
124
+
125
+ displayFields .forEach (field => {
126
+ if (form[field] !== undefined ) {
127
+ concatenatedFields += form[field] + ' , ' ;
128
+ }
129
+ });
130
+
131
+ // Trim the trailing commas and spaces, and add the concatenated string to the trimmedData array
132
+ concatenatedFields = concatenatedFields .replace (/ ,\s * $ / , ' ' );
133
+ trimmedData .push (concatenatedFields);
134
+
135
+ // Append each concatenated string as an option to the select element
136
+ $ (' #' + fieldName).append ($ (' <option>' , {
137
+ value: form[' __metadata__document_id' ],
138
+ ' data-hidden' : JSON .stringify (form),
139
+ text: concatenatedFields
140
+ }));
141
+ });
142
+ }).catch (error => {
143
+ console .error (" Error fetching data: " , error);
144
+ });
145
+ }
146
+
30
147
31
148
$ (document ).ready (function () {
32
149
33
- var apiKey = " {{ request.user.api_key }}" ;
34
150
var formName = " {{ form_name }}" ;
35
151
var documentId = " {{ document_id }}" ;
36
152
@@ -80,27 +196,26 @@ $(document).ready(function() {
80
196
// Call fetchDocumentData on page load
81
197
fetchDocumentData ();
82
198
199
+ // var elements = document.querySelectorAll('.data-lookup');
83
200
84
- // Initialize Bootstrap tooltips
85
- $ (' [data-bs-toggle="tooltip"]' ).tooltip ();
86
-
201
+ // Iterate over each element and call getLookup with appropriate parameters
202
+ // elements.forEach(function(element) {
203
+ // var dataLink = element.getAttribute('data-link');
204
+ // var id = element.id;
205
+ // getLookup(dataLink, id, element);
206
+ // });
87
207
88
- // Enable the Create button only if there's some data
89
- function toggleCreateButton () {
90
- var isFormFilled = $ (' #dataForm' ).find (' input, textarea, select' ).filter (function () {
91
- return $ .trim ($ (this ).val ()).length > 0 ;
92
- }).length > 0 ;
93
208
94
- $createButton .prop (' disabled' , ! isFormFilled);
95
- }
96
209
97
- // Check form inputs to toggle the create button
98
- $ (' #dataForm ' ).on ( ' keyup change paste ' , ' input, textarea, select ' , toggleCreateButton );
210
+ // Initialize Bootstrap tooltips
211
+ $ (' [data-bs-toggle="tooltip"] ' ).tooltip ( );
99
212
100
213
// Form submission event
101
214
$ (' #dataForm' ).submit (function (event ) {
102
215
event .preventDefault (); // Prevent the form from submitting via the browser
103
216
217
+ // Disable the submit button
218
+ $ (' #createButton' ).prop (' disabled' , true );
104
219
105
220
// Flag to track if the form is valid
106
221
let formIsValid = true ;
@@ -126,6 +241,9 @@ $(document).ready(function() {
126
241
127
242
// Prevent form submission if validation fails
128
243
if (! formIsValid) {
244
+
245
+ // Re-enable the submit button
246
+ $ (' #createButton' ).prop (' disabled' , false );
129
247
event .preventDefault ();
130
248
131
249
} else {
@@ -146,10 +264,16 @@ $(document).ready(function() {
146
264
}
147
265
});
148
266
149
- // Assuming the user's API key is added in the form as a hidden field or accessible in some other way
150
- var apiKey = " {{ request.user.api_key }}" ;
267
+ $ (' #dataForm' ).find (' input:radio' ).each (function () {
268
+ if ($ (this ).is (' :checked' )) {
269
+ formData[this .name ] = $ (this ).val ();
270
+ }
271
+ });
272
+
151
273
var formName = " {{ form_name }}" ;
152
274
275
+ // console.log(formData);
276
+
153
277
$ .ajax ({
154
278
url: ` /api/form/create/${ formName} ` ,
155
279
type: ' POST' ,
@@ -161,22 +285,19 @@ $(document).ready(function() {
161
285
dataType: ' json' ,
162
286
success : function (response ) {
163
287
// Redirect to the read_one page with the form_name and document_id from the response
164
- setFlashMessage (" Successfully updated form submission" , AlertCategories .SUCCESS );
288
+ setFlashMessage (" Successfully created form submission" , AlertCategories .SUCCESS );
165
289
166
- // We purposefully delay for a second before relocating to the new document
167
- setTimeout (function () {
168
- window .location .href = ` /ui/form/read_one/${ formName} /${ response .document_id } ` ;
169
- }, 1000 );
290
+
291
+ window .location .href = ` /ui/form/read_one/${ formName} /${ response .document_id } ` ;
170
292
171
293
},
172
294
error : function (xhr ) {
173
- // Handle errors, e.g., show an error message
295
+
296
+ // Re-enable the submit button
297
+ $ (' #createButton' ).prop (' disabled' , false );
298
+
174
299
console .error (' Form creation failed' , xhr .responseText );
175
- // Implement flashMessage or another way to show errors to the user
176
300
flashMessage (xhr .responseText , ' warning' );
177
-
178
- // Optionally re-enable the Create button here
179
- $createButton .prop (' disabled' , false );
180
301
}
181
302
});
182
303
}
0 commit comments