collavre_plan 0.1.2 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/app/components/collavre/plans_timeline_component.rb +3 -65
- data/app/controllers/collavre_plan/plans_controller.rb +22 -87
- data/app/javascript/controllers/__tests__/index.test.js +22 -0
- data/app/javascript/controllers/creatives/__tests__/set_plan_button_controller.test.js +55 -0
- data/app/javascript/controllers/creatives/__tests__/set_plan_modal_controller.test.js +102 -0
- data/app/javascript/controllers/creatives/set_plan_button_controller.js +11 -0
- data/app/javascript/controllers/creatives/set_plan_modal_controller.js +10 -23
- data/app/javascript/controllers/index.js +2 -0
- data/app/javascript/modules/__tests__/plans_menu.test.js +101 -0
- data/app/javascript/modules/plans_menu.js +34 -44
- data/app/services/collavre_plan/timeline_items.rb +74 -0
- data/app/views/collavre_plan/creatives/_set_plan_button.html.erb +1 -1
- data/lib/collavre_plan/version.rb +1 -1
- metadata +8 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f4010d48c48d0b49b8a455b19033e00fb4f5b4c76cba4d3b80f158b9c9a2cfa3
|
|
4
|
+
data.tar.gz: bbf626b7803939f57c3763d72e156980a1e06ebc9b3fcddbb378ebd64cec0b63
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c94480962005879853538886b6f63463a14d758a6c62f388232b61d3d461891ba7dc5862796f8dc82151a72283795ad65d648cb90cfadb707955b5a836c0b383
|
|
7
|
+
data.tar.gz: b69cba6b19cd352b45718900f6a7add74d41b2a6402f81fb72194d3284f6873f905f4d4b7bf0e8dae5b668f777c220323e429a0184f9c00c470789e22582069a
|
|
@@ -16,71 +16,9 @@ class PlansTimelineComponent < ViewComponent::Base
|
|
|
16
16
|
|
|
17
17
|
# Called after component enters render context - safe to use helpers here
|
|
18
18
|
def plan_data
|
|
19
|
-
@plan_data ||=
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
@modifications.map { |creative| modification_item(creative) }
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
private
|
|
26
|
-
|
|
27
|
-
def plan_item(plan)
|
|
28
|
-
{
|
|
29
|
-
id: plan.id,
|
|
30
|
-
name: (plan.creative&.effective_description(nil, false) || plan.name.presence || I18n.l(plan.target_date)),
|
|
31
|
-
created_at: plan.created_at.to_date,
|
|
32
|
-
start_date: plan.start_date,
|
|
33
|
-
target_date: plan.target_date,
|
|
34
|
-
progress: plan.progress,
|
|
35
|
-
path: plan_creatives_path(plan),
|
|
36
|
-
deletable: plan.owner_id == Current.user&.id
|
|
37
|
-
}
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
def calendar_item(event)
|
|
41
|
-
{
|
|
42
|
-
id: "calendar_event_#{event.id}",
|
|
43
|
-
name: event.summary.presence || I18n.l(event.start_time.to_date),
|
|
44
|
-
created_at: event.start_time.to_date,
|
|
45
|
-
target_date: event.end_time.to_date,
|
|
46
|
-
progress: event.creative&.progress || 0,
|
|
47
|
-
path: event.creative ? helpers.collavre.creative_path(event.creative) : event.html_link,
|
|
48
|
-
deletable: event.user_id == Current.user&.id
|
|
49
|
-
}
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def registration_item(creative)
|
|
53
|
-
{
|
|
54
|
-
id: "registration_#{creative.id}",
|
|
55
|
-
type: "registration",
|
|
56
|
-
name: (creative.effective_description(nil, false).presence || I18n.t("collavre.plans.registration_fallback", id: creative.id)),
|
|
57
|
-
created_at: creative.created_at.to_date,
|
|
58
|
-
target_date: creative.created_at.to_date,
|
|
59
|
-
progress: creative.progress,
|
|
60
|
-
path: helpers.collavre.creative_path(creative),
|
|
61
|
-
deletable: false
|
|
62
|
-
}
|
|
63
|
-
end
|
|
64
|
-
|
|
65
|
-
def modification_item(creative)
|
|
66
|
-
{
|
|
67
|
-
id: "modification_#{creative.id}",
|
|
68
|
-
type: "modification",
|
|
69
|
-
name: (creative.effective_description(nil, false).presence || I18n.t("collavre.plans.modification_fallback", id: creative.id)),
|
|
70
|
-
created_at: creative.updated_at.to_date,
|
|
71
|
-
target_date: creative.updated_at.to_date,
|
|
72
|
-
progress: creative.progress,
|
|
73
|
-
path: helpers.collavre.creative_path(creative),
|
|
74
|
-
deletable: false
|
|
75
|
-
}
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
def plan_creatives_path(plan)
|
|
79
|
-
if helpers.params[:id].present?
|
|
80
|
-
helpers.collavre.creative_path(helpers.params[:id], tags: [ plan.id ])
|
|
81
|
-
else
|
|
82
|
-
helpers.collavre.creatives_path(tags: [ plan.id ])
|
|
83
|
-
end
|
|
19
|
+
@plan_data ||= CollavrePlan::TimelineItems.new(creative_id: helpers.params[:id], routes: helpers.collavre).all(
|
|
20
|
+
plans: @plans, calendar_events: @calendar_events, registrations: @registrations, modifications: @modifications
|
|
21
|
+
)
|
|
84
22
|
end
|
|
85
23
|
end
|
|
86
24
|
end
|
|
@@ -39,11 +39,8 @@ module CollavrePlan
|
|
|
39
39
|
render html: render_to_string(Collavre::PlansTimelineComponent.new(plans: @plans, calendar_events: @calendar_events, registrations: @registrations, show_registrations: @show_registrations, modifications: @modifications, show_modifications: @show_modifications), layout: false)
|
|
40
40
|
end
|
|
41
41
|
format.json do
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
registration_jsons = @registrations.map { |c| registration_json(c) }
|
|
45
|
-
modification_jsons = @modifications.map { |c| modification_json(c) }
|
|
46
|
-
render json: plan_jsons + event_jsons + registration_jsons + modification_jsons
|
|
42
|
+
render json: timeline_items.all(plans: @plans, calendar_events: @calendar_events,
|
|
43
|
+
registrations: @registrations, modifications: @modifications)
|
|
47
44
|
end
|
|
48
45
|
end
|
|
49
46
|
end
|
|
@@ -57,19 +54,11 @@ module CollavrePlan
|
|
|
57
54
|
redirect_back fallback_location: main_app.root_path, notice: t("collavre.plans.created")
|
|
58
55
|
end
|
|
59
56
|
format.json do
|
|
60
|
-
render json:
|
|
57
|
+
render json: timeline_items.plan(@plan), status: :created
|
|
61
58
|
end
|
|
62
59
|
end
|
|
63
60
|
else
|
|
64
|
-
|
|
65
|
-
format.html do
|
|
66
|
-
flash[:alert] = @plan.errors.full_messages.join(", ")
|
|
67
|
-
redirect_back fallback_location: main_app.root_path
|
|
68
|
-
end
|
|
69
|
-
format.json do
|
|
70
|
-
render json: { errors: @plan.errors.full_messages }, status: :unprocessable_entity
|
|
71
|
-
end
|
|
72
|
-
end
|
|
61
|
+
respond_with_plan_errors
|
|
73
62
|
end
|
|
74
63
|
end
|
|
75
64
|
|
|
@@ -98,24 +87,32 @@ module CollavrePlan
|
|
|
98
87
|
notice: t("collavre.plans.updated", default: "Plan updated.")
|
|
99
88
|
end
|
|
100
89
|
format.json do
|
|
101
|
-
render json:
|
|
90
|
+
render json: timeline_items(params[:creative_id] || @plan.creative_id).plan(@plan), status: :ok
|
|
102
91
|
end
|
|
103
92
|
end
|
|
104
93
|
else
|
|
105
|
-
|
|
106
|
-
format.html do
|
|
107
|
-
flash[:alert] = @plan.errors.full_messages.join(", ")
|
|
108
|
-
redirect_back fallback_location: main_app.root_path
|
|
109
|
-
end
|
|
110
|
-
format.json do
|
|
111
|
-
render json: { errors: @plan.errors.full_messages }, status: :unprocessable_entity
|
|
112
|
-
end
|
|
113
|
-
end
|
|
94
|
+
respond_with_plan_errors
|
|
114
95
|
end
|
|
115
96
|
end
|
|
116
97
|
|
|
117
98
|
private
|
|
118
99
|
|
|
100
|
+
def timeline_items(creative_id = nil)
|
|
101
|
+
TimelineItems.new(creative_id: creative_id.presence || params[:id])
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def respond_with_plan_errors
|
|
105
|
+
respond_to do |format|
|
|
106
|
+
format.html do
|
|
107
|
+
flash[:alert] = @plan.errors.full_messages.join(", ")
|
|
108
|
+
redirect_back fallback_location: main_app.root_path
|
|
109
|
+
end
|
|
110
|
+
format.json do
|
|
111
|
+
render json: { errors: @plan.errors.full_messages }, status: :unprocessable_entity
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
119
116
|
def plan_params
|
|
120
117
|
params.require(:plan).permit(:target_date, :start_date, :creative_id)
|
|
121
118
|
end
|
|
@@ -161,19 +158,6 @@ module CollavrePlan
|
|
|
161
158
|
Collavre::Plan.where.not(creative_id: nil).select(:creative_id)
|
|
162
159
|
end
|
|
163
160
|
|
|
164
|
-
def registration_json(creative)
|
|
165
|
-
{
|
|
166
|
-
id: "registration_#{creative.id}",
|
|
167
|
-
type: "registration",
|
|
168
|
-
name: (creative.effective_description(nil, false).presence || I18n.t("collavre.plans.registration_fallback", id: creative.id)),
|
|
169
|
-
created_at: creative.created_at.to_date,
|
|
170
|
-
target_date: creative.created_at.to_date,
|
|
171
|
-
progress: creative.progress,
|
|
172
|
-
path: Collavre::Engine.routes.url_helpers.creative_path(creative),
|
|
173
|
-
deletable: false
|
|
174
|
-
}
|
|
175
|
-
end
|
|
176
|
-
|
|
177
161
|
# Modified creatives owned by the current user, drawn at their updated_at.
|
|
178
162
|
# We only include creatives genuinely edited after they came into being, so
|
|
179
163
|
# the "Modified" chip stays distinct from "Registered" rather than duplicating
|
|
@@ -202,54 +186,5 @@ module CollavrePlan
|
|
|
202
186
|
.limit(MODIFICATION_LIMIT)
|
|
203
187
|
.to_a
|
|
204
188
|
end
|
|
205
|
-
|
|
206
|
-
def modification_json(creative)
|
|
207
|
-
{
|
|
208
|
-
id: "modification_#{creative.id}",
|
|
209
|
-
type: "modification",
|
|
210
|
-
name: (creative.effective_description(nil, false).presence || I18n.t("collavre.plans.modification_fallback", id: creative.id)),
|
|
211
|
-
created_at: creative.updated_at.to_date,
|
|
212
|
-
target_date: creative.updated_at.to_date,
|
|
213
|
-
progress: creative.progress,
|
|
214
|
-
path: Collavre::Engine.routes.url_helpers.creative_path(creative),
|
|
215
|
-
deletable: false
|
|
216
|
-
}
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
def plan_json(plan, creative_id: nil)
|
|
220
|
-
{
|
|
221
|
-
id: plan.id,
|
|
222
|
-
name: (plan.creative&.effective_description(nil, false) || plan.name.presence || I18n.l(plan.target_date)),
|
|
223
|
-
created_at: plan.created_at.to_date,
|
|
224
|
-
start_date: plan.start_date,
|
|
225
|
-
target_date: plan.target_date,
|
|
226
|
-
progress: plan.progress,
|
|
227
|
-
path: plan_creatives_path(plan, creative_id: creative_id),
|
|
228
|
-
deletable: plan.owner_id == Current.user&.id
|
|
229
|
-
}
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
def calendar_json(event)
|
|
233
|
-
{
|
|
234
|
-
id: "calendar_event_#{event.id}",
|
|
235
|
-
name: event.summary.presence || I18n.l(event.start_time.to_date),
|
|
236
|
-
created_at: event.start_time.to_date,
|
|
237
|
-
target_date: event.end_time.to_date,
|
|
238
|
-
progress: event.creative&.progress || 0,
|
|
239
|
-
path: event.creative ? Collavre::Engine.routes.url_helpers.creative_path(event.creative) : event.html_link,
|
|
240
|
-
deletable: event.user_id == Current.user&.id
|
|
241
|
-
}
|
|
242
|
-
end
|
|
243
|
-
|
|
244
|
-
def plan_creatives_path(plan, creative_id: nil)
|
|
245
|
-
collavre_routes = Collavre::Engine.routes.url_helpers
|
|
246
|
-
if creative_id.present?
|
|
247
|
-
collavre_routes.creative_path(creative_id, tags: [ plan.id ])
|
|
248
|
-
elsif params[:id].present?
|
|
249
|
-
collavre_routes.creative_path(params[:id], tags: [ plan.id ])
|
|
250
|
-
else
|
|
251
|
-
collavre_routes.creatives_path(tags: [ plan.id ])
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
189
|
end
|
|
255
190
|
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { jest } from '@jest/globals'
|
|
6
|
+
|
|
7
|
+
jest.unstable_mockModule('collavre/lib/utils/dialog', () => ({ alertDialog: jest.fn() }))
|
|
8
|
+
|
|
9
|
+
const { registerControllers } = await import('../index')
|
|
10
|
+
const { default: SetPlanButtonController } = await import('../creatives/set_plan_button_controller')
|
|
11
|
+
const { default: SetPlanModalController } = await import('../creatives/set_plan_modal_controller')
|
|
12
|
+
|
|
13
|
+
describe('registerControllers', () => {
|
|
14
|
+
test('registers the set plan button and modal controllers', () => {
|
|
15
|
+
const application = { register: jest.fn() }
|
|
16
|
+
|
|
17
|
+
registerControllers(application)
|
|
18
|
+
|
|
19
|
+
expect(application.register).toHaveBeenCalledWith('creatives--set-plan-button', SetPlanButtonController)
|
|
20
|
+
expect(application.register).toHaveBeenCalledWith('creatives--set-plan-modal', SetPlanModalController)
|
|
21
|
+
})
|
|
22
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { jest } from '@jest/globals'
|
|
5
|
+
import { Application } from '@hotwired/stimulus'
|
|
6
|
+
|
|
7
|
+
jest.unstable_mockModule('collavre/lib/utils/dialog', () => ({ alertDialog: jest.fn() }))
|
|
8
|
+
|
|
9
|
+
const { default: SetPlanButtonController } = await import('../set_plan_button_controller')
|
|
10
|
+
const { default: SetPlanModalController } = await import('../set_plan_modal_controller')
|
|
11
|
+
|
|
12
|
+
describe('SetPlanButtonController', () => {
|
|
13
|
+
let application
|
|
14
|
+
|
|
15
|
+
beforeEach(async () => {
|
|
16
|
+
document.body.innerHTML = `
|
|
17
|
+
<form id="toolbar-form">
|
|
18
|
+
<button id="set-plan-btn"
|
|
19
|
+
data-controller="creatives--set-plan-button"
|
|
20
|
+
data-action="click->creatives--set-plan-button#open">Set Plan</button>
|
|
21
|
+
</form>
|
|
22
|
+
<div data-controller="creatives--set-plan-modal">
|
|
23
|
+
<div data-creatives--set-plan-modal-target="modal" style="display:none"></div>
|
|
24
|
+
</div>
|
|
25
|
+
`
|
|
26
|
+
application = Application.start()
|
|
27
|
+
application.register('creatives--set-plan-button', SetPlanButtonController)
|
|
28
|
+
application.register('creatives--set-plan-modal', SetPlanModalController)
|
|
29
|
+
await Promise.resolve()
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
afterEach(() => {
|
|
33
|
+
application.stop()
|
|
34
|
+
document.body.innerHTML = ''
|
|
35
|
+
document.body.classList.remove('no-scroll')
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('opens the set-plan modal that lives outside the button scope', () => {
|
|
39
|
+
document.getElementById('set-plan-btn').click()
|
|
40
|
+
|
|
41
|
+
expect(document.querySelector('[data-creatives--set-plan-modal-target="modal"]').style.display).toBe('flex')
|
|
42
|
+
expect(document.body.classList.contains('no-scroll')).toBe(true)
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
test('prevents the default button action', () => {
|
|
46
|
+
const submitted = jest.fn((event) => event.preventDefault())
|
|
47
|
+
document.getElementById('toolbar-form').addEventListener('submit', submitted)
|
|
48
|
+
const event = new MouseEvent('click', { bubbles: true, cancelable: true })
|
|
49
|
+
|
|
50
|
+
document.getElementById('set-plan-btn').dispatchEvent(event)
|
|
51
|
+
|
|
52
|
+
expect(event.defaultPrevented).toBe(true)
|
|
53
|
+
expect(submitted).not.toHaveBeenCalled()
|
|
54
|
+
})
|
|
55
|
+
})
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @jest-environment jsdom
|
|
3
|
+
*/
|
|
4
|
+
import { jest } from '@jest/globals'
|
|
5
|
+
import { Application } from '@hotwired/stimulus'
|
|
6
|
+
|
|
7
|
+
const alertDialog = jest.fn()
|
|
8
|
+
jest.unstable_mockModule('collavre/lib/utils/dialog', () => ({ alertDialog }))
|
|
9
|
+
|
|
10
|
+
const { default: SetPlanModalController } = await import('../set_plan_modal_controller')
|
|
11
|
+
|
|
12
|
+
describe('SetPlanModalController plan requests', () => {
|
|
13
|
+
let application
|
|
14
|
+
const originalFetch = global.fetch
|
|
15
|
+
|
|
16
|
+
async function mount({ checked = ['3', '5'], planId = '9' } = {}) {
|
|
17
|
+
document.head.innerHTML = '<meta name="csrf-token" content="token-1">'
|
|
18
|
+
document.body.innerHTML = `
|
|
19
|
+
<input type="checkbox" class="select-creative-checkbox" value="3">
|
|
20
|
+
<input type="checkbox" class="select-creative-checkbox" value="4">
|
|
21
|
+
<input type="checkbox" class="select-creative-checkbox" value="5">
|
|
22
|
+
<div data-controller="set-plan-modal"
|
|
23
|
+
data-set-plan-modal-select-one-value="Select a creative"
|
|
24
|
+
data-set-plan-modal-select-plan-value="Select a plan">
|
|
25
|
+
<div data-set-plan-modal-target="modal" style="display:flex">
|
|
26
|
+
<form data-set-plan-modal-target="form" action="/plans/assign"
|
|
27
|
+
data-action="submit->set-plan-modal#submit">
|
|
28
|
+
<input type="hidden" data-set-plan-modal-target="idsInput">
|
|
29
|
+
<select data-set-plan-modal-target="planSelect">
|
|
30
|
+
<option value=""></option>
|
|
31
|
+
<option value="9">Plan</option>
|
|
32
|
+
</select>
|
|
33
|
+
<button type="button" id="remove" data-remove-path="/plans/unassign"
|
|
34
|
+
data-action="click->set-plan-modal#remove"></button>
|
|
35
|
+
</form>
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
`
|
|
39
|
+
document.querySelectorAll('.select-creative-checkbox').forEach((checkbox) => {
|
|
40
|
+
checkbox.checked = checked.includes(checkbox.value)
|
|
41
|
+
})
|
|
42
|
+
document.querySelector('select').value = planId
|
|
43
|
+
application = Application.start()
|
|
44
|
+
application.register('set-plan-modal', SetPlanModalController)
|
|
45
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function act(trigger) {
|
|
49
|
+
trigger()
|
|
50
|
+
await new Promise((resolve) => setTimeout(resolve, 0))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const submit = () => document.querySelector('form').dispatchEvent(new Event('submit', { bubbles: true, cancelable: true }))
|
|
54
|
+
const remove = () => document.getElementById('remove').click()
|
|
55
|
+
|
|
56
|
+
beforeEach(() => {
|
|
57
|
+
global.fetch = jest.fn().mockResolvedValue({ ok: true, json: () => Promise.resolve({ message: 'Done' }) })
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
afterEach(() => {
|
|
61
|
+
application.stop()
|
|
62
|
+
global.fetch = originalFetch
|
|
63
|
+
document.head.innerHTML = ''
|
|
64
|
+
document.body.innerHTML = ''
|
|
65
|
+
jest.clearAllMocks()
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test.each([
|
|
69
|
+
['submit', submit, 'http://localhost/plans/assign', 'POST'],
|
|
70
|
+
['remove', remove, '/plans/unassign', 'DELETE'],
|
|
71
|
+
])('%s sends the selected creatives and plan, then closes the modal', async (_name, trigger, url, method) => {
|
|
72
|
+
await mount()
|
|
73
|
+
|
|
74
|
+
await act(trigger)
|
|
75
|
+
|
|
76
|
+
expect(global.fetch).toHaveBeenCalledWith(url, {
|
|
77
|
+
method,
|
|
78
|
+
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json', 'X-CSRF-Token': 'token-1' },
|
|
79
|
+
body: JSON.stringify({ plan_id: '9', creative_ids: '3,5' }),
|
|
80
|
+
})
|
|
81
|
+
expect(alertDialog).toHaveBeenCalledWith('Done')
|
|
82
|
+
expect(document.querySelector('[data-set-plan-modal-target="modal"]').style.display).toBe('none')
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
test.each([['submit', submit], ['remove', remove]])('%s asks for a creative when none is selected', async (_name, trigger) => {
|
|
86
|
+
await mount({ checked: [] })
|
|
87
|
+
|
|
88
|
+
await act(trigger)
|
|
89
|
+
|
|
90
|
+
expect(alertDialog).toHaveBeenCalledWith('Select a creative')
|
|
91
|
+
expect(global.fetch).not.toHaveBeenCalled()
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test.each([['submit', submit], ['remove', remove]])('%s asks for a plan when none is chosen', async (_name, trigger) => {
|
|
95
|
+
await mount({ planId: '' })
|
|
96
|
+
|
|
97
|
+
await act(trigger)
|
|
98
|
+
|
|
99
|
+
expect(alertDialog).toHaveBeenCalledWith('Select a plan')
|
|
100
|
+
expect(global.fetch).not.toHaveBeenCalled()
|
|
101
|
+
})
|
|
102
|
+
})
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus'
|
|
2
|
+
|
|
3
|
+
// Opens the set-plan modal from the creative toolbar button. The button lives
|
|
4
|
+
// outside the modal controller's scope, so it signals via a document event
|
|
5
|
+
// instead of an inline onclick (blocked by the CSP script-src policy).
|
|
6
|
+
export default class extends Controller {
|
|
7
|
+
open(event) {
|
|
8
|
+
event.preventDefault()
|
|
9
|
+
document.dispatchEvent(new CustomEvent('plan:open-modal'))
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -48,43 +48,30 @@ export default class extends Controller {
|
|
|
48
48
|
async submit(event) {
|
|
49
49
|
event.preventDefault()
|
|
50
50
|
if (!this.hasFormTarget || !this.hasIdsInputTarget) return
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
if (ids.length === 0) {
|
|
54
|
-
this.alert(this.selectOneValue)
|
|
55
|
-
return
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const planId = this.planSelectTarget.value
|
|
59
|
-
if (!planId) {
|
|
60
|
-
this.alert(this.selectPlanValue)
|
|
61
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
await this.performRequest(this.formTarget.action, 'POST', {
|
|
65
|
-
plan_id: planId,
|
|
66
|
-
creative_ids: ids.join(',')
|
|
67
|
-
})
|
|
51
|
+
const body = this.selectionPayload()
|
|
52
|
+
if (body) await this.performRequest(this.formTarget.action, 'POST', body)
|
|
68
53
|
}
|
|
69
54
|
|
|
70
55
|
async remove(event) {
|
|
71
56
|
event.preventDefault()
|
|
57
|
+
const body = this.selectionPayload()
|
|
58
|
+
if (body) await this.performRequest(event.currentTarget.dataset.removePath, 'DELETE', body)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
selectionPayload() {
|
|
72
62
|
const ids = this.selectedIds()
|
|
73
63
|
if (ids.length === 0) {
|
|
74
64
|
this.alert(this.selectOneValue)
|
|
75
|
-
return
|
|
65
|
+
return null
|
|
76
66
|
}
|
|
77
67
|
|
|
78
68
|
const planId = this.planSelectTarget.value
|
|
79
69
|
if (!planId) {
|
|
80
70
|
this.alert(this.selectPlanValue)
|
|
81
|
-
return
|
|
71
|
+
return null
|
|
82
72
|
}
|
|
83
73
|
|
|
84
|
-
|
|
85
|
-
plan_id: planId,
|
|
86
|
-
creative_ids: ids.join(',')
|
|
87
|
-
})
|
|
74
|
+
return { plan_id: planId, creative_ids: ids.join(',') }
|
|
88
75
|
}
|
|
89
76
|
|
|
90
77
|
async performRequest(url, method, body) {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// CollavrePlan Engine Controllers
|
|
2
|
+
import CreativesSetPlanButtonController from "./creatives/set_plan_button_controller"
|
|
2
3
|
import CreativesSetPlanModalController from "./creatives/set_plan_modal_controller"
|
|
3
4
|
|
|
4
5
|
export function registerControllers(application) {
|
|
6
|
+
application.register("creatives--set-plan-button", CreativesSetPlanButtonController)
|
|
5
7
|
application.register("creatives--set-plan-modal", CreativesSetPlanModalController)
|
|
6
8
|
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { jest } from '@jest/globals'
|
|
2
|
+
|
|
3
|
+
const notifyPopupOpen = jest.fn()
|
|
4
|
+
const onOtherPopupOpen = jest.fn()
|
|
5
|
+
jest.unstable_mockModule('collavre/lib/gnb_popup_manager', () => ({
|
|
6
|
+
notifyPopupOpen, onOtherPopupOpen
|
|
7
|
+
}), { virtual: true })
|
|
8
|
+
|
|
9
|
+
await import('../plans_menu.js')
|
|
10
|
+
|
|
11
|
+
function renderMenu() {
|
|
12
|
+
document.body.innerHTML = `
|
|
13
|
+
<button class="plans-menu-btn"><span>Plans</span></button>
|
|
14
|
+
<button class="plans-menu-btn" id="mobile-plans">Plans</button>
|
|
15
|
+
<div id="plans-list-area" style="display:none" data-plans-url="/mounted/plans.json">
|
|
16
|
+
<div id="plans-timeline"></div>
|
|
17
|
+
</div>`
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function navigate() {
|
|
21
|
+
document.dispatchEvent(new window.Event('turbo:load'))
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function click(selector = '.plans-menu-btn span') {
|
|
25
|
+
document.querySelector(selector).click()
|
|
26
|
+
await new Promise(resolve => setTimeout(resolve, 0))
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
beforeEach(() => {
|
|
30
|
+
renderMenu()
|
|
31
|
+
global.fetch = jest.fn().mockResolvedValue({ json: async () => [{ id: 1 }] })
|
|
32
|
+
window.initPlansTimeline = jest.fn()
|
|
33
|
+
notifyPopupOpen.mockClear()
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
test('opens once after repeated navigation with the GNB preserved', async () => {
|
|
37
|
+
navigate()
|
|
38
|
+
navigate()
|
|
39
|
+
await click()
|
|
40
|
+
expect(document.getElementById('plans-list-area').style.display).toBe('block')
|
|
41
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
42
|
+
expect(fetch).toHaveBeenCalledWith('/mounted/plans.json')
|
|
43
|
+
expect(notifyPopupOpen).toHaveBeenCalledTimes(1)
|
|
44
|
+
expect(window.initPlansTimeline).toHaveBeenCalledWith(document.getElementById('plans-timeline'))
|
|
45
|
+
expect(document.getElementById('plans-timeline').dataset.plans).toBe('[{"id":1}]')
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('desktop and mobile toggle the same panel and reuse loaded plans', async () => {
|
|
49
|
+
navigate()
|
|
50
|
+
await click()
|
|
51
|
+
await click('#mobile-plans')
|
|
52
|
+
expect(document.getElementById('plans-list-area').style.display).toBe('none')
|
|
53
|
+
await click('#mobile-plans')
|
|
54
|
+
expect(document.getElementById('plans-list-area').style.display).toBe('block')
|
|
55
|
+
expect(fetch).toHaveBeenCalledTimes(1)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('loads the new panel after full page replacement', async () => {
|
|
59
|
+
navigate()
|
|
60
|
+
await click()
|
|
61
|
+
renderMenu()
|
|
62
|
+
navigate()
|
|
63
|
+
await click()
|
|
64
|
+
expect(document.getElementById('plans-list-area').style.display).toBe('block')
|
|
65
|
+
expect(fetch).toHaveBeenCalledTimes(2)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('other popups close the current panel after replacement', async () => {
|
|
69
|
+
navigate()
|
|
70
|
+
renderMenu()
|
|
71
|
+
navigate()
|
|
72
|
+
await click()
|
|
73
|
+
onOtherPopupOpen.mock.calls.at(-1)[1]()
|
|
74
|
+
expect(document.getElementById('plans-list-area').style.display).toBe('none')
|
|
75
|
+
document.body.innerHTML = ''
|
|
76
|
+
expect(() => onOtherPopupOpen.mock.calls.at(-1)[1]()).not.toThrow()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('ignores unrelated clicks and pages without a plans panel', async () => {
|
|
80
|
+
navigate()
|
|
81
|
+
await click('#plans-timeline')
|
|
82
|
+
document.getElementById('plans-list-area').remove()
|
|
83
|
+
await click()
|
|
84
|
+
expect(fetch).not.toHaveBeenCalled()
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test('supports the fallback endpoint and an absent timeline', async () => {
|
|
88
|
+
document.getElementById('plans-list-area').removeAttribute('data-plans-url')
|
|
89
|
+
document.getElementById('plans-timeline').remove()
|
|
90
|
+
navigate()
|
|
91
|
+
await click()
|
|
92
|
+
expect(fetch).toHaveBeenCalledWith('/plans.json')
|
|
93
|
+
expect(window.initPlansTimeline).not.toHaveBeenCalled()
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('stores plans when the timeline initializer is unavailable', async () => {
|
|
97
|
+
delete window.initPlansTimeline
|
|
98
|
+
navigate()
|
|
99
|
+
await click()
|
|
100
|
+
expect(document.getElementById('plans-timeline').dataset.plans).toBe('[{"id":1}]')
|
|
101
|
+
})
|
|
@@ -1,50 +1,40 @@
|
|
|
1
1
|
// Plans menu functionality
|
|
2
|
-
//
|
|
2
|
+
// Delegate clicks once: workspace navigation preserves the GNB across turbo:load.
|
|
3
3
|
|
|
4
4
|
import { notifyPopupOpen, onOtherPopupOpen } from 'collavre/lib/gnb_popup_manager'
|
|
5
5
|
|
|
6
6
|
const POPUP_ID = 'plans-menu'
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
loaded = true
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
} else {
|
|
43
|
-
area.style.display = 'none'
|
|
44
|
-
}
|
|
45
|
-
})
|
|
7
|
+
const loadedAreas = new WeakSet()
|
|
8
|
+
|
|
9
|
+
onOtherPopupOpen(POPUP_ID, function() {
|
|
10
|
+
const area = document.getElementById('plans-list-area')
|
|
11
|
+
if (area) area.style.display = 'none'
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
document.addEventListener('click', function(event) {
|
|
15
|
+
if (!event.target.closest('.plans-menu-btn')) return
|
|
16
|
+
|
|
17
|
+
const area = document.getElementById('plans-list-area')
|
|
18
|
+
if (!area) return
|
|
19
|
+
|
|
20
|
+
if (area.style.display !== 'none') {
|
|
21
|
+
area.style.display = 'none'
|
|
22
|
+
return
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
notifyPopupOpen(POPUP_ID)
|
|
26
|
+
area.style.display = 'block'
|
|
27
|
+
if (loadedAreas.has(area)) return
|
|
28
|
+
|
|
29
|
+
const timeline = document.getElementById('plans-timeline')
|
|
30
|
+
const plansUrl = area.dataset.plansUrl || '/plans.json'
|
|
31
|
+
fetch(plansUrl)
|
|
32
|
+
.then(function(r) { return r.json() })
|
|
33
|
+
.then(function(plans) {
|
|
34
|
+
if (timeline) { timeline.dataset.plans = JSON.stringify(plans) }
|
|
35
|
+
if (window.initPlansTimeline && timeline) {
|
|
36
|
+
window.initPlansTimeline(timeline)
|
|
37
|
+
}
|
|
38
|
+
loadedAreas.add(area)
|
|
46
39
|
})
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
initPlansMenu()
|
|
40
|
+
})
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
module CollavrePlan
|
|
2
|
+
class TimelineItems
|
|
3
|
+
# routes: pass a request-aware proxy (e.g. helpers.collavre) to keep the caller's script name.
|
|
4
|
+
def initialize(creative_id: nil, routes: Collavre::Engine.routes.url_helpers)
|
|
5
|
+
@creative_id = creative_id
|
|
6
|
+
@routes = routes
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def all(plans:, calendar_events:, registrations:, modifications:)
|
|
10
|
+
plans.map { |record| plan(record) } +
|
|
11
|
+
calendar_events.map { |event| calendar_event(event) } +
|
|
12
|
+
registrations.map { |creative| registration(creative) } +
|
|
13
|
+
modifications.map { |creative| modification(creative) }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def plan(plan)
|
|
17
|
+
{
|
|
18
|
+
id: plan.id,
|
|
19
|
+
name: (plan.creative&.effective_description(nil, false) || plan.name.presence || I18n.l(plan.target_date)),
|
|
20
|
+
created_at: plan.created_at.to_date,
|
|
21
|
+
start_date: plan.start_date,
|
|
22
|
+
target_date: plan.target_date,
|
|
23
|
+
progress: plan.progress,
|
|
24
|
+
path: plan_creatives_path(plan),
|
|
25
|
+
deletable: plan.owner_id == Collavre::Current.user&.id
|
|
26
|
+
}
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def calendar_event(event)
|
|
30
|
+
{
|
|
31
|
+
id: "calendar_event_#{event.id}",
|
|
32
|
+
name: event.summary.presence || I18n.l(event.start_time.to_date),
|
|
33
|
+
created_at: event.start_time.to_date,
|
|
34
|
+
target_date: event.end_time.to_date,
|
|
35
|
+
progress: event.creative&.progress || 0,
|
|
36
|
+
path: event.creative ? @routes.creative_path(event.creative) : event.html_link,
|
|
37
|
+
deletable: event.user_id == Collavre::Current.user&.id
|
|
38
|
+
}
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def registration(creative)
|
|
42
|
+
marker(creative, "registration", creative.created_at.to_date,
|
|
43
|
+
I18n.t("collavre.plans.registration_fallback", id: creative.id))
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def modification(creative)
|
|
47
|
+
marker(creative, "modification", creative.updated_at.to_date,
|
|
48
|
+
I18n.t("collavre.plans.modification_fallback", id: creative.id))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def marker(creative, type, date, fallback_name)
|
|
54
|
+
{
|
|
55
|
+
id: "#{type}_#{creative.id}",
|
|
56
|
+
type: type,
|
|
57
|
+
name: creative.effective_description(nil, false).presence || fallback_name,
|
|
58
|
+
created_at: date,
|
|
59
|
+
target_date: date,
|
|
60
|
+
progress: creative.progress,
|
|
61
|
+
path: @routes.creative_path(creative),
|
|
62
|
+
deletable: false
|
|
63
|
+
}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def plan_creatives_path(plan)
|
|
67
|
+
if @creative_id.present?
|
|
68
|
+
@routes.creative_path(@creative_id, tags: [ plan.id ])
|
|
69
|
+
else
|
|
70
|
+
@routes.creatives_path(tags: [ plan.id ])
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -1 +1 @@
|
|
|
1
|
-
<button id="set-plan-btn" class="set-plan-btn" style="display:none;" data-creatives--
|
|
1
|
+
<button type="button" id="set-plan-btn" class="set-plan-btn" style="display:none;" data-controller="creatives--set-plan-button" data-action="click->creatives--set-plan-button#open" data-creatives--select-mode-target="setPlan"><%= t('app.set_plan') %></button>
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: collavre_plan
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.3
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Collavre
|
|
@@ -52,12 +52,18 @@ files:
|
|
|
52
52
|
- app/controllers/collavre_plan/creative_plans_controller.rb
|
|
53
53
|
- app/controllers/collavre_plan/plans_controller.rb
|
|
54
54
|
- app/javascript/collavre_plan.js
|
|
55
|
+
- app/javascript/controllers/__tests__/index.test.js
|
|
56
|
+
- app/javascript/controllers/creatives/__tests__/set_plan_button_controller.test.js
|
|
57
|
+
- app/javascript/controllers/creatives/__tests__/set_plan_modal_controller.test.js
|
|
58
|
+
- app/javascript/controllers/creatives/set_plan_button_controller.js
|
|
55
59
|
- app/javascript/controllers/creatives/set_plan_modal_controller.js
|
|
56
60
|
- app/javascript/controllers/index.js
|
|
61
|
+
- app/javascript/modules/__tests__/plans_menu.test.js
|
|
57
62
|
- app/javascript/modules/plans_menu.js
|
|
58
63
|
- app/javascript/modules/plans_timeline.js
|
|
59
64
|
- app/models/collavre/plan.rb
|
|
60
65
|
- app/services/collavre/creatives/plan_tagger.rb
|
|
66
|
+
- app/services/collavre_plan/timeline_items.rb
|
|
61
67
|
- app/views/collavre/creatives/_set_plan_modal.html.erb
|
|
62
68
|
- app/views/collavre/labels/_plan_extra.html.erb
|
|
63
69
|
- app/views/collavre/labels/_plan_suffix.html.erb
|
|
@@ -91,7 +97,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
91
97
|
- !ruby/object:Gem::Version
|
|
92
98
|
version: '0'
|
|
93
99
|
requirements: []
|
|
94
|
-
rubygems_version: 3.6.
|
|
100
|
+
rubygems_version: 3.6.9
|
|
95
101
|
specification_version: 4
|
|
96
102
|
summary: Plan feature for Collavre
|
|
97
103
|
test_files: []
|