Skip to content

Commit a48daa8

Browse files
committed
feature/Add avatars to users
1 parent 73591e3 commit a48daa8

19 files changed

+285
-22
lines changed

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gem 'sass-rails', '>= 6'
1414
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
1515
gem 'webpacker', '~> 4.0'
1616
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
17-
# gem 'turbolinks', '~> 5'
17+
gem 'turbolinks', '~> 5'
1818
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
1919
gem 'jbuilder', '~> 2.7'
2020
# Serializing
@@ -23,6 +23,7 @@ gem 'active_model_serializers', '~> 0.10.0'
2323
gem 'byebug'
2424

2525
gem 'devise'
26+
2627
# Use Redis adapter to run Action Cable in production
2728
# gem 'redis', '~> 4.0'
2829
# Use Active Model has_secure_password

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ GEM
173173
thor (1.0.1)
174174
thread_safe (0.3.6)
175175
tilt (2.0.10)
176+
turbolinks (5.2.1)
177+
turbolinks-source (~> 5.2)
178+
turbolinks-source (5.2.0)
176179
tzinfo (1.2.7)
177180
thread_safe (~> 0.1)
178181
warden (1.2.8)
@@ -207,6 +210,7 @@ DEPENDENCIES
207210
sass-rails (>= 6)
208211
spring
209212
spring-watcher-listen (~> 2.0.0)
213+
turbolinks (~> 5)
210214
tzinfo-data
211215
web-console (>= 3.3.0)
212216
webpacker (~> 4.0)

app/controllers/application_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ class ApplicationController < ActionController::Base
66
protected
77

88
def configure_permitted_parameters
9-
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password)}
9+
devise_parameter_sanitizer.permit(:sign_up) { |u| u.permit(:name, :email, :password, :avatar)}
1010

11-
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :bio, :location, :password, :current_password)}
11+
devise_parameter_sanitizer.permit(:account_update) { |u| u.permit(:name, :email, :bio, :location, :password, :avatar, :current_password)}
1212
end
1313
end
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
class RegistrationsController < Devise::RegistrationsController
2+
# def create
3+
# byebug
4+
# super
5+
# end
6+
27
def update
3-
if current_user.update(account_update_params)
4-
render json: { message: "Profile updated" }
5-
else
6-
render json: { message: "Failed to update profile" }, status: 400
7-
end
8+
current_user.updated_at = Time.now
9+
current_user.avatar.attach(params[:user][:avatar])
10+
current_user.save!
11+
# if current_user.update(account_update_params)
12+
# render json: { message: "Profile updated" }
13+
# else
14+
# render json: { message: "Failed to update profile" }, status: 400
15+
# end
816
end
917
end

app/controllers/users_controller.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ def show
77
@following = @user.following
88
end
99

10+
def modify
11+
current_user.assign_attributes(user_params)
12+
current_user.avatar.attach(avatar_param) if avatar_param
13+
14+
if current_user.save
15+
render json: { message: "Profile updated" }
16+
else
17+
render json: { message: "Failed to update profile" }, status: 400
18+
end
19+
20+
# current_user.updated_at = Time.now
21+
# current_user.avatar.attach(params[:user][:avatar])
22+
# current_user.save!
23+
# byebug
24+
end
25+
1026
def follow
1127
@user = User.find_by(id: params[:id])
1228

@@ -26,4 +42,17 @@ def unfollow
2642
render json: { message: "You can't unfollow someone you aren't following you nutter!" }, status: 400
2743
end
2844
end
45+
46+
private
47+
48+
def user_params
49+
params
50+
.require(:user)
51+
.permit(:name, :bio, :location)
52+
.merge(updated_at: Time.now) #This is to fix an activestorage bug
53+
end
54+
55+
def avatar_param
56+
params[:user][:avatar]
57+
end
2958
end

app/javascript/edit-avatar.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<img ref="avatar" src="https://media-exp1.licdn.com/dms/image/C4E03AQG0MUNjxFvBTw/profile-displayphoto-shrink_200_200/0?e=1593648000&v=beta&t=WnnJ_Q6MpQFur5Uev2iqeLAnoX6Rdb1Bv6RoAijK3tA" style="border-radius: 50%;" width="120" height="120">
44
<div class="edit-avatar--edit-button" @click="selectNewAvatar">
55
<img src="/icons/edit.svg" width="16">
6-
<input ref="fileInput" type="file" @change="handleFileSelect" style="visibility: hidden" />
6+
<input name="avatar" ref="fileInput" type="file" @change="handleFileSelect" style="visibility: hidden" />
77
</div>
88
</div>
99
</template>
@@ -24,6 +24,8 @@ export default {
2424
2525
reader.onload = function(e) {
2626
self.$refs.avatar.setAttribute('src', e.target.result);
27+
28+
self.$emit('change', input.files[0])
2729
}
2830
2931
reader.readAsDataURL(input.files[0]);

app/javascript/edit-profile.vue

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
33
<div class="edit-profile--wrapper">
4-
<edit-avatar />
4+
<edit-avatar @change="addAvatarToParams"/>
55

66
<div class="edit-profile--fields">
77
<form-field label="Name" type="text" v-model="userParams.name" :margin-top="false" />
@@ -34,8 +34,10 @@ export default {
3434
userParams: {
3535
name: this.currentUser.name,
3636
bio: this.currentUser.bio,
37-
location: this.currentUser.location
38-
}
37+
location: this.currentUser.location,
38+
avatar: undefined
39+
},
40+
avatar: null
3941
}
4042
},
4143
@@ -46,14 +48,28 @@ export default {
4648
4749
methods: {
4850
saveForm() {
49-
this.updateUser(this.userParams)
51+
let formData = new FormData()
52+
53+
Object.keys(this.userParams).forEach(attribute => {
54+
const value = this.userParams[attribute];
55+
56+
if (value) { formData.append(`user[${attribute}]`, value); };
57+
})
58+
59+
60+
61+
this.updateUser(formData)
5062
.then(res => {
5163
EventBus.$emit('presentToast', res.data.message)
5264
})
5365
.catch(error => {
5466
console.error(error)
5567
EventBus.$emit('presentToast', error.response.data.message)
5668
})
69+
},
70+
71+
addAvatarToParams(avatarFile) {
72+
this.userParams.avatar = avatarFile;
5773
}
5874
}
5975
}

app/javascript/mixins/usersMixin.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ export default {
88
},
99

1010
methods: {
11-
updateUser(userParams) {
12-
return axios.put(`/users?ajax=true`, { user: userParams })
11+
updateUser(userForm) {
12+
const config = {
13+
headers: {
14+
'content-type': 'multipart/form-data'
15+
}
16+
}
17+
18+
return axios.put(`/users/modify?ajax=true`, userForm, config)
1319
},
1420

1521
followUser(userId) {

app/javascript/packs/snippet_app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
//
5454
// Then uncomment the code block below:
5555
//
56-
// import TurbolinksAdapter from 'vue-turbolinks'
56+
import TurbolinksAdapter from 'vue-turbolinks'
5757
import Vue from 'vue/dist/vue.esm'
5858

5959
import { store } from '../store.js';
@@ -84,7 +84,7 @@ import Toast from '../toast.vue';
8484
import UserPreview from '../user-preview.vue';
8585
import UserProfile from '../user-profile.vue'
8686

87-
// Vue.use(TurbolinksAdapter)
87+
Vue.use(TurbolinksAdapter)
8888

8989

9090
document.addEventListener('DOMContentLoaded', () => {

app/javascript/user-preview.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div class="user-preview--wrapper">
33
<a :href="`/users/${user.id}`">
4-
<img class="user-preview--img" src="https://media-exp1.licdn.com/dms/image/C4E03AQG0MUNjxFvBTw/profile-displayphoto-shrink_200_200/0?e=1593648000&v=beta&t=WnnJ_Q6MpQFur5Uev2iqeLAnoX6Rdb1Bv6RoAijK3tA" height="48" width="48" />
4+
<img class="user-preview--img" :src="user.avatar_url" height="48" width="48" />
55
</a>
66
<div class="user-preview--summary">
77
<a :href="`/users/${user.id}`">

0 commit comments

Comments
 (0)