<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikita</title>
    <description>The latest articles on DEV Community by Nikita (@n2kkrd).</description>
    <link>https://dev.to/n2kkrd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F899548%2F534f5d52-5e19-40f2-8b2f-454fa87b9c22.jpeg</url>
      <title>DEV Community: Nikita</title>
      <link>https://dev.to/n2kkrd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/n2kkrd"/>
    <language>en</language>
    <item>
      <title>Problem with uploading data from Vuex storage</title>
      <dc:creator>Nikita</dc:creator>
      <pubDate>Sat, 13 May 2023 22:12:31 +0000</pubDate>
      <link>https://dev.to/n2kkrd/problem-with-uploading-data-from-vuex-storage-11j</link>
      <guid>https://dev.to/n2kkrd/problem-with-uploading-data-from-vuex-storage-11j</guid>
      <description>&lt;p&gt;I have a ready-made implementation of displaying information about a Vue student in the form of a grid table. Also I need to display the same data only as a bar chart and pie chart. I decided to implement this using the library Chart.js . I have implemented the component StudentChar.vue and its display SecondView.vue. At the moment, StudentChart looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
    &amp;lt;v-container&amp;gt;
      &amp;lt;v-row&amp;gt;
        &amp;lt;v-col cols="6"&amp;gt;
          &amp;lt;v-card&amp;gt;
            &amp;lt;v-card-title&amp;gt;Круговая диаграмма распределения оценок&amp;lt;/v-card-title&amp;gt;
            &amp;lt;v-card-text&amp;gt;
              &amp;lt;canvas ref="pieChart" width="400" height="400"&amp;gt;&amp;lt;/canvas&amp;gt;
            &amp;lt;/v-card-text&amp;gt;
          &amp;lt;/v-card&amp;gt;
        &amp;lt;/v-col&amp;gt;
        &amp;lt;v-col cols="6"&amp;gt;
          &amp;lt;v-card&amp;gt;
            &amp;lt;v-card-title&amp;gt;Столбчатая диаграмма распределения оценок по курсам&amp;lt;/v-card-title&amp;gt;
            &amp;lt;v-card-text&amp;gt;
              &amp;lt;canvas ref="barChart" width="400" height="400"&amp;gt;&amp;lt;/canvas&amp;gt;
            &amp;lt;/v-card-text&amp;gt;
          &amp;lt;/v-card&amp;gt;
        &amp;lt;/v-col&amp;gt;
      &amp;lt;/v-row&amp;gt;
    &amp;lt;/v-container&amp;gt;
  &amp;lt;/template&amp;gt;

  &amp;lt;script&amp;gt;
  import Chart from 'chart.js';

  export default {
    name: 'StudentChart',
    data() {
      return {
        pieData: [],
        barData: [],
        pieChart: null,
        barChart: null,
      };
    },
    async created() {
      await this.loadData();
    },
    methods: {
      async loadData() {
        this.pieData = await this.$store.dispatch('grade/getGrades');
        this.barData = await this.$store.dispatch('grade/getCourses');
        this.createPieChart();
        this.createBarChart();
      },
      createPieChart() {
        const data = {
          labels: this.pieData.map((grade) =&amp;gt; grade.studentName),
          datasets: [
            {
              data: this.pieData.map((grade) =&amp;gt; grade.grade),
              backgroundColor: [
                '#FF6384',
                '#36A2EB',
                '#FFCE56',
                '#8DFF00',
                '#FF5733',
                '#C41E3D',
              ],
              hoverBackgroundColor: [
                '#FF6384',
                '#36A2EB',
                '#FFCE56',
                '#8DFF00',
                '#FF5733',
                '#C41E3D',
              ],
            },
          ],
        };
        const options = {
          responsive: true,
          maintainAspectRatio: false,
          legend: {
            display: true,
            position: 'right',
            align: 'center',
            labels: {
              boxWidth: 20,
              fontSize: 12,
            },
          },
          tooltips: {
            mode: 'index',
            intersect: false,
          },
        };
        this.pieChart = new Chart(this.$refs.pieChart, {
          type: 'pie',
          data,
          options,
        });
      },
      createBarChart() {
        const data = {
          labels: this.barData.map((course) =&amp;gt; course.courseName),
          datasets: [
            {
              label: 'Оценки',
              backgroundColor: '#4CAF50',
              data: this.barData.map((course) =&amp;gt; course.averageGrade),
            },
          ],
        };
        const options = {
          responsive: true,
          maintainAspectRatio: false,
          scales: {
            yAxes: [
              {
                ticks: {
                  beginAtZero: true,
                },
              },
            ],
          },
          legend: {
            display: false,
          },
          tooltips: {
            mode: 'index',
            intersect: false,
          },
        };
        this.barChart = new Chart(this.$refs.barChart, {
          type: 'bar',
          data,
          options,
        });
      },
    },
  };
  &amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But the data is not loaded from it, but for example in the first implementation they are loaded: GrdGrid.vue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;template&amp;gt;
    &amp;lt;v-data-table
        :headers="headers"
        :items="actualGrades"
        :items-per-page="15"
        item-key="code"
        class="elevation-3"
    &amp;gt;

        &amp;lt;template v-slot:top&amp;gt;
            &amp;lt;v-toolbar dense&amp;gt;
                &amp;lt;v-toolbar-title&amp;gt;Таблица грейдов&amp;lt;/v-toolbar-title&amp;gt;
                &amp;lt;v-spacer&amp;gt;&amp;lt;/v-spacer&amp;gt;
                &amp;lt;v-btn class="primary text-lg-button"
                       @click="$store.state.grade.newGradeDialog = true"
                       x-small
                       fab
                &amp;gt;
                +&amp;lt;/v-btn&amp;gt;
            &amp;lt;/v-toolbar&amp;gt;
        &amp;lt;/template&amp;gt;

        &amp;lt;template v-slot:item.grade="editGrade"&amp;gt;
            &amp;lt;v-edit-dialog
                :return-value.sync="editGrade.item.grade"
                @save="save(editGrade)"
                @open="open(editGrade.item.grade)"
            &amp;gt;
                {{ editGrade.item.grade }}

                &amp;lt;template v-slot:input&amp;gt;
                    &amp;lt;v-text-field
                        v-model="editGrade.item.grade"
                        :rules="[rules.grade]"
                        label="Грейд"
                    &amp;gt;&amp;lt;/v-text-field&amp;gt;
                &amp;lt;/template&amp;gt;
            &amp;lt;/v-edit-dialog&amp;gt;
        &amp;lt;/template&amp;gt;

        &amp;lt;template v-slot:item.actions="item"&amp;gt;
            &amp;lt;v-icon @click="deleteItem(item.item)"&amp;gt;mdi-delete&amp;lt;/v-icon&amp;gt;
        &amp;lt;/template&amp;gt;

        &amp;lt;template v-slot:no-data v-if="!$store.state.isLoading"&amp;gt;
            &amp;lt;v-btn small color="primary" @click="initData"&amp;gt;
                Восстановить данные
            &amp;lt;/v-btn&amp;gt;
        &amp;lt;/template&amp;gt;
    &amp;lt;/v-data-table&amp;gt;
&amp;lt;/template&amp;gt;

&amp;lt;script&amp;gt;
export default {
    name: "GrdGrid",

    data() {
        return {
            headers: [
                {text: "Код", value: "code"},
                {text: "Курс", value: "courseName"},
                {text: "ФИО", value: "studentName"},
                {text: "Грейд", value: "grade"},
                {text: "Дата", value: "formatGradeDate", align: "center"},
                {text: "Удалить", value: "actions", align: "center", sortable: false}
            ],

            rules: {
                grade: val =&amp;gt; (val &amp;amp;&amp;amp; !isNaN(val) &amp;amp;&amp;amp; val &amp;gt;= 0 &amp;amp;&amp;amp; val &amp;lt;= 25) || "число от 0 до 25"
            },

            currentGradeValue: null
        };
    },

    methods: {
        open(value) {
            this.currentGradeValue = value;
        },
        async save(edit) {
            let isError = 1;
            if (this.rules.grade(edit.item.grade) === true) {
                try {
                    edit.item.grade = Number(edit.item.grade);
                    isError = (await this.$store.dispatch("grade/putGrade", edit.item)).resultCode;

                } catch (error) {
                    console.error(error);
                }
            }

            setTimeout(() =&amp;gt; (edit.item.grade = isError ? this.currentGradeValue : Number(edit.item.grade)));
        },

        deleteItem(grade) {
            this.$store.dispatch("grade/deleteGrade", grade);
        },

        initData() {
            this.$store.dispatch("grade/initData");
        },
    },

    computed: {
        actualGrades() {
            return this.$store.state.grade.grades.filter(grade =&amp;gt; !grade.isDelete);
        }
    },

    async mounted() {
        await this.$store.dispatch("grade/getCourses");
        await this.$store.dispatch("student/getStudents");
        await this.$store.dispatch("grade/getGrades");
    }
};
&amp;lt;/script&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;what could be the problem? The file grade.js from the store looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import api from '@/store/api'

import Grade from '../../model/Grade'
import Course from '../../model/Course'

export default {
    namespaced: true,

    state: {
        courses: new Map(),
        grades: [],
        newGradeDialog: false
    },

    mutations: {
        setCourses(state, courses) {
            state.courses.clear();
            courses.forEach(course =&amp;gt; {
                state.courses.set(
                    course.code, 
                    new Course(
                        course.code,
                        course.name,
                        course.dateStart,
                        course.dateEnd
                    )
                );
            });
        },
        setGrades(state, grades) {
            state.grades = grades.map(grade =&amp;gt; {
                return new Grade(
                    grade.code,
                    grade.courseCode,
                    grade.studentCode,
                    grade.grade,
                    grade.gradeDate,
                    grade.isDelete
                );
            });
        },

        postGrade(state, grade) {
            state.grades.push(
                new Grade(
                    grade.code,
                    grade.courseCode,
                    grade.studentCode,
                    grade.grade,
                    grade.gradeDate,
                    grade.isDelete
                )
            );
        },
        deleteGrade(state, grade) {
            const index = state.grades.indexOf(grade);

            grade.isDelete = 1;
            state.grades.splice(index, 1, grade);
        }
    },

    actions: {
        async getCourses(context) {
            context.commit('setCourses', await api.course());
        },

        async getGrades(context) {
            context.commit('setGrades', await api.grade());
        },

        async postGrade(context, grade) {
            context.commit('postGrade', await api.postGrade(grade));
        },

        async putGrade(context, grade) {
            return await api.putGrade(grade.code, grade);
        },

        async deleteGrade(context, grade) {
            if(!(await api.deleteGrade(grade.code)).resultCode) {
                context.commit('deleteGrade', grade);
            }
        },

        async initData(context) {
            context.commit('setGrades', await api.initData());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've tried writing methods for loading data in different ways, but they all don't work. Also, for verification, I output the values of pie data and barbara in the methods to the console. They turned out to be undefined&lt;/p&gt;

</description>
      <category>vue</category>
    </item>
  </channel>
</rss>
