FormComponent.vue 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <template>
  2. <main>
  3. <v-form
  4. ref="form"
  5. v-model="valid"
  6. lazy-validation
  7. >
  8. <v-row>
  9. <slot></slot>
  10. </v-row>
  11. <v-btn
  12. class="mr-4 submitBtn"
  13. @click="submit"
  14. >
  15. submit
  16. </v-btn>
  17. </v-form>
  18. <AlertComponent type="success" v-if="saveOk">Sauvegarde Effectuée</AlertComponent>
  19. <AlertComponent type="error" v-if="saveKo">Sauvegarde Echouée</AlertComponent>
  20. </main>
  21. </template>
  22. <script lang="ts">
  23. import {defineComponent, ref, toRef} from '@vue/composition-api'
  24. import {Repository} from "@vuex-orm/core";
  25. export default defineComponent({
  26. props: {
  27. repository:{
  28. type: Object as () => Repository,
  29. required: true
  30. }
  31. },
  32. setup(){
  33. const valid = true
  34. const saveOk = ref(false)
  35. const saveKo = ref(false)
  36. return {
  37. valid,
  38. saveOk,
  39. saveKo
  40. }
  41. },
  42. methods:{
  43. async submit(){
  44. if(this.$refs.form.validate()){
  45. const model = await this.repository.find(parseInt(this.$route.params.id));
  46. const playload = model.$toJson();
  47. this.$http.$put(`https://local.new.api.opentalent.fr/api/${model.$entity}/${this.$route.params.id}`, playload)
  48. .then(()=>{
  49. this.saveOk=true
  50. }, () =>{
  51. this.saveKo=true
  52. })
  53. setTimeout(()=>{
  54. this.saveOk=false
  55. this.saveKo=false
  56. },2000)
  57. }
  58. }
  59. }
  60. })
  61. </script>
  62. <style scoped>
  63. .submitBtn{
  64. margin-top: 20px;
  65. }
  66. </style>