blob: 8a16b88b8854a80505bcfcc0cdc6688d70e12ec5 [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">
21 <b-row>
22 <b-col sm="8" md="6" xl="12">
23 <b-form-group
24 :label="$t('pageManagePowerUsage.powerCapSettingLabel')"
25 >
26 <b-form-checkbox
27 v-model="isPowerCapFieldEnabled"
28 name="power-cap-setting"
29 >
30 {{ $t('pageManagePowerUsage.powerCapSettingData') }}
31 </b-form-checkbox>
32 </b-form-group>
33 </b-col>
34 </b-row>
35
36 <b-row>
37 <b-col sm="8" md="6" xl="3">
38 <b-form-group
39 id="input-group-1"
40 :label="$t('pageManagePowerUsage.powerCapLabel')"
41 label-for="input-1"
42 >
43 <b-form-text id="power-help-text">
44 {{
45 $t('pageManagePowerUsage.powerCapLabelTextInfo', {
46 min: 1,
47 max: 10000
48 })
49 }}
50 </b-form-text>
51
52 <b-form-input
53 id="input-1"
54 v-model.number="powerCapValue"
55 :disabled="!isPowerCapFieldEnabled"
56 type="number"
57 aria-describedby="power-help-text"
58 :state="getValidationState($v.powerCapValue)"
59 ></b-form-input>
60
61 <b-form-invalid-feedback id="input-live-feedback" role="alert">
62 <template v-if="!$v.powerCapValue.required">
63 {{ $t('global.form.fieldRequired') }}
64 </template>
65 <template v-else-if="!$v.powerCapValue.between">
66 {{ $t('global.form.invalidValue') }}
67 </template>
68 </b-form-invalid-feedback>
69 </b-form-group>
70 </b-col>
71 </b-row>
72
73 <b-button variant="primary" type="submit">
74 {{ $t('global.action.save') }}
75 </b-button>
76 </b-form>
77 </b-container>
78</template>
79
80<script>
81import PageTitle from '@/components/Global/PageTitle';
82import VuelidateMixin from '@/components/Mixins/VuelidateMixin.js';
83import BVToastMixin from '@/components/Mixins/BVToastMixin';
84import { requiredIf, between } from 'vuelidate/lib/validators';
85import { mapGetters } from 'vuex';
86
87export default {
88 name: 'ManagePowerUsage',
89 components: { PageTitle },
90 mixins: [VuelidateMixin, BVToastMixin],
91 computed: {
92 ...mapGetters({
93 powerConsumptionValue: 'powerControl/powerConsumptionValue'
94 }),
95
96 /**
97 Computed property isPowerCapFieldEnabled is used to enable or disable the input field.
98 The input field is enabled when the powercapValue property is not null.
99 **/
100 isPowerCapFieldEnabled: {
101 get() {
102 return this.powerCapValue !== null;
103 },
104 set(value) {
105 let newValue = value ? '' : null;
106 this.$v.$reset();
107 this.$store.dispatch('powerControl/setPowerCapUpdatedValue', newValue);
108 }
109 },
110 powerCapValue: {
111 get() {
112 return this.$store.getters['powerControl/powerCapValue'];
113 },
114 set(value) {
115 this.$v.$touch();
116 this.$store.dispatch('powerControl/setPowerCapUpdatedValue', value);
117 }
118 }
119 },
120 validations: {
121 powerCapValue: {
122 between: between(1, 10000),
123 required: requiredIf(function() {
124 return this.isPowerCapFieldEnabled;
125 })
126 }
127 },
128 methods: {
129 submitForm() {
130 this.$v.$touch();
131 if (this.$v.$invalid) return;
132 this.$store
133 .dispatch('powerControl/setPowerControl', this.powerCapValue)
134 .then(message => this.successToast(message))
135 .catch(({ message }) => this.errorToast(message));
136 }
137 }
138};
139</script>