|
29 | 29 | /** @type {boolean} */ |
30 | 30 | export let validateKey = true; |
31 | 31 | export let validateValue = true; |
| 32 | + export let requireActiveRounds = false; |
32 | 33 |
|
33 | 34 | /** @type {import('$types').UserStateDetailModel[]} */ |
34 | 35 | export let states = []; |
35 | 36 |
|
| 37 | + /** @type {import('$types').UserStateDetailModel} */ |
| 38 | + const defaultState = { |
| 39 | + key: { data: '', isValid: true }, |
| 40 | + value: { data: '', isValid: true }, |
| 41 | + active_rounds: { data: -1, isValid: true } |
| 42 | + }; |
| 43 | +
|
36 | 44 | $: { |
37 | 45 | if (isOpen) { |
38 | 46 | if (states?.length > 0) { |
39 | 47 | states = [...states]; |
40 | 48 | } else { |
41 | | - states = [{ key: { data: '', isValid: true }, value: { data: '', isValid: true } }]; |
| 49 | + states = [{...JSON.parse(JSON.stringify(defaultState))}]; |
42 | 50 | } |
43 | 51 | } |
44 | 52 | } |
45 | 53 |
|
46 | | -
|
47 | 54 | /** @param {any} e */ |
48 | 55 | function handleConfirm(e) { |
49 | 56 | e.preventDefault(); |
|
76 | 83 | } |
77 | 84 |
|
78 | 85 | function addState() { |
79 | | - states = [...states, { key: { data: '', isValid: true }, value: { data: '', isValid: true } }]; |
| 86 | + states = [...states, {...JSON.parse(JSON.stringify(defaultState))}]; |
80 | 87 | } |
81 | 88 |
|
82 | 89 | /** @param {number} index */ |
|
111 | 118 | return state; |
112 | 119 | }); |
113 | 120 | } |
| 121 | +
|
| 122 | + /** |
| 123 | + * @param {any} e |
| 124 | + * @param {number} index |
| 125 | + */ |
| 126 | + function changeActiveRounds(e, index) { |
| 127 | + states = states.map((state, idx) => { |
| 128 | + if (idx === index) { |
| 129 | + state.active_rounds.isValid = true; |
| 130 | + state.active_rounds.data = validateActiveRounds(Number(e.target.value) || 0); |
| 131 | + } |
| 132 | + return state; |
| 133 | + }); |
| 134 | + } |
| 135 | +
|
| 136 | + /** @param {number} rounds */ |
| 137 | + function validateActiveRounds(rounds) { |
| 138 | + let res = rounds; |
| 139 | + const lowerLimit = -1; |
| 140 | + const upperLimit = 9999; |
| 141 | +
|
| 142 | + if (rounds <= 0) { |
| 143 | + res = lowerLimit; |
| 144 | + } else if (rounds > upperLimit) { |
| 145 | + res = upperLimit; |
| 146 | + } |
| 147 | + return res; |
| 148 | + } |
114 | 149 | </script> |
115 | 150 |
|
116 | 151 | <Modal class={className} fade size={size} isOpen={isOpen} toggle={() => toggleModal && toggleModal()}> |
|
119 | 154 | <Form class="state-form"> |
120 | 155 | {#each states as state, idx (idx)} |
121 | 156 | <Row> |
122 | | - <div class="state-input"> |
| 157 | + <div class={`${requireActiveRounds ? 'state-key-input' : 'state-input'}`}> |
123 | 158 | <FormGroup> |
124 | 159 | {#if idx === 0} |
125 | 160 | <label for="key"> |
|
129 | 164 | <Input class={`${!state.key.isValid ? 'invalid' : ''}`} placeholder="Enter a key" value={state.key.data} maxlength={50} on:input={(e) => changeKey(e, idx)} /> |
130 | 165 | </FormGroup> |
131 | 166 | </div> |
132 | | - <div class="state-input"> |
| 167 | + <div class={`${requireActiveRounds ? 'state-key-input' : 'state-input'}`}> |
133 | 168 | <FormGroup> |
134 | 169 | {#if idx === 0} |
135 | 170 | <label for="value"> |
|
139 | 174 | <Input class={`${!state.value.isValid ? 'invalid' : ''}`} placeholder="Enter a value" value={state.value.data} maxlength={1000} on:input={(e) => changeValue(e, idx)} /> |
140 | 175 | </FormGroup> |
141 | 176 | </div> |
| 177 | + {#if requireActiveRounds} |
| 178 | + <div class="state-num-input"> |
| 179 | + <FormGroup> |
| 180 | + {#if idx === 0} |
| 181 | + <label for="value"> |
| 182 | + {`Active rounds (Optional)`} |
| 183 | + </label> |
| 184 | + {/if} |
| 185 | + <Input type="number" placeholder="Enter a value" value={state.active_rounds.data} on:input={(e) => changeActiveRounds(e, idx)} /> |
| 186 | + </FormGroup> |
| 187 | + </div> |
| 188 | + {/if} |
142 | 189 | <div class="state-delete"> |
143 | 190 | {#if idx !== 0} |
144 | 191 | <Button |
|
0 commit comments