Code Duplication    Length = 84-84 lines in 2 locations

resources/js/widget/mixins/form.js 1 location

@@ 1-84 (lines=84) @@
1
import axios from 'axios'
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)) {
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

resources/js/dashboard/mixins/form.js 1 location

@@ 1-84 (lines=84) @@
1
import axios from 'axios'
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)) {
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