@@ -81,36 +81,49 @@ define('admin/extend/rewards', [
8181 function selectReward ( el ) {
8282 const parent = el . parents ( '[data-rid]' ) ;
8383 const div = parent . find ( '.inputs' ) ;
84- let inputs ;
85- let html = '' ;
84+ const selectedReward = getReward ( el ) ;
85+ if ( ! selectedReward ) return ;
8686
87+ parent . attr ( 'data-rid' , selectedReward . rid ) ;
88+
89+ const inputs = selectedReward . inputs ;
90+ const html = inputsHtml ( inputs ) ;
91+ if ( ! html ) {
92+ return alerts . error ( '[[admin/extend/rewards:alert.no-inputs-found]] ' + el . attr ( 'data-selected' ) ) ;
93+ }
94+
95+ div . html ( html ) ;
96+ }
97+
98+ //Helper function for selectReward to get the reward
99+ function getReward ( el ) {
87100 const selectedReward = available . find ( reward => reward . rid === el . attr ( 'data-selected' ) ) ;
88- if ( selectedReward ) {
89- inputs = selectedReward . inputs ;
90- parent . attr ( 'data-rid' , selectedReward . rid ) ;
101+ if ( ! selectedReward ) {
102+ return null ;
91103 }
104+ return selectedReward ;
105+ }
92106
107+ //Helper function for selectReward to build the HTML
108+ function inputsHtml ( inputs ) {
93109 if ( ! inputs ) {
94- return alerts . error ( '[[admin/extend/rewards:alert.no-inputs-found]] ' + el . attr ( 'data-selected' ) ) ;
95- }
110+ return alerts . error ( '[[admin/extend/rewards:alert.no-inputs-found]]' ) ;
111+ } ;
96112
97- inputs . forEach ( function ( input ) {
113+ return inputs . map ( input => {
114+ let html = '' ;
98115 html += `<label class="form-label text-nowrap" for="${ input . name } ">${ input . label } <br />` ;
99- switch ( input . type ) {
100- case 'select' :
101- html += `<select class="form-select form-select-sm" name="${ input . name } " >` ;
102- input . values . forEach ( function ( value ) {
103- html += `<option value="${ value . value } ">${ value . name } </option>` ;
104- } ) ;
105- break ;
106- case 'text' :
107- html += `<input type="text" class="form-control form-control-sm" name="${ input . name } " />` ;
108- break ;
116+ if ( input . type == 'text' ) {
117+ html += `<input type="text" class="form-control form-control-sm" name="${ input . name } " />` ;
118+ } else if ( input . type == 'select' ) {
119+ html += `<select class="form-select form-select-sm" name="${ input . name } " >` ;
120+ input . values . forEach ( function ( value ) {
121+ html += `<option value="${ value . value } ">${ value . name } </option>` ;
122+ } ) ;
109123 }
110124 html += '</label>' ;
125+ return html ;
111126 } ) ;
112-
113- div . html ( html ) ;
114127 }
115128
116129 function populateInputs ( ) {
0 commit comments