blob: 5fcf938b3a4e11962958a663d2c137185c42c22c [file] [log] [blame]
Sukanya Pandey9055d982020-03-31 17:37:53 +05301<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 Gapski471f2e02020-07-27 14:43:26 +020021 <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 Pandey9055d982020-03-31 17:37:53 +053026 >
Mateusz Gapski471f2e02020-07-27 14:43:26 +020027 <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 Pandey9055d982020-03-31 17:37:53 +053037
Mateusz Gapski471f2e02020-07-27 14:43:26 +020038 <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 Montague602e98a2020-10-21 16:20:00 -050049 max: 10000,
Mateusz Gapski471f2e02020-07-27 14:43:26 +020050 })
51 }}
52 </b-form-text>
Sukanya Pandey9055d982020-03-31 17:37:53 +053053
Mateusz Gapski471f2e02020-07-27 14:43:26 +020054 <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 Pandey9055d982020-03-31 17:37:53 +053063
Mateusz Gapski471f2e02020-07-27 14:43:26 +020064 <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 Pandey9055d982020-03-31 17:37:53 +053075
Mateusz Gapski471f2e02020-07-27 14:43:26 +020076 <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 Pandey9055d982020-03-31 17:37:53 +053084 </b-form>
85 </b-container>
86</template>
87
88<script>
89import PageTitle from '@/components/Global/PageTitle';
Sukanya Pandey8cb0d252020-06-14 20:28:56 +053090import LoadingBarMixin from '@/components/Mixins/LoadingBarMixin';
Sukanya Pandey9055d982020-03-31 17:37:53 +053091import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
92import BVToastMixin from '@/components/Mixins/BVToastMixin';
93import { requiredIf, between } from 'vuelidate/lib/validators';
94import { mapGetters } from 'vuex';
95
96export default {
97 name: 'ManagePowerUsage',
98 components: { PageTitle },
Sukanya Pandey8cb0d252020-06-14 20:28:56 +053099 mixins: [VuelidateMixin, BVToastMixin, LoadingBarMixin],
Derick Montague602e98a2020-10-21 16:20:00 -0500100 beforeRouteLeave(to, from, next) {
101 this.hideLoader();
102 next();
103 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530104 computed: {
105 ...mapGetters({
Derick Montague602e98a2020-10-21 16:20:00 -0500106 powerConsumptionValue: 'powerControl/powerConsumptionValue',
Sukanya Pandey9055d982020-03-31 17:37:53 +0530107 }),
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 Montague602e98a2020-10-21 16:20:00 -0500121 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530122 },
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 Montague602e98a2020-10-21 16:20:00 -0500130 },
131 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530132 },
Sukanya Pandey8cb0d252020-06-14 20:28:56 +0530133 created() {
134 this.startLoader();
135 this.$store
136 .dispatch('powerControl/getPowerControl')
137 .finally(() => this.endLoader());
138 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530139 validations: {
140 powerCapValue: {
141 between: between(1, 10000),
Derick Montague602e98a2020-10-21 16:20:00 -0500142 required: requiredIf(function () {
Sukanya Pandey9055d982020-03-31 17:37:53 +0530143 return this.isPowerCapFieldEnabled;
Derick Montague602e98a2020-10-21 16:20:00 -0500144 }),
145 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530146 },
147 methods: {
148 submitForm() {
149 this.$v.$touch();
150 if (this.$v.$invalid) return;
Sukanya Pandey8cb0d252020-06-14 20:28:56 +0530151 this.startLoader();
Sukanya Pandey9055d982020-03-31 17:37:53 +0530152 this.$store
153 .dispatch('powerControl/setPowerControl', this.powerCapValue)
Derick Montague602e98a2020-10-21 16:20:00 -0500154 .then((message) => this.successToast(message))
Sukanya Pandey8cb0d252020-06-14 20:28:56 +0530155 .catch(({ message }) => this.errorToast(message))
156 .finally(() => this.endLoader());
Derick Montague602e98a2020-10-21 16:20:00 -0500157 },
158 },
Sukanya Pandey9055d982020-03-31 17:37:53 +0530159};
160</script>