diff --git a/app/controllers/client_api/users/users_controller.rb b/app/controllers/client_api/users/users_controller.rb index 255d7a0bd..9f2ca152c 100644 --- a/app/controllers/client_api/users/users_controller.rb +++ b/app/controllers/client_api/users/users_controller.rb @@ -13,85 +13,43 @@ module ClientApi end def change_password - user = current_user - user.password = params['passwrd'] - user.save - "" + user = User.find(current_user.id) + is_saved = user.update(user_params) + + if is_saved + bypass_sign_in(user) + res = "success" + else + res = "could not change password" + end + + respond_to do |format| + if is_saved + format.json { render json: { msg: res} } + else + format.json { render json: { msg: res}, status: 422 } + end + end end def change_assignements_notification - user = current_user - user.assignments_notification = params['status'] - - status = if user.save - user.assignments_notification - else - user.reload.assignments_notification - end - - respond_to do |format| - format.json { render json: { status: status }} - end + change_notification(:assignments_notification, params) end def change_assignements_notification_email - user = current_user - user.assignments_notification_email = params['status'] - - status = if user.save - user.assignments_notification_email - else - user.reload.assignments_notification_email - end - - respond_to do |format| - format.json { render json: { status: status }} - end + change_notification(:assignments_notification_email, params) end def change_recent_notification - user = current_user - user.recent_notification = params['status'] - - status = if user.save - user.recent_notification - else - user.reload.recent_notification - end - - respond_to do |format| - format.json { render json: { status: status }} - end + change_notification(:recent_notification, params) end def change_recent_notification_email - user = current_user - user.recent_notification_email = params['status'] - - status = if user.save - user.recent_notification_email - else - user.reload.recent_notification_email - end - - respond_to do |format| - format.json { render json: { status: status }} - end + change_notification(:recent_notification_email, params) end def change_system_notification_email - user = current_user - user.system_message_notification_email = params['status'] - - status = if user.save - user.system_message_notification_email - else - user.reload.system_message_notification_email - end - - respond_to do |format| - format.json { render json: { status: status }} - end + change_notification(:system_message_notification_email, params) end def change_timezone @@ -103,7 +61,7 @@ module ClientApi user.time_zone else user.reload.time_zone - errors[:timezone_errors] << 'You nedd to select valid TimeZone.' + errors[:timezone_errors] << 'You need to select valid TimeZone.' end respond_to do |format| @@ -159,6 +117,28 @@ module ClientApi format.json { render json: { initials: saved_initials } } end end + + private + + def user_params + params.require(:user).permit(:password) + end + + def change_notification(dinamic_param, params) + user = current_user + user[dinamic_param] = params['status'] + + status = + if user.save + user[dinamic_param] + else + user.reload[dinamic_param] + end + + respond_to do |format| + format.json { render json: { status: status } } + end + end end end end diff --git a/app/javascript/packs/shared/actions/UsersActions.js b/app/javascript/packs/shared/actions/UsersActions.js index 54b8a0c74..86d123c98 100644 --- a/app/javascript/packs/shared/actions/UsersActions.js +++ b/app/javascript/packs/shared/actions/UsersActions.js @@ -119,12 +119,14 @@ export function savePassword(password) { }; } -export function changePassword(passwrd) { +export function changePassword(password) { return dispatch => { axios .post(CHANGE_USER_PASSWORD_PATH, { - withCredentials: true, - passwrd + user: { + withCredentials: true, + password + } }) .then(({ data }) => { dispatch(savePassword(data)); diff --git a/config/routes.rb b/config/routes.rb index 8b8b8af5b..3510c9912 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -34,14 +34,15 @@ Rails.application.routes.draw do post '/change_password', to: 'users#change_password' post '/change_timezone', to: 'users#change_timezone' post '/change_assignements_notification', - to: 'users#change_assignements_notification' + to: 'users#change_assignements_notification' post '/change_assignements_notification_email', - to: 'users#change_assignements_notification_email' - post '/change_recent_notification', to: 'users#change_recent_notification' + to: 'users#change_assignements_notification_email' + post '/change_recent_notification', + to: 'users#change_recent_notification' post '/change_recent_notification_email', - to: 'users#change_recent_notification_email' + to: 'users#change_recent_notification_email' post '/change_system_notification_email', - to: 'users#change_system_notification_email' + to: 'users#change_system_notification_email' end end