Skip to content

Commit c12c157

Browse files
committed
WIP
1 parent 1749ae6 commit c12c157

File tree

13 files changed

+94
-19
lines changed

13 files changed

+94
-19
lines changed
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
class MoviesController < ApplicationController
22
def show
3-
# TODO: fetch if not in session
4-
json = session["#{params['title']}/#{params['year']}"]
5-
@movie = RecursiveOstruct.from(json)
3+
title = params['title']
4+
year = params['year']
5+
6+
@movie =
7+
RecursiveOstruct.from(
8+
session.fetch("#{title}/#{year}") do
9+
Movie.get(title, year)
10+
end
11+
)
12+
613
@page_title = "#{@movie.title} (#{@movie.year})"
7-
@poster_url = @movie.poster_url || vite_asset_path('images/default-poster.png')
14+
@poster_url = @movie.poster_url
815
end
916
end
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
class SearchController < ApplicationController
22
def new
3+
@query = params[:query]
4+
end
5+
6+
def create
37
# TODO: don't fetch if in session
48
# TODO: use redis session. cookie will explode
5-
result = JSON.parse(`should-i-watch-this lookup -f json #{params[:query]} -l`)
6-
if result['error']
7-
# TODO: error
8-
else
9-
title = result['title']
10-
year = result['year']
11-
session["#{title}/#{year}"] = result
9+
movie = Movie.search(params[:query])
10+
if movie
11+
title = movie['title']
12+
year = movie['year']
13+
session["#{title}/#{year}"] = movie
1214
redirect_to movie_url(title, year)
15+
else
1316
end
1417
end
1518
end

www_rails/app/javascript/controllers/application.js renamed to www_rails/app/frontend/controllers/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Application } from "@hotwired/stimulus"
2+
import { registerControllers } from 'stimulus-vite-helpers'
23

34
const application = Application.start()
5+
const controllers = import.meta.glob('./**/*_controller.js', { eager: true })
6+
registerControllers(application, controllers)
47

58
// Configure Stimulus development experience
69
application.debug = false

www_rails/app/javascript/controllers/hello_controller.js renamed to www_rails/app/frontend/controllers/search_controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ import { Controller } from "@hotwired/stimulus"
22

33
export default class extends Controller {
44
connect() {
5-
this.element.textContent = "Hello World!"
5+
this.element.submit()
66
}
77
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
const spinnerCharacters = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]
4+
5+
export default class extends Controller {
6+
connect() {
7+
this.currentIndex = 0
8+
9+
this.onInterval(() => {
10+
this.currentIndex = (this.currentIndex + 1) % spinnerCharacters.length
11+
this.setCurrentCharacter()
12+
}, 60)
13+
}
14+
15+
setCurrentCharacter() {
16+
this.element.textContent = spinnerCharacters[this.currentIndex]
17+
}
18+
19+
onInterval(callback, milliseconds) {
20+
const interval = setInterval(callback, milliseconds)
21+
}
22+
}

www_rails/app/frontend/entrypoints/application.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ console.log('Visit the guide for more information: ', 'https://vite-ruby.netlify
1515

1616
// Example: Load Rails libraries in Vite.
1717
//
18-
// import * as Turbo from '@hotwired/turbo'
19-
// Turbo.start()
18+
import * as Turbo from '@hotwired/turbo'
19+
Turbo.start()
2020
//
2121
// import ActiveStorage from '@rails/activestorage'
2222
// ActiveStorage.start()
@@ -26,3 +26,5 @@ console.log('Visit the guide for more information: ', 'https://vite-ruby.netlify
2626

2727
// Example: Import a stylesheet in app/frontend/index.css
2828
// import '~/index.css'
29+
//
30+
import '~/controllers/application'

www_rails/app/frontend/entrypoints/application.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,8 @@ a.result-more {
239239
width: 800px;
240240
}
241241
}
242+
243+
// spinner
244+
.spinner {
245+
font-size: 21px;
246+
}

www_rails/app/models/movie.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module Movie
2+
def self.get(title, year)
3+
json = JSON.parse(`should-i-watch-this lookup -f json #{title} -y #{year} -l`)
4+
5+
if json['error']
6+
nil
7+
else
8+
json
9+
end
10+
end
11+
12+
def self.search(query)
13+
json = JSON.parse(`should-i-watch-this lookup -f json #{query} -l`)
14+
15+
if json['error']
16+
nil
17+
else
18+
json
19+
end
20+
end
21+
end

www_rails/app/views/movies/show.html.slim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
= render 'shared/search_box'
22

3+
- @poster_url ||= vite_asset_path('images/default-poster.png')
4+
35
section.box
46
.summary
57
.description
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
= render 'shared/search_box', query: @query
2+
3+
section.box.padded
4+
span.spinner data-controller="spinner"
5+
6+
= form_tag search_path, 'data-controller': 'search'
7+
= hidden_field_tag :query, @query

0 commit comments

Comments
 (0)