From aed5d1fc1b2a08a6ce4c69d4a6598e6f8d120b87 Mon Sep 17 00:00:00 2001 From: Du Peng Date: Thu, 11 May 2023 11:22:23 +0200 Subject: [PATCH] (wip) add event type --- app/controllers/api/events_controller.rb | 2 +- .../components/events/event-form.tsx | 26 ++++++++++++++----- .../src/javascript/controllers/events.js.erb | 2 +- app/frontend/src/javascript/models/event.ts | 3 ++- app/frontend/templates/events/show.html | 4 +-- app/models/event.rb | 2 ++ app/views/api/events/_event.json.jbuilder | 2 +- config/locales/app.admin.en.yml | 7 +++-- config/locales/app.admin.fr.yml | 7 +++-- ...9121907_add_booking_nominative_to_event.rb | 8 ------ ...rt_item_event_reservation_booking_users.rb | 3 ++- .../20230511081018_add_event_type_to_event.rb | 10 +++++++ db/structure.sql | 6 +++-- test/fixtures/history_values.yml | 1 - .../components/events/event-form.test.tsx | 2 +- test/integration/events/as_admin_test.rb | 1 + 16 files changed, 56 insertions(+), 30 deletions(-) delete mode 100644 db/migrate/20230509121907_add_booking_nominative_to_event.rb create mode 100644 db/migrate/20230511081018_add_event_type_to_event.rb diff --git a/app/controllers/api/events_controller.rb b/app/controllers/api/events_controller.rb index 1b8abdc77..4d05f6b56 100644 --- a/app/controllers/api/events_controller.rb +++ b/app/controllers/api/events_controller.rb @@ -96,7 +96,7 @@ class API::EventsController < API::APIController # handle general properties event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time, :amount, :nb_total_places, :availability_id, :all_day, :recurrence, - :recurrence_end_at, :category_id, :event_theme_ids, :age_range_id, :booking_nominative, + :recurrence_end_at, :category_id, :event_theme_ids, :age_range_id, :event_type, event_theme_ids: [], event_image_attributes: %i[id attachment], event_files_attributes: %i[id attachment _destroy], diff --git a/app/frontend/src/javascript/components/events/event-form.tsx b/app/frontend/src/javascript/components/events/event-form.tsx index 033dff894..0a2ce1d8c 100644 --- a/app/frontend/src/javascript/components/events/event-form.tsx +++ b/app/frontend/src/javascript/components/events/event-form.tsx @@ -1,7 +1,7 @@ import { useEffect, useState } from 'react'; import * as React from 'react'; import { SubmitHandler, useFieldArray, useForm, useWatch } from 'react-hook-form'; -import { Event, EventDecoration, EventPriceCategoryAttributes, RecurrenceOption } from '../../models/event'; +import { Event, EventDecoration, EventPriceCategoryAttributes, RecurrenceOption, EventType } from '../../models/event'; import EventAPI from '../../api/event'; import { useTranslation } from 'react-i18next'; import { FormInput } from '../form/form-input'; @@ -40,7 +40,7 @@ interface EventFormProps { * Form to edit or create events */ export const EventForm: React.FC = ({ action, event, onError, onSuccess }) => { - const { handleSubmit, register, control, setValue, formState } = useForm({ defaultValues: { ...event } }); + const { handleSubmit, register, control, setValue, formState } = useForm({ defaultValues: Object.assign({ event_type: 'standard' }, event) }); const output = useWatch({ control }); const { fields, append, remove } = useFieldArray({ control, name: 'event_price_categories_attributes' }); @@ -168,6 +168,17 @@ export const EventForm: React.FC = ({ action, event, onError, on ]; }; + /** + * This method provides event type options + */ + const buildEventTypeOptions = (): Array> => { + return [ + { label: t('app.admin.event_form.event_types.standard'), value: 'standard' }, + { label: t('app.admin.event_form.event_types.nominative'), value: 'nominative' }, + { label: t('app.admin.event_form.event_types.family'), value: 'family' } + ]; + }; + return (
@@ -203,6 +214,12 @@ export const EventForm: React.FC = ({ action, event, onError, on label={t('app.admin.event_form.description')} limit={null} heading bulletList blockquote link video image /> + = ({ action, event, onError, on label={t('app.admin.event_form.seats_available')} type="number" tooltip={t('app.admin.event_form.seats_help')} /> - {{ 'app.public.events_show.ticket' | translate:{NUMBER:reserve.nbReservePlaces} }}
-
+
@@ -132,7 +132,7 @@ {{ 'app.public.events_show.ticket' | translate:{NUMBER:reserve.tickets[price.id]} }}
-
+
diff --git a/app/models/event.rb b/app/models/event.rb index 60bb3f70b..96ab48199 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -33,6 +33,8 @@ class Event < ApplicationRecord has_many :cart_item_event_reservations, class_name: 'CartItem::EventReservation', dependent: :destroy + validates :event_type, inclusion: { in: %w[standard nominative family] }, presence: true + attr_accessor :recurrence, :recurrence_end_at before_save :update_nb_free_places diff --git a/app/views/api/events/_event.json.jbuilder b/app/views/api/events/_event.json.jbuilder index 11be0748e..885a29d28 100644 --- a/app/views/api/events/_event.json.jbuilder +++ b/app/views/api/events/_event.json.jbuilder @@ -1,6 +1,6 @@ # frozen_string_literal: true -json.extract! event, :id, :title, :description, :booking_nominative +json.extract! event, :id, :title, :description, :event_type if event.event_image json.event_image_attributes do json.id event.event_image.id diff --git a/config/locales/app.admin.en.yml b/config/locales/app.admin.en.yml index a8ccdbafc..8fcbfb195 100644 --- a/config/locales/app.admin.en.yml +++ b/config/locales/app.admin.en.yml @@ -139,8 +139,6 @@ en: event_themes: "Event themes" age_range: "Age range" add_price: "Add a price" - booking_nominative: "Nominative booking" - booking_nominative_help: "If you check this option, the members will have to enter the names of the participants when booking." save: "Save" create_success: "The event was created successfully" events_updated: "{COUNT, plural, =1{One event was} other{{COUNT} Events were}} successfully updated" @@ -153,6 +151,11 @@ en: every_week: "Every week" every_month: "Every month" every_year: "Every year" + event_type: "Event type" + event_types: + standard: "Event standard" + nominative: "Event nominative" + family: "Event family" plan_form: ACTION_title: "{ACTION, select, create{New} other{Update the}} plan" tab_settings: "Settings" diff --git a/config/locales/app.admin.fr.yml b/config/locales/app.admin.fr.yml index 67970617b..22cfd5e86 100644 --- a/config/locales/app.admin.fr.yml +++ b/config/locales/app.admin.fr.yml @@ -139,8 +139,6 @@ fr: event_themes: "Thèmes de l'événement" age_range: "Tranche d'âge" add_price: "Ajouter un tarif" - booking_nominative: "Réservation nominative" - booking_nominative_help: "Si cette option est activée, les réservations seront nominatives. Les participants devront s'identifier pour réserver." save: "Enregistrer" create_success: "L'événement a bien été créé" events_updated: "{COUNT, plural, one {}=1{Un événement à été} other{{COUNT} événements ont été}} mis à jour avec succès" @@ -153,6 +151,11 @@ fr: every_week: "Chaque semaine" every_month: "Chaque mois" every_year: "Chaque année" + event_type: "Type d'événement" + event_types: + standard: "Evénement standard" + nominative: "Evénement nominatif" + family: "Evénement famille" plan_form: ACTION_title: "{ACTION, select, create{Nouvelle} other{Mettre à jour la}} formule d'abonnement" tab_settings: "Paramètres" diff --git a/db/migrate/20230509121907_add_booking_nominative_to_event.rb b/db/migrate/20230509121907_add_booking_nominative_to_event.rb deleted file mode 100644 index 1a709170d..000000000 --- a/db/migrate/20230509121907_add_booking_nominative_to_event.rb +++ /dev/null @@ -1,8 +0,0 @@ -# frozen_string_literal: true - -# add booking_nominative to event -class AddBookingNominativeToEvent < ActiveRecord::Migration[7.0] - def change - add_column :events, :booking_nominative, :boolean, default: false - end -end diff --git a/db/migrate/20230510141305_create_cart_item_event_reservation_booking_users.rb b/db/migrate/20230510141305_create_cart_item_event_reservation_booking_users.rb index bbaae5626..e5d1a3465 100644 --- a/db/migrate/20230510141305_create_cart_item_event_reservation_booking_users.rb +++ b/db/migrate/20230510141305_create_cart_item_event_reservation_booking_users.rb @@ -5,7 +5,8 @@ class CreateCartItemEventReservationBookingUsers < ActiveRecord::Migration[7.0] def change create_table :cart_item_event_reservation_booking_users do |t| t.string :name - t.belongs_to :cart_item_event_reservation, foreign_key: true, index: { name: 'index_cart_item_booking_users_on_cart_item_event_reservation' } + t.belongs_to :cart_item_event_reservation, foreign_key: true, + index: { name: 'index_cart_item_booking_users_on_cart_item_event_reservation' } t.references :event_price_category, foreign_key: true, index: { name: 'index_cart_item_booking_users_on_event_price_category' } t.references :booked, polymorphic: true diff --git a/db/migrate/20230511081018_add_event_type_to_event.rb b/db/migrate/20230511081018_add_event_type_to_event.rb new file mode 100644 index 000000000..4f1f832a9 --- /dev/null +++ b/db/migrate/20230511081018_add_event_type_to_event.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +# Add event_type to event model, to be able to create standard/nominative/family events +class AddEventTypeToEvent < ActiveRecord::Migration[7.0] + def change + add_column :events, :event_type, :string, default: 'standard' + Event.reset_column_information + Event.update_all(event_type: 'standard') + end +end diff --git a/db/structure.sql b/db/structure.sql index c19642c69..35a9af3e8 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1243,7 +1243,7 @@ CREATE TABLE public.events ( age_range_id integer, category_id integer, deleted_at timestamp without time zone, - booking_nominative boolean DEFAULT false + event_type character varying DEFAULT 'standard'::character varying ); @@ -8927,6 +8927,8 @@ INSERT INTO "schema_migrations" (version) VALUES ('20230331132506'), ('20230509121907'), ('20230509161557'), -('20230510141305'); +('20230510141305'), +('20230511080650'), +('20230511081018'); diff --git a/test/fixtures/history_values.yml b/test/fixtures/history_values.yml index 42f684753..523ddea7f 100644 --- a/test/fixtures/history_values.yml +++ b/test/fixtures/history_values.yml @@ -858,5 +858,4 @@ history_value_101: value: 'false' created_at: '2023-03-31 14:38:40.000421' updated_at: '2023-03-31 14:38:40.000421' - footprint: invoicing_profile_id: 1 diff --git a/test/frontend/components/events/event-form.test.tsx b/test/frontend/components/events/event-form.test.tsx index 49f50ce79..219542a23 100644 --- a/test/frontend/components/events/event-form.test.tsx +++ b/test/frontend/components/events/event-form.test.tsx @@ -15,6 +15,7 @@ describe('EventForm', () => { expect(screen.getByLabelText(/app.admin.event_form.title/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.matching_visual/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.description/)).toBeInTheDocument(); + expect(screen.getByLabelText(/app.admin.event_form.event_type/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.event_category/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.event_themes/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.age_range/)).toBeInTheDocument(); @@ -27,7 +28,6 @@ describe('EventForm', () => { expect(screen.getByLabelText(/app.admin.event_form._and_ends_on/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.seats_available/)).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.event_form.standard_rate/)).toBeInTheDocument(); - expect(screen.getByLabelText(/app.admin.event_form.booking_nominative/)).toBeInTheDocument(); expect(screen.getByRole('button', { name: /app.admin.event_form.add_price/ })).toBeInTheDocument(); expect(screen.getByRole('button', { name: /app.admin.event_form.add_a_new_file/ })).toBeInTheDocument(); expect(screen.getByLabelText(/app.admin.advanced_accounting_form.code/)).toBeInTheDocument(); diff --git a/test/integration/events/as_admin_test.rb b/test/integration/events/as_admin_test.rb index e4d721ed4..5d59eef64 100644 --- a/test/integration/events/as_admin_test.rb +++ b/test/integration/events/as_admin_test.rb @@ -22,6 +22,7 @@ class Events::AsAdminTest < ActionDispatch::IntegrationTest end_date: 1.week.from_now.utc, end_time: 1.week.from_now.utc.change(hour: 20), category_id: Category.first.id, + event_type: 'standard', amount: 0 } }.to_json,