resources/js/dashboard/mixins/form.js   A
last analyzed

Complexity

Total Complexity 18
Complexity/F 2

Size

Lines of Code 84
Function Count 9

Duplication

Duplicated Lines 84
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 53
nc 1
dl 84
loc 84
c 0
b 0
f 0
cc 0
rs 10
wmc 18
mnd 2
bc 17
fnc 9
bpm 1.8888
cpm 2
noi 1

8 Functions

Rating   Name   Duplication   Size   Complexity  
A ???.methods.feedback 5 5 2
A ???.data 6 6 1
A ???.computed.isNew 3 3 1
B ???.methods.onSubmit 35 35 5
A ???.methods.state 6 6 3
A ???.methods.onModelChanged 1 1 1
A ???.methods.fetchData 16 16 2
A ???.created 3 3 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1 View Code Duplication
import axios from 'axios'
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
2
3
export default {
4
  props: ['id'],
5
  data() {
6
    return {
7
      validation: {},
8
      pending: false
9
    }
10
  },
11
  computed: {
12
    isNew() {
13
      return this.id === undefined
14
    }
15
  },
16
  methods: {
17
    async fetchData() {
18
      if (!this.isNew) {
19
        let { data } = await axios.get(
20
          this.$app.route(`admin.${this.resourceRoute}.show`, {
21
            [this.modelName]: this.id
22
          })
23
        )
24
25
        Object.keys(data).forEach(key => {
26
          if (key in this.model) {
27
            this.model[key] = data[key]
28
          }
29
        })
30
        this.onModelChanged()
31
      }
32
    },
33
    onModelChanged() {},
34
    feedback(name) {
35
      if (this.state(name)) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this.state(name) is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
36
        return this.validation.errors[name][0]
37
      }
38
    },
39
    state(name) {
40
      return this.validation.errors !== undefined &&
41
        this.validation.errors.hasOwnProperty(name)
42
        ? 'invalid'
43
        : null
44
    },
45
    async onSubmit() {
46
      this.pending = true
47
      let router = this.$router
48
      let action = this.isNew
49
        ? this.$app.route(`admin.${this.resourceRoute}.store`)
50
        : this.$app.route(`admin.${this.resourceRoute}.update`, {
51
            [this.modelName]: this.id
52
          })
53
54
      let formData = this.$app.objectToFormData(this.model)
55
56
      if (!this.isNew) {
57
        formData.append('_method', 'PATCH')
58
      }
59
60
      try {
61
        let { data } = await axios.post(action, formData)
62
        this.pending = false
63
64
        this.$app.noty[data.status](data.message)
65
        if (this.listPath) {
66
          router.push(this.listPath)
67
        }
68
      } catch (e) {
69
        this.pending = false
70
71
        // Validation errors
72
        if (e.response.status === 422) {
73
          this.validation = e.response.data
74
          return
75
        }
76
77
        this.$app.error(e)
78
      }
79
    }
80
  },
81
  created() {
82
    this.fetchData()
83
  }
84
}
85