From 2015309fba1bab6e2dda749723e9846cdeca6cc8 Mon Sep 17 00:00:00 2001
From: "robinwilliam.hundt" <robinwilliam.hundt@stud.uni-goettingen.de>
Date: Mon, 6 Aug 2018 17:19:00 +0200
Subject: [PATCH] Fixed a bunch possible bugs...

---
 frontend/src/api.ts                           | 18 ++++++---
 frontend/src/store/actions.ts                 |  6 +--
 frontend/src/store/modules/authentication.ts  |  8 ++--
 .../modules/feedback_list/feedback-table.ts   |  2 +-
 .../src/store/modules/submission-notes.ts     |  4 +-
 frontend/src/store/modules/subscriptions.ts   | 39 ++++++++++---------
 frontend/src/store/mutations.ts               | 21 ----------
 frontend/src/util/helpers.ts                  | 11 +++---
 8 files changed, 49 insertions(+), 60 deletions(-)

diff --git a/frontend/src/api.ts b/frontend/src/api.ts
index 029722cd..670854ca 100644
--- a/frontend/src/api.ts
+++ b/frontend/src/api.ts
@@ -1,5 +1,5 @@
 import axios from 'axios'
-import {Credentials} from "@/store/modules/authentication";
+import {Credentials} from '@/store/modules/authentication'
 
 function addFieldsToUrl ({url, fields = []}: {url: string, fields?: string[]}) {
   return fields.length > 0 ? url + '?fields=pk,' + fields : url
@@ -68,7 +68,7 @@ export async function fetchAllStudents (fields: string[] = []) {
 }
 
 export async function fetchStudent ({pk, fields = []}:
-                                      {pk: string, fields?: string[]}) {
+{pk: string, fields?: string[]}) {
   const url = addFieldsToUrl({
     url: `/api/student/${pk}/`,
     fields
@@ -111,7 +111,7 @@ export async function fetchFeedback ({ofSubmission}) {
 }
 
 export async function fetchExamType ({examPk, fields = []}:
-                                       {examPk: string, fields?: string[]}) {
+{examPk?: string, fields?: string[]}) {
   const url = addFieldsToUrl({
     url: `/api/examtype/${examPk !== undefined ? examPk + '/' : ''}`,
     fields})
@@ -126,8 +126,16 @@ export async function fetchStatistics (opt = {fields: []}) {
   return (await ax.get(url)).data
 }
 
-export async function subscribeTo (type, key, stage) {
-  let data = {
+interface SubscriptionPayload {
+  /* eslint-disable */
+  query_type: string,
+  query_key?: string,
+  feedback_stage?: string
+  /* eslint-enable */
+}
+
+export async function subscribeTo (type: string, key: string, stage: string) {
+  let data: SubscriptionPayload = {
     query_type: type
   }
 
diff --git a/frontend/src/store/actions.ts b/frontend/src/store/actions.ts
index b6ce7879..47ca46fc 100644
--- a/frontend/src/store/actions.ts
+++ b/frontend/src/store/actions.ts
@@ -53,9 +53,9 @@ const actions = {
       handleError(err, dispatch, 'Unable to fetch tutor data')
     }
   },
-  async getAllFeedback ({commit, dispatch}, fields = []) {
+  async getAllFeedback ({commit, dispatch}, fields: string[] = []) {
     try {
-      const feedback = await api.fetchAllFeedback({fields})
+      const feedback = await api.fetchAllFeedback(fields)
       commit(mut.SET_ALL_FEEDBACK, feedback)
       commit(mut.MAP_FEEDBACK_OF_SUBMISSION_TYPE)
     } catch (err) {
@@ -110,7 +110,7 @@ const actions = {
     commit('submissionNotes/' + subNotesMut.RESET_STATE)
     commit(authMut.SET_MESSAGE, message)
     router.push({name: 'login'})
-    router.go()
+    router.go(0)
   }
 }
 
diff --git a/frontend/src/store/modules/authentication.ts b/frontend/src/store/modules/authentication.ts
index b0101d5d..9e0c95e7 100644
--- a/frontend/src/store/modules/authentication.ts
+++ b/frontend/src/store/modules/authentication.ts
@@ -1,6 +1,6 @@
 import * as api from '@/api'
 import gradySays from '../grady_speak'
-import {ActionContext, Module} from "vuex";
+import {ActionContext, Module} from 'vuex'
 
 export interface Credentials {
     username: string,
@@ -17,7 +17,7 @@ interface AuthState {
         pk: string,
         username: string,
         role: string,
-        is_admin: boolean
+        is_admin: boolean //eslint-disable-line
     }
 }
 function initialState (): AuthState {
@@ -55,10 +55,10 @@ const authentication: Module<AuthState, any> = {
     isStudent: (state: AuthState) => {
       return state.user.role === 'Student'
     },
-    isTutor: (state: AuthState)=> {
+    isTutor: (state: AuthState) => {
       return state.user.role === 'Tutor'
     },
-    isReviewer: (state: AuthState)=> {
+    isReviewer: (state: AuthState) => {
       return state.user.role === 'Reviewer'
     },
     isTutorOrReviewer: (state: AuthState, getters) => {
diff --git a/frontend/src/store/modules/feedback_list/feedback-table.ts b/frontend/src/store/modules/feedback_list/feedback-table.ts
index 35e526ac..4ea766a8 100644
--- a/frontend/src/store/modules/feedback_list/feedback-table.ts
+++ b/frontend/src/store/modules/feedback_list/feedback-table.ts
@@ -41,7 +41,7 @@ const feedbackTable = {
   actions: {
     mapFeedbackHistOfSubmissionType ({getters, commit, state}) {
       for (const feedback of Object.values(state.feedbackHist)) {
-        const type = getters.getSubmissionType(feedback.of_submission_type)
+        const type = getters.getSubmissionType((feedback as any).of_submission_type)
         commit(feedbackTableMut.SET_FEEDBACK_OF_SUBMISSION_TYPE, {feedback, type})
       }
     },
diff --git a/frontend/src/store/modules/submission-notes.ts b/frontend/src/store/modules/submission-notes.ts
index 08669ce0..cd02557f 100644
--- a/frontend/src/store/modules/submission-notes.ts
+++ b/frontend/src/store/modules/submission-notes.ts
@@ -122,7 +122,7 @@ const submissionNotes = {
     deleteComments: async function ({state}) {
       return Promise.all(
         Object.values(state.commentsMarkedForDeletion).map(comment => {
-          return api.deleteComment(comment)
+          return api.deleteComment((comment as any))
         })
       )
     },
@@ -143,7 +143,7 @@ const submissionNotes = {
       if (!state.hasOrigFeedback) {
         return api.submitFeedbackForAssignment({feedback})
       } else {
-        feedback.pk = state.origFeedback.pk
+        feedback['pk']= state.origFeedback.pk
         return api.submitUpdatedFeedback({feedback})
       }
     }
diff --git a/frontend/src/store/modules/subscriptions.ts b/frontend/src/store/modules/subscriptions.ts
index 531ea809..588867f0 100644
--- a/frontend/src/store/modules/subscriptions.ts
+++ b/frontend/src/store/modules/subscriptions.ts
@@ -50,10 +50,10 @@ const subscriptions = {
       return stages
     },
     availableSubmissionTypeQueryKeys (state, getters, rootState) {
-      return Object.values(rootState.submissionTypes).map(subType => subType.pk)
+      return Object.values(rootState.submissionTypes).map((subType: any) => subType.pk)
     },
     availableExamTypeQueryKeys (state, getters, rootState) {
-      return Object.values(rootState.examTypes).map(examType => examType.pk)
+      return Object.values(rootState.examTypes).map((examType: any) => examType.pk)
     },
     activeSubscription (state) {
       return state.subscriptions[state.activeSubscriptionPk]
@@ -86,25 +86,26 @@ const subscriptions = {
         acc[curr] = subscriptionsByType()
         return acc
       }, {})
-      Object.values(state.subscriptions).forEach(subscription => {
+      Object.values(state.subscriptions).forEach((subscription: any) => {
         subscriptionsByStage[subscription.feedback_stage][subscription.query_type].push(subscription)
       })
       // sort the resulting arrays in subscriptions lexicographically by their query_keys
-      const sortSubscriptions = subscriptionsByType => Object.values(subscriptionsByType).forEach(arr => {
-        if (arr.length > 1 && arr[0].hasOwnProperty('query_key')) {
-          arr.sort((subA, subB) => {
-            const subALower = getters.resolveSubscriptionKeyToName(subA).toLowerCase()
-            const subBLower = getters.resolveSubscriptionKeyToName(subB).toLowerCase()
-            if (subALower < subBLower) {
-              return -1
-            } else if (subALower > subBLower) {
-              return 1
-            } else {
-              return 0
-            }
-          })
-        }
-      })
+      const sortSubscriptions = subscriptionsByType => Object.values(subscriptionsByType)
+        .forEach((arr: object[]) => {
+          if (arr.length > 1 && arr[0].hasOwnProperty('query_key')) {
+            arr.sort((subA, subB) => {
+              const subALower = getters.resolveSubscriptionKeyToName(subA).toLowerCase()
+              const subBLower = getters.resolveSubscriptionKeyToName(subB).toLowerCase()
+              if (subALower < subBLower) {
+                return -1
+              } else if (subALower > subBLower) {
+                return 1
+              } else {
+                return 0
+              }
+            })
+          }
+        })
       Object.values(subscriptionsByStage).forEach(subscriptionsByType => {
         sortSubscriptions(subscriptionsByType)
       })
@@ -186,7 +187,7 @@ const subscriptions = {
       }
     },
     async cleanAssignmentsFromSubscriptions ({commit, state, dispatch}, excludeActive = true) {
-      Object.values(state.subscriptions).forEach(subscription => {
+      Object.values(state.subscriptions).forEach((subscription: any) => {
         if (!excludeActive || subscription.pk !== state.activeSubscriptionPk) {
           subscription.assignments.forEach(assignment => {
             api.deleteAssignment({assignment})
diff --git a/frontend/src/store/mutations.ts b/frontend/src/store/mutations.ts
index 2e4c4081..1c9e80ad 100644
--- a/frontend/src/store/mutations.ts
+++ b/frontend/src/store/mutations.ts
@@ -26,27 +26,6 @@ const mutations = {
       return acc
     }, {})
   },
-  [mut.SET_SUBSCRIPTIONS] (state, subscriptions) {
-    state.subscriptions = subscriptions.reduce((acc, curr) => {
-      acc[curr['pk']] = curr
-      return acc
-    }, {})
-    for (let subscription of Object.values(subscriptions)) {
-      for (let assignment of subscription.assignments) {
-        if (assignment.feedback) {
-          Vue.set(assignment.feedback, 'feedback_stage_for_user', subscription.feedback_stage)
-        }
-      }
-    }
-  },
-  [mut.SET_SUBSCRIPTION] (state, subscription) {
-    Vue.set(state.subscriptions, subscription.pk, subscription)
-    for (let assignment of subscription.assignments) {
-      if (assignment.feedback) {
-        Vue.set(assignment.feedback, 'feedback_stage_for_user', subscription.feedback_stage)
-      }
-    }
-  },
   [mut.SET_STUDENTS] (state, students) {
     state.students = students.reduce((acc, curr) => {
       acc[curr.pk] = mapStudent(curr, state.studentMap)
diff --git a/frontend/src/util/helpers.ts b/frontend/src/util/helpers.ts
index 73e8fc49..1927f50a 100644
--- a/frontend/src/util/helpers.ts
+++ b/frontend/src/util/helpers.ts
@@ -38,8 +38,8 @@ interface GetSetPair {
  * @returns {*}
  */
 export function createComputedGetterSetter (
-    {path, mutation, namespace}:
-        {path: string, mutation: string, namespace:string}): GetSetPair {
+  {path, mutation, namespace}:
+  {path: string, mutation: string, namespace:string}): GetSetPair {
   return {
     get (): any {
       return getObjectValueByPath(vueInstance.$store.state, path)
@@ -68,8 +68,8 @@ interface MappedState {
  * @param items array that contains objects {name, path, mutation}
  */
 export function mapStateToComputedGetterSetter (
-    {namespace = '', pathPrefix = '', items = []}:
-        {namespace: string, pathPrefix: string, items: StateMapperItem[]}): MappedState {
+  {namespace = '', pathPrefix = '', items = []}:
+  {namespace: string, pathPrefix: string, items: StateMapperItem[]}): MappedState {
   return items.reduce((acc: MappedState, curr) => {
     // if no path is give, use name
     const itemPath = curr.path || curr.name
@@ -83,6 +83,7 @@ export function mapStateToComputedGetterSetter (
 // https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript/43053803#43053803
 let cartesianHelper = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))))
 export function cartesian (a, b, ...c) {
+  // @ts-ignore can be ignored since cartesian is only recursively called if b si truthy
   return b ? cartesian(cartesianHelper(a, b), ...c) : a
 }
 
@@ -100,7 +101,7 @@ export function objectifyArray<T> (arr: T[], key = 'pk'): {[key: string]: T} {
   }, {})
 }
 
-export function once (fn, context) {
+export function once (fn: Function, context?: object) {
   let result
   return function () {
     if (!result) {
-- 
GitLab