OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Implemennt workdays component
Oliver Günther385840ec89a1
diff --git a/app/models/user_preference.rb b/app/models/user_preference.rbindex d8addfce1e4..1eeab687538 100644--- a/app/models/user_preference.rb+++ b/app/models/user_preference.rb@@ -126,6 +126,10 @@ class UserPreference < ApplicationRecordsuper.presence || { mentioned: false }.with_indifferent_accessend+ def workdays+ super.presence || [1, 2, 3, 4, 5]+ end+def canonical_time_zonereturn if time_zone.nil?diff --git a/config/locales/js-en.yml b/config/locales/js-en.ymlindex 8056c7573bd..d8d31917325 100644--- a/config/locales/js-en.yml+++ b/config/locales/js-en.yml@@ -645,6 +645,8 @@ en:explanation: 'You will receive these reminders only for unread notifications and only at hours you specify.'time_label: 'Time %{counter}:'title: 'Send me daily email reminders for unread notifications'+ workdays:+ title: 'Receive email reminders on these days'immediate:title: 'Send me an email reminder'mentioned: 'Immediately when someone @mentions me'diff --git a/db/migrate/20210928133538_add_reminder_workdays.rb b/db/migrate/20210928133538_add_reminder_workdays.rbindex e5d2857fd5a..395d8321576 100644--- a/db/migrate/20210928133538_add_reminder_workdays.rb+++ b/db/migrate/20210928133538_add_reminder_workdays.rb@@ -3,35 +3,15 @@ require_relative './migration_utils/utils'class AddReminderWorkdays < ActiveRecord::Migration[6.1]include ::Migration::Utils- def change- reversible do |dir|- dir.up do- execute <<~SQL.squish- UPDATE user_preferences- SET settings = settings || '{ "workdays": [1,2,3,4,5] }'- SQL- end-- dir.down do- execute <<~SQL.squish- UPDATE user_preferences- SET settings = settings - 'workdays'- SQL- end- end-- reversible do |dir|- dir.up do- add_index :user_preferences,- "(settings->'workdays')",- using: :gin,- name: :index_user_prefs_settings_workdays-- end+ def up+ add_index :user_preferences,+ "(settings->'workdays')",+ using: :gin,+ name: :index_user_prefs_settings_workdays+ end- dir.down do- remove_index_if_exists :user_preferences, :index_user_prefs_settings_workdays- end- end+ def down+ remove_index_if_exists :user_preferences, :index_user_prefs_settings_workdaysendend+diff --git a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.htmlindex feca58b534e..f8a04d3d726 100644--- a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html+++ b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.html@@ -19,6 +19,10 @@<op-reminder-settings-daily-time></op-reminder-settings-daily-time></section>+ <section class="form--section">+ <h5 [textContent]="text.workdays.title"></h5>+ <op-workdays-settings></op-workdays-settings>+ </section><section class="form--section"><h5 [textContent]="text.alerts.title"></h5><p [textContent]="text.alerts.explanation"></p>diff --git a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.tsindex 9ca42eb8222..8bddc5c6daa 100644--- a/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts+++ b/frontend/src/app/features/user-preferences/reminder-settings/page/reminder-settings-page.component.ts@@ -54,6 +54,15 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementenabled: this.fb.control(false),times: this.fb.array([]),}),+ workdays: this.fb.array([+ this.fb.control(false),+ this.fb.control(true),+ this.fb.control(true),+ this.fb.control(true),+ this.fb.control(true),+ this.fb.control(true),+ this.fb.control(false),+ ]),emailAlerts: this.fb.group({newsAdded: this.fb.control(false),newsCommented: this.fb.control(false),@@ -73,6 +82,9 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementtitle: this.I18n.t('js.reminders.settings.daily.title'),explanation: this.I18n.t('js.reminders.settings.daily.explanation'),},+ workdays: {+ title: this.I18n.t('js.reminders.settings.workdays.title'),+ },immediate: {title: this.I18n.t('js.reminders.settings.immediate.title'),explanation: this.I18n.t('js.reminders.settings.immediate.explanation'),@@ -126,6 +138,12 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementdailyReminderTimes.enable({ emitEvent: true });+ const workdays = this.form.get('workdays') as FormArray;+ for (let i = 0; i <= 6; i++) {+ const control = workdays.at(i);+ control.setValue(settings.workdays.includes(i + 1));+ }+emailAlerts.forEach((alert) => {this.form.get(`emailAlerts.${alert}`)?.setValue(globalSetting[alert]);});@@ -140,9 +158,11 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implementconst globalNotifications = prefs.notifications.filter((notification) => !notification._links.project.href);const projectNotifications = prefs.notifications.filter((notification) => !!notification._links.project.href);const reminderSettings = (this.form.value as IReminderSettingsFormValue);+ const workdays = ReminderSettingsPageComponent.buildWorkdays(this.form.value.workdays as boolean[]);this.storeService.update(this.userId, {...prefs,+ workdays,dailyReminders: reminderSettings.dailyReminders,immediateReminders: reminderSettings.immediateReminders,notifications: [@@ -156,4 +176,18 @@ export class ReminderSettingsPageComponent extends UntilDestroyedMixin implement],});}++ private static buildWorkdays(formValues:boolean[]):number[] {+ return formValues+ .reduce(+ (result, val, index) => {+ if (val) {+ return result.concat([index + 1]);+ }++ return result;+ },+ [] as number[],+ );+ }}diff --git a/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.html b/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.htmlnew file mode 100644index 00000000000..ab0141b59ac--- /dev/null+++ b/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.html@@ -0,0 +1,18 @@+<ng-container [formGroup]="formGroup.control">+ <div *ngFor="let workday of localeWorkdays; let i = index"+ formArrayName="workdays"+ >+ <op-checkbox-field+ [control]="controlForLocalWorkday(workday)"+ >+ <input+ slot="input"+ type="checkbox"+ [formControlName]="indexOfLocalWorkday(workday)"+ />+ <p slot="description"+ [textContent]="workday">+ </p>+ </op-checkbox-field>+ </div>+</ng-container>diff --git a/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.sass b/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.sassnew file mode 100644index 00000000000..e69de29bb2ddiff --git a/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.ts b/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.tsnew file mode 100644index 00000000000..efa808f71fa--- /dev/null+++ b/frontend/src/app/features/user-preferences/reminder-settings/workdays/workdays-settings.component.ts@@ -0,0 +1,63 @@+import {+ ChangeDetectionStrategy,+ Component,+ OnInit,+} from '@angular/core';+import {+ FormArray,+ FormControl,+ FormGroupDirective,+} from '@angular/forms';+import * as moment from 'moment';++@Component({+ selector: 'op-workdays-settings',+ templateUrl: './workdays-settings.component.html',+ styleUrls: ['./workdays-settings.component.sass'],+ changeDetection: ChangeDetectionStrategy.OnPush+})+export class WorkdaysSettingsComponent implements OnInit {+ control:FormArray;++ /**+ * The locale might render workdays in a different order, which is what moment return with localeSorted+ * and used for rendering the component.+ */+ localeWorkdays:string[] = moment.weekdays(true);++ /**+ * Almost* ISO workdays with localized strings.+ * ISO workdays are 1=Monday, ... 7=Sunday which is what we persist+ *+ * Working with the FormArray however, we use 0=Monday, 6=Sunday and add one before saving+ * @private+ */+ private isoWorkdays:string[] = this.buildISOWeekdays();++ constructor(+ readonly formGroup:FormGroupDirective,+ ) {+ }++ ngOnInit():void {+ this.control = this.formGroup.control.get('workdays') as FormArray;+ }++ indexOfLocalWorkday(day:string):number {+ return this.isoWorkdays.indexOf(day);+ }++ controlForLocalWorkday(day:string):FormControl {+ const index = this.indexOfLocalWorkday(day);+ return this.control.at(index) as FormControl;+ }++ /** Workdays from moment.js are in non-ISO order, that means Sunday=0, Saturday=6 */+ private buildISOWeekdays():string[] {+ const days = moment.weekdays(false);++ days.push(days.shift() as string);++ return days;+ }+}diff --git a/frontend/src/app/features/user-preferences/state/user-preferences.model.ts b/frontend/src/app/features/user-preferences/state/user-preferences.model.tsindex 8dd44a17498..1cde1277341 100644--- a/frontend/src/app/features/user-preferences/state/user-preferences.model.ts+++ b/frontend/src/app/features/user-preferences/state/user-preferences.model.ts@@ -14,6 +14,7 @@ export interface UserPreferencesModel {hideMail:boolean;timeZone:string|null;warnOnLeavingUnsaved:boolean;+ workdays:number[];notifications:NotificationSetting[];dailyReminders:DailyRemindersSettings;immediateReminders:ImmediateRemindersSettings;diff --git a/frontend/src/app/features/user-preferences/state/user-preferences.store.ts b/frontend/src/app/features/user-preferences/state/user-preferences.store.tsindex 9de94ab486b..96f2a67f8f8 100644--- a/frontend/src/app/features/user-preferences/state/user-preferences.store.ts+++ b/frontend/src/app/features/user-preferences/state/user-preferences.store.ts@@ -42,6 +42,7 @@ function createInitialState():UserPreferencesModel {enabled: true,times: ['08:00'],},+ workdays: [1,2,3,4,5],immediateReminders: {mentioned: false,},diff --git a/frontend/src/app/features/user-preferences/user-preferences.module.ts b/frontend/src/app/features/user-preferences/user-preferences.module.tsindex d7589c6569e..e9002d6a6fb 100644--- a/frontend/src/app/features/user-preferences/user-preferences.module.ts+++ b/frontend/src/app/features/user-preferences/user-preferences.module.ts@@ -17,6 +17,7 @@ import { ReminderSettingsPageComponent } from './reminder-settings/page/reminderimport { ReminderSettingsDailyTimeComponent } from 'core-app/features/user-preferences/reminder-settings/reminder-time/reminder-settings-daily-time.component';import { ImmediateReminderSettingsComponent } from 'core-app/features/user-preferences/reminder-settings/immediate-reminders/immediate-reminder-settings.component';import { EmailAlertsSettingsComponent } from 'core-app/features/user-preferences/reminder-settings/email-alerts/email-alerts-settings.component';+import { WorkdaysSettingsComponent } from './reminder-settings/workdays/workdays-settings.component';@NgModule({providers: [@@ -31,6 +32,7 @@ import { EmailAlertsSettingsComponent } from 'core-app/features/user-preferencesReminderSettingsDailyTimeComponent,ImmediateReminderSettingsComponent,EmailAlertsSettingsComponent,+ WorkdaysSettingsComponent,],imports: [CommonModule,diff --git a/packaging/scripts/web b/packaging/scripts/webindex 5c098af24ed..1a1db9f7ae5 100755--- a/packaging/scripts/web+++ b/packaging/scripts/web@@ -11,3 +11,4 @@ if [ "$USE_PUMA" = "true" ]; thenelsebundle exec unicorn --config-file config/unicorn.rb --host $HOST --port $PORT --env $RAILS_ENVfi+diff --git a/spec/features/notifications/settings/workdays_settings_spec.rb b/spec/features/notifications/settings/workdays_settings_spec.rbnew file mode 100644index 00000000000..d2683739267--- /dev/null+++ b/spec/features/notifications/settings/workdays_settings_spec.rb@@ -0,0 +1,100 @@+require 'spec_helper'++describe "Workday notification settings", type: :feature, js: true do+ shared_examples 'workday settings' do+ before do+ current_user.language = locale+ current_user.save! && pref.save!+ end++ context 'with english locale' do+ let(:locale) { :en }++ it 'allows to configure the workdays' do+ # Configure the reminders+ settings_page.visit!++ # Expect Mo-Fr to be checked+ settings_page.expect_workdays %w[Monday Tuesday Wednesday Thursday Friday]+ settings_page.expect_non_workdays %w[Saturday Sunday]++ settings_page.set_workdays Monday: true,+ Tuesday: true,+ Wednesday: false,+ Thursday: false,+ Friday: true,+ Saturday: true,+ Sunday: true++ settings_page.save++ settings_page.expect_and_dismiss_notification(message: I18n.t('js.notice_successful_update'))++ settings_page.reload!++ settings_page.expect_workdays %w[Monday Tuesday Friday Saturday Sunday]+ settings_page.expect_non_workdays %w[Wednesday Thursday]++ expect(pref.reload.workdays).to eq [1, 2, 5, 6, 7]+ end+ end++ context 'with german locale' do+ let(:locale) { :de }++ it 'allows to configure the workdays' do+ I18n.locale = :de++ # Configure the reminders+ settings_page.visit!++ # Expect Mo-Fr to be checked+ settings_page.expect_workdays %w[Montag Dienstag Mittwoch Donnerstag Freitag]+ settings_page.expect_non_workdays %w[Samstag Sonntag]++ settings_page.set_workdays Montag: true,+ Dienstag: true,+ Mittwoch: false,+ Donnerstag: false,+ Freitag: true,+ Samstag: true,+ Sonntag: false++ settings_page.save++ settings_page.expect_and_dismiss_notification message: I18n.t('js.notice_successful_update')++ settings_page.reload!++ settings_page.expect_workdays %w[Montag Dienstag Freitag Samstag]+ settings_page.expect_non_workdays %w[Mittwoch Donnerstag Sonntag]++ expect(pref.reload.workdays).to eq [1, 2, 5, 6]+ end+ end+ end++ context 'with the my page' do+ let(:settings_page) { Pages::My::Reminders.new(current_user) }+ let(:pref) { current_user.pref }++ current_user do+ FactoryBot.create :user+ end++ it_behaves_like 'workday settings'+ end++ context 'with the user administration page' do+ let(:settings_page) { Pages::Reminders::Settings.new(other_user) }++ let(:other_user) { FactoryBot.create :user }+ let(:pref) { other_user.pref }++ current_user do+ FactoryBot.create :admin+ end++ it_behaves_like 'workday settings'+ end+enddiff --git a/spec/support/pages/reminders/settings.rb b/spec/support/pages/reminders/settings.rbindex 81711430db2..99004719e15 100644--- a/spec/support/pages/reminders/settings.rb+++ b/spec/support/pages/reminders/settings.rb@@ -86,8 +86,30 @@ module Pagesendend+ def expect_workdays(days)+ days.each do |name|+ expect(page).to have_checked_field(name)+ end+ end++ def expect_non_workdays(days)+ days.each do |name|+ expect(page).to have_unchecked_field(name)+ end+ end++ def set_workdays(days)+ days.each do |name, enabled|+ if enabled+ page.check name+ else+ page.uncheck name+ end+ end+ end+def save- click_button 'Save'+ click_button I18n.t(:button_save)endendend