-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add NotificationPreference
for weekly reminder
#1867
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1bbb798
Add `NotificationPreference` for weekly reminder
1409fcf
Merge branch 'develop' into notification_preferences
8138691
Add notification settings in Team and Profile pages
5f9febc
Update mobile notification screen
a18ff97
Create `NotificationPreference` after owner sign up or team member ac…
cae51bb
Merge branch 'develop' into notification_preferences
5aa121c
Fix review pointers
3d81906
Fix review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
app/controllers/internal_api/v1/team_members/notification_preferences_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
class InternalApi::V1::TeamMembers::NotificationPreferencesController < InternalApi::V1::ApplicationController | ||
def show | ||
authorize notification_preference, policy_class: TeamMembers::NotificationPreferencePolicy | ||
render json: { notification_enabled: notification_preference.notification_enabled }, status: :ok | ||
end | ||
|
||
def update | ||
authorize notification_preference, policy_class: TeamMembers::NotificationPreferencePolicy | ||
|
||
notification_preference.update!(notification_preference_params) | ||
render json: { | ||
notification_enabled: notification_preference.notification_enabled, | ||
notice: "Preference updated successfully" | ||
}, status: :ok | ||
end | ||
|
||
private | ||
|
||
def notification_preference | ||
@notification_preference ||= NotificationPreference.find_by( | ||
user_id: params[:team_id], | ||
company_id: current_company.id) | ||
end | ||
|
||
def notification_preference_params | ||
params.require(:notification_preference).permit(:notification_enabled) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import axios from "./api"; | ||
|
||
const get = async userId => | ||
axios.get(`team/${userId}/notification_preferences`); | ||
|
||
const updatePreference = async (userId, payload) => | ||
axios.patch(`team/${userId}/notification_preferences`, payload); | ||
|
||
const preferencesApi = { get, updatePreference }; | ||
|
||
export default preferencesApi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
app/javascript/src/components/Profile/Personal/NotificationPreferences/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* eslint-disable no-unused-vars */ | ||
import React, { Fragment, useEffect, useState } from "react"; | ||
|
||
import { useParams } from "react-router-dom"; | ||
|
||
import preferencesApi from "apis/preferences"; | ||
import CustomToggle from "common/CustomToggle"; | ||
import Loader from "common/Loader/index"; | ||
import { MobileEditHeader } from "common/Mobile/MobileEditHeader"; | ||
import DetailsHeader from "components/Profile/Common/DetailsHeader"; | ||
import { useProfileContext } from "context/Profile/ProfileContext"; | ||
import { useUserContext } from "context/UserContext"; | ||
|
||
const NotificationPreferences = () => { | ||
const { user, isDesktop } = useUserContext(); | ||
const { memberId } = useParams(); | ||
const { isCalledFromSettings } = useProfileContext(); | ||
const currentUserId = isCalledFromSettings ? user.id : memberId; | ||
|
||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
const [isSelected, setIsSelected] = useState<boolean>(false); | ||
|
||
const getPreferences = async () => { | ||
const res = await preferencesApi.get(currentUserId); | ||
setIsSelected(res.data.notification_enabled); | ||
setIsLoading(false); | ||
}; | ||
|
||
const updatePreferences = async () => { | ||
setIsLoading(true); | ||
const res = await preferencesApi.updatePreference(currentUserId, { | ||
notification_enabled: !isSelected, | ||
}); | ||
setIsLoading(false); | ||
}; | ||
|
||
useEffect(() => { | ||
setIsLoading(true); | ||
getPreferences(); | ||
}, []); | ||
|
||
return ( | ||
<Fragment> | ||
{isDesktop ? ( | ||
<DetailsHeader subTitle="" title="Notification Settings" /> | ||
) : ( | ||
<MobileEditHeader | ||
backHref={isCalledFromSettings ? "/settings/" : `/team/${memberId}`} | ||
href="" | ||
showEdit={false} | ||
title="Notification Settings" | ||
/> | ||
)} | ||
{isLoading ? ( | ||
<Loader className="min-h-70v" /> | ||
) : ( | ||
<div className="mt-10 flex h-full items-center justify-between bg-miru-gray-100 p-10 lg:mt-4"> | ||
<span className="w-1/2">Weekly Email Reminder</span> | ||
<CustomToggle | ||
id={currentUserId} | ||
isChecked={isSelected} | ||
setIsChecked={setIsSelected} | ||
toggleCss="mt-5" | ||
onToggle={updatePreferences} | ||
/> | ||
</div> | ||
)} | ||
</Fragment> | ||
); | ||
}; | ||
|
||
export default NotificationPreferences; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: notification_preferences | ||
# | ||
# id :bigint not null, primary key | ||
# notification_enabled :boolean default(FALSE), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# company_id :bigint not null | ||
# user_id :bigint not null | ||
# | ||
# Indexes | ||
# | ||
# index_notification_preferences_on_company_id (company_id) | ||
# index_notification_preferences_on_user_id (user_id) | ||
# index_notification_preferences_on_user_id_and_company_id (user_id,company_id) UNIQUE | ||
# | ||
# Foreign Keys | ||
# | ||
# fk_rails_... (company_id => companies.id) | ||
# fk_rails_... (user_id => users.id) | ||
# | ||
class NotificationPreference < ApplicationRecord | ||
belongs_to :user | ||
belongs_to :company | ||
|
||
validates :notification_enabled, inclusion: { in: [true, false] } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
app/policies/team_members/notification_preference_policy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class TeamMembers::NotificationPreferencePolicy < ApplicationPolicy | ||
def show? | ||
return false unless record.present? | ||
|
||
authorize_current_user | ||
end | ||
|
||
def update? | ||
return false unless record.present? | ||
|
||
authorize_current_user | ||
end | ||
|
||
private | ||
|
||
def authorize_current_user | ||
unless user.current_workspace_id == record.company_id | ||
@error_message_key = :different_workspace | ||
return false | ||
end | ||
|
||
has_owner_or_admin_role? || record_belongs_to_user? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
db/migrate/20240617135248_create_notification_preferences.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateNotificationPreferences < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :notification_preferences do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.references :company, null: false, foreign_key: true | ||
t.boolean :notification_enabled, default: false, null: false | ||
t.index [:user_id, :company_id], unique: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: I think it would be better if we have this check in SendWeeklyReminderToUserMailer, so that even when we trigger mailer manually, it won't send any email if the user's notification preference is not satisfied.
In future, we can even have this as a before action check in the application mailer, If we plan to use this notification preference for all the mail notifications.