1414 * @since 2014-03-21
1515 */
1616
17- import { pushNotification } from '../utils' ;
17+ import { pushNotification } from '../../../../assets/src/utils' ;
18+ import { Response } from '../interfaces' ;
1819
19- export const fetchActivateInput = async ( csrf , formId , inputId , checked ) => {
20+ export const fetchActivateInput = async (
21+ csrf : string ,
22+ formId : string ,
23+ inputId : string ,
24+ checked : boolean
25+ ) : Promise < void > => {
2026 try {
2127 const response = await fetch ( 'api/forms/activate' , {
2228 method : 'POST' ,
@@ -33,22 +39,27 @@ export const fetchActivateInput = async (csrf, formId, inputId, checked) => {
3339 } ) ;
3440
3541 if ( response . ok ) {
36- const result = await response . json ( ) ;
42+ const result : Response = await response . json ( ) ;
3743 if ( result . success ) {
38- pushNotification ( result . success ) ;
44+ pushNotification ( result . success ) ; // @todo move that to the forms.ts file in the content folder
3945 } else {
4046 console . error ( result . error ) ;
4147 }
4248 } else {
43- throw new Error ( 'Network response was not ok: ' , response . text ( ) ) ;
49+ throw new Error ( 'Network response was not ok: ' + ( await response . text ( ) ) ) ;
4450 }
4551 } catch ( error ) {
4652 console . error ( 'Error activating/deactivating input:' , error ) ;
4753 throw error ;
4854 }
4955} ;
5056
51- export const fetchSetInputAsRequired = async ( csrf , formId , inputId , checked ) => {
57+ export const fetchSetInputAsRequired = async (
58+ csrf : string ,
59+ formId : string ,
60+ inputId : string ,
61+ checked : boolean
62+ ) : Promise < void > => {
5263 try {
5364 const response = await fetch ( 'api/forms/required' , {
5465 method : 'POST' ,
@@ -65,22 +76,28 @@ export const fetchSetInputAsRequired = async (csrf, formId, inputId, checked) =>
6576 } ) ;
6677
6778 if ( response . ok ) {
68- const result = await response . json ( ) ;
79+ const result : Response = await response . json ( ) ;
6980 if ( result . success ) {
70- pushNotification ( result . success ) ;
81+ pushNotification ( result . success ) ; // @todo move that to the forms.ts file in the content folder
7182 } else {
7283 console . error ( result . error ) ;
7384 }
7485 } else {
75- throw new Error ( 'Network response was not ok: ' , response . text ( ) ) ;
86+ throw new Error ( 'Network response was not ok: ' + ( await response . text ( ) ) ) ;
7687 }
7788 } catch ( error ) {
7889 console . error ( 'Error setting input as required:' , error ) ;
7990 throw error ;
8091 }
8192} ;
8293
83- export const fetchEditTranslation = async ( csrf , formId , inputId , label , lang ) => {
94+ export const fetchEditTranslation = async (
95+ csrf : string ,
96+ formId : string ,
97+ inputId : string ,
98+ label : string ,
99+ lang : string
100+ ) : Promise < void > => {
84101 try {
85102 const response = await fetch ( 'api/forms/translation-edit' , {
86103 method : 'POST' ,
@@ -98,22 +115,28 @@ export const fetchEditTranslation = async (csrf, formId, inputId, label, lang) =
98115 } ) ;
99116
100117 if ( response . ok ) {
101- const result = await response . json ( ) ;
118+ const result : Response = await response . json ( ) ;
102119 if ( result . success ) {
103- pushNotification ( result . success ) ;
120+ pushNotification ( result . success ) ; // @todo move that to the forms.ts file in the content folder
104121 } else {
105122 console . error ( result . error ) ;
106123 }
107124 } else {
108- throw new Error ( 'Network response was not ok: ' , response . text ( ) ) ;
125+ throw new Error ( 'Network response was not ok: ' + ( await response . text ( ) ) ) ;
109126 }
110127 } catch ( error ) {
111128 console . error ( 'Error editing translation:' , error ) ;
112129 throw error ;
113130 }
114131} ;
115132
116- export const fetchDeleteTranslation = async ( csrf , formId , inputId , lang , element ) => {
133+ export const fetchDeleteTranslation = async (
134+ csrf : string ,
135+ formId : string ,
136+ inputId : string ,
137+ lang : string ,
138+ element : HTMLElement
139+ ) : Promise < void > => {
117140 try {
118141 const response = await fetch ( 'api/forms/translation-delete' , {
119142 method : 'POST' ,
@@ -130,26 +153,33 @@ export const fetchDeleteTranslation = async (csrf, formId, inputId, lang, elemen
130153 } ) ;
131154
132155 if ( response . ok ) {
133- const result = await response . json ( ) ;
156+ const result : Response = await response . json ( ) ;
134157 if ( result . success ) {
158+ // @todo move that to the forms.ts file in the content folder
135159 pushNotification ( result . success ) ;
136- document . getElementById ( 'item_' + element . getAttribute ( 'data-pmf-lang' ) ) . remove ( ) ;
160+ document . getElementById ( 'item_' + element . getAttribute ( 'data-pmf-lang' ) ) ? .remove ( ) ;
137161 const option = document . createElement ( 'option' ) ;
138- option . innerText = element . getAttribute ( 'data-pmf-langname' ) ;
139- document . getElementById ( 'languageSelect' ) . appendChild ( option ) ;
162+ option . innerText = element . getAttribute ( 'data-pmf-langname' ) ! ;
163+ document . getElementById ( 'languageSelect' ) ? .appendChild ( option ) ;
140164 } else {
141165 console . error ( result . error ) ;
142166 }
143167 } else {
144- throw new Error ( 'Network response was not ok: ' , response . text ( ) ) ;
168+ throw new Error ( 'Network response was not ok: ' + ( await response . text ( ) ) ) ;
145169 }
146170 } catch ( error ) {
147171 console . error ( 'Error deleting translation:' , error ) ;
148172 throw error ;
149173 }
150174} ;
151175
152- export const fetchAddTranslation = async ( csrf , formId , inputId , lang , translation ) => {
176+ export const fetchAddTranslation = async (
177+ csrf : string ,
178+ formId : string ,
179+ inputId : string ,
180+ lang : string ,
181+ translation : string
182+ ) : Promise < void > => {
153183 try {
154184 const response = await fetch ( 'api/forms/translation-add' , {
155185 method : 'POST' ,
@@ -167,8 +197,9 @@ export const fetchAddTranslation = async (csrf, formId, inputId, lang, translati
167197 } ) ;
168198
169199 if ( response . ok ) {
170- const result = await response . json ( ) ;
200+ const result : Response = await response . json ( ) ;
171201 if ( result . success ) {
202+ // @todo move that to the forms.ts file in the content folder
172203 pushNotification ( result . success ) ;
173204 setTimeout ( function ( ) {
174205 window . location . reload ( ) ;
@@ -177,9 +208,9 @@ export const fetchAddTranslation = async (csrf, formId, inputId, lang, translati
177208 console . error ( result . error ) ;
178209 }
179210 } else {
180- throw new Error ( 'Network response was not ok: ' , response . text ( ) ) ;
211+ throw new Error ( 'Network response was not ok: ' + ( await response . text ( ) ) ) ;
181212 }
182- } catch {
213+ } catch ( error ) {
183214 console . error ( 'Error adding translation:' , error ) ;
184215 throw error ;
185216 }
0 commit comments