Commit b95c13a2 authored by Zac Wood's avatar Zac Wood
Browse files

Semesters

parent 9821b71c
...@@ -31,3 +31,8 @@ document.addEventListener('DOMContentLoaded', () => { ...@@ -31,3 +31,8 @@ document.addEventListener('DOMContentLoaded', () => {
document.addEventListener('turbolinks:load', () => { document.addEventListener('turbolinks:load', () => {
FontAwesome.dom.i2svg(); FontAwesome.dom.i2svg();
}); });
const setSemester = async select => {
const resp = await fetch(`/sessions/update?semester_id=${select.value}`);
location.reload(true);
};
...@@ -12,7 +12,7 @@ class Schedule { ...@@ -12,7 +12,7 @@ class Schedule {
this._ids = ids; this._ids = ids;
document.getElementById('course-counter').innerText = ids.length; document.getElementById('course-counter').innerText = ids.length;
fetch('/search/update?ids=' + ids.join(','), { cache: 'no-store' }); fetch('/sessions/update?crns=' + ids.join(','), { cache: 'no-store' });
} }
toggle() { toggle() {
......
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
// Place all the styles related to the sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
# Configures the application. # Configures the application.
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session protect_from_forgery with: :null_session
before_action :set_cookies, :set_cart, :set_semester before_action :set_semester, :set_cookies, :set_cart
def set_semester
@semester = if cookies.key?(:semester_id)
Semester.find_by(id: cookies[:semester_id])
else
Semester.find_by(season: 'Spring', year: '2019')
end
end
def set_cart def set_cart
@cart = cookies[:ids].split(',').map do |crn| @cart = cookies[:crns].split(',').map do |crn|
CourseSection.find_by_crn crn s = CourseSection.find_by_crn(crn)
s if s.course.semester == @semester
end end
end
def set_cookies @cart.compact!
cookies[:ids] = "" if cookies[:ids].nil?
end end
def set_semester def set_cookies
@semester = if params.key?(:semester_id) cookies[:crns] = "" if cookies[:crns].nil?
Semester.find_by(id: params[:semester_id])
else
Semester.find_by(season: 'Fall', year: '2018')
end
end end
end end
...@@ -7,6 +7,9 @@ class InstructorsController < ApplicationController ...@@ -7,6 +7,9 @@ class InstructorsController < ApplicationController
def show def show
sections = CourseSection.where instructor: @instructor sections = CourseSection.where instructor: @instructor
sections = sections.select do |s|
s.course.semester == @semester
end
# TODO: move this to a model somewhere # TODO: move this to a model somewhere
@courses = [].to_set @courses = [].to_set
......
...@@ -4,10 +4,4 @@ class SearchController < ApplicationController ...@@ -4,10 +4,4 @@ class SearchController < ApplicationController
course.course_sections.count.positive? course.course_sections.count.positive?
end end
end end
def update
cookies[:ids] = params[:ids]
head :ok
end
end end
class SessionsController < ApplicationController
def update
set_cookie :crns
set_cookie :semester_id
head :ok
end
private
def update_cookie(sym)
cookies[sym] = params[sym] unless params[sym].nil?
end
end
<h1>Sessions#update</h1>
<p>Find me in app/views/sessions/update.html.erb</p>
<div class="container-fluid"> <div class="container-fluid">
<div class="row align-left align-sm-center align-md-right" id="navbar"> <div class="row align-left align-sm-center align-md-right" id="navbar">
<div class="col-8 col-sm align-center"> <div class="col-8 col-sm align-center">
<a href="/" id="logo"> <div>
<i class="fas fa-calendar-alt"></i> <a href="/" id="logo">
Schedules <i class="fas fa-calendar-alt"></i>
</a> Schedules
</a>
<select onchange="setSemester(this)">
<% for semester in Semester.all %>
<option value="<%= semester.id %>" <% if @semester == semester %> selected <% end %> >
<%= "#{semester.season} #{semester.year}" %>
</option>
<% end %>
</select>
</div>
</div> </div>
<div class="col-4 col-sm align-center order-0 order-sm-1" onclick="window.schedule.toggle()"> <div class="col-4 col-sm align-center order-0 order-sm-1" onclick="window.schedule.toggle()">
......
# Registers all routes for the app. # Registers all routes for the app.
Rails.application.routes.draw do Rails.application.routes.draw do
get 'search', to: 'search#index' get 'search', to: 'search#index'
get 'search/update', to: 'search#update', as: 'update_cookie' get 'sessions/update', as: 'update_session'
resources :instructors, only: [:index, :show] resources :instructors, only: [:index, :show]
......
...@@ -110,7 +110,7 @@ def main ...@@ -110,7 +110,7 @@ def main
wipe_db wipe_db
parser = PatriotWeb::Parser.new parser = PatriotWeb::Parser.new
semesters = [parser.parse_semesters.first] # expand to include however many semesters you want semesters = parser.parse_semesters[0..1] # expand to include however many semesters you want
courses = nil courses = nil
semesters.each do |semester| semesters.each do |semester|
......
...@@ -5,9 +5,4 @@ class SearchControllerTest < ActionDispatch::IntegrationTest ...@@ -5,9 +5,4 @@ class SearchControllerTest < ActionDispatch::IntegrationTest
get search_url get search_url
assert_response :success assert_response :success
end end
test "should update cookie" do
get update_cookie_url crns: '71926,71924'
assert_response :success
end
end end
require 'test_helper'
class SessionsControllerTest < ActionDispatch::IntegrationTest
# test "should get update" do
# get update_session_url
# assert_response :success
# end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment