Skip to content

Commit

Permalink
Feature: Add more and improve view component previews
Browse files Browse the repository at this point in the history
Because:
* It's more useful the more previews there are

This commit:
* Adds Notification and User::Avatar previews
* Makes use of lookbook tags to group related previews together
* Adds more state to completion components to more easily compare
* Rework Title link component previews as the base component was changed
to Section::LessonComponent
  • Loading branch information
ChargrilledChook committed Nov 9, 2022
1 parent 552faf7 commit 7d54fb0
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module AboutPage
# @label Media Cards
# Note: Name is no longer really representative of what this does, should be refactored to a more general name
class BeliefComponentPreview < ViewComponent::Preview
def with_about_page_collection
render(BeliefComponent.with_collection(I18n.t('static_pages.about.beliefs')))
Expand Down
23 changes: 21 additions & 2 deletions spec/components/previews/complete/button_component_preview.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
module Complete
class ButtonComponentPreview < ViewComponent::Preview
def default
render(ButtonComponent.new(lesson: Lesson.first, current_user: User.first))
# @!group
def uncompleted
render(ButtonComponent.new(lesson: Lesson.first, current_user: User.find(21)))
end

def completed
lesson = Lesson.first
current_user = User.last
complete_lesson!(lesson, User.last)
render(ButtonComponent.new(lesson:, current_user:))
end

private

def complete_lesson!(lesson, user)
user.lesson_completions.create(
lesson_id: lesson.id,
lesson_identifier_uuid: lesson.identifier_uuid,
course_id: lesson.course.id,
path_id: lesson.course.path.id,
)
end
end
end
21 changes: 20 additions & 1 deletion spec/components/previews/complete/icon_component_preview.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
module Complete
class IconComponentPreview < ViewComponent::Preview
def default
# @!group
def uncompleted
render(IconComponent.new(lesson: Lesson.first, current_user: User.first))
end

def completed
lesson = Lesson.first
current_user = User.last
complete_lesson!(lesson, current_user)
render(IconComponent.new(lesson:, current_user:))
end

private

def complete_lesson!(lesson, user)
user.lesson_completions.create(
lesson_id: lesson.id,
lesson_identifier_uuid: lesson.identifier_uuid,
course_id: lesson.course.id,
path_id: lesson.course.path.id,
)
end
end
end
18 changes: 0 additions & 18 deletions spec/components/previews/lessons/title_link_component_preview.rb

This file was deleted.

21 changes: 21 additions & 0 deletions spec/components/previews/notification_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class NotificationComponentPreview < ViewComponent::Preview
# @!group
# @display bg_color "#eee"
def unread
render(NotificationComponent.new(notification: unread_notification))
end

def read
render(NotificationComponent.new(notification: read_notification))
end

private

def unread_notification
Notification.new(id: 1, title: 'test title', message: 'test message')
end

def read_notification
Notification.new(id: 1, title: 'test title', message: 'test message', read_at: DateTime.yesterday)
end
end
36 changes: 36 additions & 0 deletions spec/components/previews/sections/lesson_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Sections
class LessonComponentPreview < ViewComponent::Preview
# @!group
def with_lesson
render(LessonComponent.new(lesson: Lesson.first, current_user: User.first))
end

def with_completed_lesson
lesson = Lesson.first
current_user = User.last
complete_lesson!(lesson, current_user)
render(LessonComponent.new(lesson: Lesson.first, current_user: User.last))
end

def with_project
render(LessonComponent.new(lesson: Lesson.find(22), current_user: User.first))
end

# @param lesson_id
def with_param(lesson_id: 6)
lesson = Lesson.find_by(id: lesson_id) || Lesson.first
render(LessonComponent.new(lesson:, current_user: User.first))
end

private

def complete_lesson!(lesson, user)
user.lesson_completions.create(
lesson_id: lesson.id,
lesson_identifier_uuid: lesson.identifier_uuid,
course_id: lesson.course.id,
path_id: lesson.course.path.id,
)
end
end
end
6 changes: 6 additions & 0 deletions spec/components/previews/user/avatar_component_preview.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class User::AvatarComponentPreview < ViewComponent::Preview
def default
current_user = User.first
render(User::AvatarComponent.new(current_user:, classes: nil))
end
end

0 comments on commit 7d54fb0

Please sign in to comment.