Guest User

Untitled

a guest
Jun 27th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2.    
  3.     <div  v-if="isLoggedIn" class="column">
  4.         <section class="section">
  5.             <h1 class="title">Edit User</h1>
  6.             <div class="columns is-centered">
  7.                 <div class="column is-half">
  8.                     <form @submit.prevent>
  9.                         <div class="field">
  10.                             <label for="name" class="label">Username</label>
  11.                             <div class="control">
  12.                                 <input type="text" v-model="user.username" class="input" required>
  13.                             </div>
  14.                         </div>
  15.                         <div class="field">
  16.                             <label for="role" class="label">Role</label>
  17.                             <div class="control">
  18.                                 <div class="select">
  19.                                     <select v-model="user.role_id">
  20.                                         <option v-for="role in roles" :value="role.id">
  21.                                             {{ role.name | capitalize }}
  22.                                         </option>
  23.                                     </select>
  24.                                    
  25.                                 </div>
  26.                             </div>
  27.                         </div>                      
  28.                         <div class="field">
  29.                             <button class="button is-primary" @click="open_reset_password_modal(user.id)">Reset Password</button>
  30.                         </div>
  31.                         <div class="field">
  32.                             <label for="name" class="label">Email</label>
  33.                             <div class="control">
  34.                                 <input type="email" v-model="user.email" class="input" required>
  35.                             </div>
  36.                         </div>
  37.                         <div class="field">
  38.                             <label for="name" class="label">Phone Number</label>
  39.                             <div class="control">
  40.                                 <input type="text" v-model="user.phone_number" class="input" required>
  41.                             </div>
  42.                         </div>
  43.                         <div class="field is-grouped">
  44.                             <p class="control">
  45.                                 <button class="button is-primary" :class="{ 'is-loading': isLoading }" @click="updateUser"> Update </button>
  46.                             </p>
  47.                             <p class="control">
  48.                                 <button class="button is-light" @click.prevent="cancel" :disabled="isLoading"> Cancel </button>
  49.                             </p>
  50.                         </div>
  51.                         <div class="field">
  52.                             <p class="help is-danger" v-if="errors">{{errors}}</p>
  53.                         </div>    
  54.                     </form>  
  55.                 </div>
  56.             </div>
  57.         <resetPassword-modal
  58.             @cancel = "close_reset_password_modal"
  59.             @confirm = "resetPassword"
  60.             action = 'is-info'
  61.             :data = "modal_data"
  62.             :isActive = "is_reset_modal_active"
  63.         />
  64.         </section>
  65.     </div>
  66.  
  67. </template>
  68.  
  69. <script>
  70. export default {
  71.     name: 'edit',
  72.     components:{
  73.       'resetPasswordModal': () => import('@/views/components/modals/resetPassword'),
  74.     },
  75.     data () {
  76.         return {
  77.             is_reset_modal_active: false,
  78.             modal_data: null,
  79.             errors:'',
  80.             isLoading: false,
  81.             options: [
  82.                 { text: 'Admin', value: 1},
  83.                 { text: 'Supervisor', value: 2},
  84.                 { text: 'Sales Admin', value: 3},
  85.                 { text: 'Sales', value: 4},
  86.             ],
  87.             user: {
  88.                 role_id: 1,
  89.                 username: '',
  90.                 email: '',
  91.                 phone_number: '',
  92.             },
  93.         }
  94.     },
  95.     methods: {
  96.         cancel:function(){
  97.             this.$router.push({ name: 'dashboard' })
  98.         },
  99.         validate: function(){
  100.             if (this.user.password != this.user.password_confirmation){
  101.                 console.log("not same")
  102.             }
  103.         },
  104.         resetPassword: function(){
  105.           let id = this.$route.params.id
  106.           this.axios({
  107.             methods: 'PUT',
  108.             url: process.env.API_URL + 'users/reset/'+id,
  109.             headers:{
  110.               'Authorization': localStorage.getItem('token')
  111.                // 'Authorization': this.$store.getters.token
  112.             },
  113.             data: {
  114.                 user: data
  115.             }
  116.           })          
  117.           .then(response => {
  118.             console.log(response)
  119.             console.log("update success")
  120.             this.$router.push({ name: 'dashboard' })
  121.           })
  122.           .catch(error => {
  123.             this.isLoading = false;
  124.             this.errors = error.response.data.errors
  125.             console.log(error);
  126.             console.log("update not success")
  127.           })
  128.           this.close_reset_password_modal()
  129.         },
  130.         open_reset_password_modal: function(data){
  131.           this.modal_data = data
  132.           this.is_reset_modal_active = true
  133.         },
  134.         close_reset_password_modal: function(){
  135.           this.is_reset_modal_active = false
  136.         },
  137.         updateUser: function() {
  138.           this.isLoading = true;
  139.           let data = this.user
  140.           console.log(data)
  141.           let id = this.$route.params.id
  142.           this.axios({
  143.             method: 'PUT',
  144.             url: process.env.API_URL + 'users/'+id,
  145.             headers:{
  146.               'Authorization': localStorage.getItem('token')
  147.                // 'Authorization': this.$store.getters.token
  148.             },
  149.             data: {
  150.                 user: data
  151.             }
  152.           })
  153.           .then(response => {
  154.             console.log(response)
  155.             console.log("update success")
  156.             this.$router.push({ name: 'dashboard' })
  157.           })
  158.           .catch(error => {
  159.             this.isLoading = false;
  160.             this.errors = error.response.data.errors
  161.             console.log(error);
  162.             console.log("update not success")
  163.           })
  164.         },
  165.         getUser: function(){
  166.             let id = this.$route.params.id
  167.            
  168.             this.axios({
  169.                 method: 'GET',
  170.                 url: process.env.API_URL + 'users/'+id,
  171.                 headers:{
  172.                     'Authorization': localStorage.getItem('token')
  173.                     // 'Authorization': this.$store.getters.token
  174.                 }
  175.             })
  176.             .then(response => {
  177.                 let user = response.data
  178.                 console.log(response)
  179.                 console.log("get success")
  180.                 this.user.username = user.username
  181.                 this.user.email = user.email
  182.                 this.user.id = user.id
  183.                 this.user.phone_number = user.phone_number
  184.                 this.user.role_id = user.role_id
  185.             })
  186.             .catch(error => {
  187.                 console.log(error);
  188.                 console.log("add not success")
  189.             })
  190.         },
  191.         get_roles: function() {
  192.             this.$store.dispatch("get_roles", {
  193.                 token: localStorage.getItem('token')
  194.             })
  195.         },
  196.     },
  197.     computed: {
  198.         isLoggedIn: function() {
  199.             return this.$store.getters.isLoggedIn
  200.         },
  201.         roles: function(){
  202.             return this.$store.getters.roles
  203.         }
  204.     },
  205.     created: function(){
  206.         this.getUser()
  207.         this.get_roles()
  208.     }
  209. }
  210. </script>
  211.  
  212. <!-- Add "scoped" attribute to limit CSS to this component only -->
  213. <style scoped>
  214.     .hero{
  215.         background-color: whitesmoke;
  216.     }
  217. </style>
Add Comment
Please, Sign In to add comment