Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 1 | <template> |
| 2 | <b-container fluid="xl"> |
| 3 | <page-title :description="$t('pageManagePowerUsage.description')" /> |
| 4 | |
| 5 | <b-row> |
| 6 | <b-col sm="8" md="6" xl="12"> |
| 7 | <dl> |
| 8 | <dt>{{ $t('pageManagePowerUsage.powerConsumption') }}</dt> |
| 9 | <dd> |
| 10 | {{ |
| 11 | powerConsumptionValue |
| 12 | ? `${powerConsumptionValue} W` |
| 13 | : $t('global.status.notAvailable') |
| 14 | }} |
| 15 | </dd> |
| 16 | </dl> |
| 17 | </b-col> |
| 18 | </b-row> |
| 19 | |
| 20 | <b-form @submit.prevent="submitForm"> |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 21 | <b-form-group :disabled="loading"> |
| 22 | <b-row> |
| 23 | <b-col sm="8" md="6" xl="12"> |
| 24 | <b-form-group |
| 25 | :label="$t('pageManagePowerUsage.powerCapSettingLabel')" |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 26 | > |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 27 | <b-form-checkbox |
| 28 | v-model="isPowerCapFieldEnabled" |
| 29 | data-test-id="managePowerUsage-checkbox-togglePowerCapField" |
| 30 | name="power-cap-setting" |
| 31 | > |
| 32 | {{ $t('pageManagePowerUsage.powerCapSettingData') }} |
| 33 | </b-form-checkbox> |
| 34 | </b-form-group> |
| 35 | </b-col> |
| 36 | </b-row> |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 37 | |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 38 | <b-row> |
| 39 | <b-col sm="8" md="6" xl="3"> |
| 40 | <b-form-group |
| 41 | id="input-group-1" |
| 42 | :label="$t('pageManagePowerUsage.powerCapLabel')" |
| 43 | label-for="input-1" |
| 44 | > |
| 45 | <b-form-text id="power-help-text"> |
| 46 | {{ |
| 47 | $t('pageManagePowerUsage.powerCapLabelTextInfo', { |
| 48 | min: 1, |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 49 | max: 10000, |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 50 | }) |
| 51 | }} |
| 52 | </b-form-text> |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 53 | |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 54 | <b-form-input |
| 55 | id="input-1" |
| 56 | v-model.number="powerCapValue" |
| 57 | :disabled="!isPowerCapFieldEnabled" |
| 58 | data-test-id="managePowerUsage-input-powerCapValue" |
| 59 | type="number" |
| 60 | aria-describedby="power-help-text" |
| 61 | :state="getValidationState($v.powerCapValue)" |
| 62 | ></b-form-input> |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 63 | |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 64 | <b-form-invalid-feedback id="input-live-feedback" role="alert"> |
| 65 | <template v-if="!$v.powerCapValue.required"> |
| 66 | {{ $t('global.form.fieldRequired') }} |
| 67 | </template> |
| 68 | <template v-else-if="!$v.powerCapValue.between"> |
| 69 | {{ $t('global.form.invalidValue') }} |
| 70 | </template> |
| 71 | </b-form-invalid-feedback> |
| 72 | </b-form-group> |
| 73 | </b-col> |
| 74 | </b-row> |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 75 | |
Mateusz Gapski | 471f2e0 | 2020-07-27 14:43:26 +0200 | [diff] [blame] | 76 | <b-button |
| 77 | variant="primary" |
| 78 | type="submit" |
| 79 | data-test-id="managePowerUsage-button-savePowerCapValue" |
| 80 | > |
| 81 | {{ $t('global.action.save') }} |
| 82 | </b-button> |
| 83 | </b-form-group> |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 84 | </b-form> |
| 85 | </b-container> |
| 86 | </template> |
| 87 | |
| 88 | <script> |
| 89 | import PageTitle from '@/components/Global/PageTitle'; |
Sukanya Pandey | 8cb0d25 | 2020-06-14 20:28:56 +0530 | [diff] [blame] | 90 | import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin'; |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 91 | import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js'; |
| 92 | import BVToastMixin from '@/components/Mixins/BVToastMixin'; |
| 93 | import { requiredIf, between } from 'vuelidate/lib/validators'; |
| 94 | import { mapGetters } from 'vuex'; |
| 95 | |
| 96 | export default { |
| 97 | name: 'ManagePowerUsage', |
| 98 | components: { PageTitle }, |
Sukanya Pandey | 8cb0d25 | 2020-06-14 20:28:56 +0530 | [diff] [blame] | 99 | mixins: [VuelidateMixin, BVToastMixin, LoadingBarMixin], |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 100 | beforeRouteLeave(to, from, next) { |
| 101 | this.hideLoader(); |
| 102 | next(); |
| 103 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 104 | computed: { |
| 105 | ...mapGetters({ |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 106 | powerConsumptionValue: 'powerControl/powerConsumptionValue', |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 107 | }), |
| 108 | |
| 109 | /** |
| 110 | Computed property isPowerCapFieldEnabled is used to enable or disable the input field. |
| 111 | The input field is enabled when the powercapValue property is not null. |
| 112 | **/ |
| 113 | isPowerCapFieldEnabled: { |
| 114 | get() { |
| 115 | return this.powerCapValue !== null; |
| 116 | }, |
| 117 | set(value) { |
| 118 | let newValue = value ? '' : null; |
| 119 | this.$v.$reset(); |
| 120 | this.$store.dispatch('powerControl/setPowerCapUpdatedValue', newValue); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 121 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 122 | }, |
| 123 | powerCapValue: { |
| 124 | get() { |
| 125 | return this.$store.getters['powerControl/powerCapValue']; |
| 126 | }, |
| 127 | set(value) { |
| 128 | this.$v.$touch(); |
| 129 | this.$store.dispatch('powerControl/setPowerCapUpdatedValue', value); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 130 | }, |
| 131 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 132 | }, |
Sukanya Pandey | 8cb0d25 | 2020-06-14 20:28:56 +0530 | [diff] [blame] | 133 | created() { |
| 134 | this.startLoader(); |
| 135 | this.$store |
| 136 | .dispatch('powerControl/getPowerControl') |
| 137 | .finally(() => this.endLoader()); |
| 138 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 139 | validations: { |
| 140 | powerCapValue: { |
| 141 | between: between(1, 10000), |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 142 | required: requiredIf(function () { |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 143 | return this.isPowerCapFieldEnabled; |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 144 | }), |
| 145 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 146 | }, |
| 147 | methods: { |
| 148 | submitForm() { |
| 149 | this.$v.$touch(); |
| 150 | if (this.$v.$invalid) return; |
Sukanya Pandey | 8cb0d25 | 2020-06-14 20:28:56 +0530 | [diff] [blame] | 151 | this.startLoader(); |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 152 | this.$store |
| 153 | .dispatch('powerControl/setPowerControl', this.powerCapValue) |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 154 | .then((message) => this.successToast(message)) |
Sukanya Pandey | 8cb0d25 | 2020-06-14 20:28:56 +0530 | [diff] [blame] | 155 | .catch(({ message }) => this.errorToast(message)) |
| 156 | .finally(() => this.endLoader()); |
Derick Montague | 602e98a | 2020-10-21 16:20:00 -0500 | [diff] [blame^] | 157 | }, |
| 158 | }, |
Sukanya Pandey | 9055d98 | 2020-03-31 17:37:53 +0530 | [diff] [blame] | 159 | }; |
| 160 | </script> |