Skip to content

Commit 8c8edd2

Browse files
committed
feature/Allow user to update name
1 parent b252d37 commit 8c8edd2

File tree

7 files changed

+44
-14
lines changed

7 files changed

+44
-14
lines changed

app/controllers/application_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class ApplicationController < ActionController::Base
2+
respond_to :html, :json
23
serialization_scope :view_context
34
before_action :configure_permitted_parameters, if: :devise_controller?
45

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class RegistrationsController < Devise::RegistrationsController
2+
def update
3+
if current_user.update(sign_up_params)
4+
render json: { message: "Profile updated" }
5+
else
6+
render json: { message: "Failed to update profile" }, status: 400
7+
end
8+
end
9+
end

app/javascript/edit-profile.vue

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<edit-avatar />
55

66
<div class="edit-profile--fields">
7-
<form-field label="Name" type="text" :value="currentUser.name" :margin-top="false" @change="logChange"/>
8-
<form-field label="Description" type="textarea" value="Dummy description" />
9-
<form-field label="Location" type="text" value="location" />
7+
<form-field label="Name" type="text" v-model="userParams.name" :margin-top="false" />
8+
<form-field label="Description" type="textarea" v-model="userParams.bio" />
9+
<form-field label="Location" type="text" v-model="userParams.location" />
1010
</div>
1111

1212
</div>
1313

1414
<div class="folders--options-wrapper">
15-
<button class="button--cta-blue">SAVE</button>
15+
<button @click="saveForm" class="button--cta-blue">SAVE</button>
1616
</div>
1717
</div>
1818
</template>
@@ -21,19 +21,39 @@
2121
import EditAvatar from './edit-avatar';
2222
import FormField from './form-field';
2323
24+
import usersMixin from './mixins/usersMixin';
25+
2426
export default {
2527
components: { EditAvatar, FormField },
2628
27-
// TODO: Extract this into a class or module that can be reused accross the application
29+
mixins: [usersMixin],
30+
2831
data() {
2932
return {
30-
currentUser: this.$store.state.currentUser
33+
userParams: {
34+
name: this.currentUser.name,
35+
bio: 'Dummy bio',
36+
location: 'Cambridge UK'
37+
}
3138
}
3239
},
3340
41+
// TODO: Extract this into a class or module that can be reused across the application
42+
beforeCreate() {
43+
this.currentUser = this.$store.state.currentUser;
44+
},
45+
3446
methods: {
35-
logChange(value) {
36-
console.log(value)
47+
saveForm() {
48+
console.log('params', this.userParams)
49+
50+
this.updateUser(this.userParams)
51+
.then(res => {
52+
console.log('aya', res)
53+
})
54+
.catch(error => {
55+
console.error(error)
56+
})
3757
}
3858
}
3959
}

app/javascript/form-field.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export default {
3737
3838
methods: {
3939
emitValueChange() {
40-
this.$emit('change', this.valueDup)
40+
this.$emit('input', this.valueDup)
4141
}
4242
}
4343
}

app/javascript/mixins/usersMixin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export default {
88
},
99

1010
methods: {
11+
updateUser(userParams) {
12+
return axios.put(`/users?ajax=true`, { user: userParams })
13+
},
14+
1115
followUser(userId) {
1216
return axios.post(`/users/${userId}/follow?ajax=true`)
1317
},

app/javascript/new-snippet.vue

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,6 @@ export default {
9999
}, 300),
100100
101101
createSnippet() {
102-
console.log('snippet params', this.snippetParams)
103-
104102
axios.post('/snippets?ajax=true', { snippet: this.snippetParams })
105103
.then(res => {
106104
const snippet = res.data.snippet

config/routes.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Rails.application.routes.draw do
2-
devise_for :users
2+
devise_for :users, :controllers => { registrations: :registrations }
33
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
44
root to: 'home#index'
55

@@ -8,8 +8,6 @@
88
resources :users, only: :show do
99
post :follow, on: :member
1010
post :unfollow, on: :member
11-
get :followers, on: :member
12-
get :following, on: :member
1311
end
1412

1513
resources :likes, only: :create

0 commit comments

Comments
 (0)