Commit 49b66f05 authored by Zac Wood's avatar Zac Wood

Deal with CORS in a less hacky way

parent 459ba299
......@@ -53,7 +53,7 @@ end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'httparty'
gem 'icalendar'
gem 'nokogiri'
gem 'rack-cors', require: 'rack/cors'
gem 'rubyXL'
gem 'icalendar'
......@@ -96,6 +96,7 @@ GEM
public_suffix (3.0.2)
puma (3.11.3)
rack (2.0.4)
rack-cors (1.0.2)
rack-test (1.0.0)
rack (>= 1.0, < 3)
rails (5.1.6)
......@@ -193,6 +194,7 @@ DEPENDENCIES
pry
pry-doc
puma (~> 3.7)
rack-cors
rails (~> 5.1.6)
rubyXL
sass-rails (~> 5.0)
......
......@@ -4,9 +4,8 @@ require 'time'
class CalendarGeneratorController < ApplicationController
def generate
cal = Icalendar::Calendar.new
posted_crns = JSON.parse(request.body.read)
posted_crns.each do |crn|
params[:_json].each do |crn|
section = Section.find_by_crn(crn)
event = generate_event_from_section(section)
cal.add_event(event)
......
......@@ -14,8 +14,13 @@ module Schedules
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.action_dispatch.default_headers = {
'Access-Control-Allow-Origin' => '*'
}
# allow GET, POST or OPTIONS requests from any origin on any resource
config.middleware.insert_before 0, Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
end
......@@ -32,20 +32,23 @@ class App extends React.Component<any, State> {
}
addSectionToCurrentScheduleIfUnique = (section: Section) => {
if (!this.state.currentSchedule.find(sectionInSchedule => section === sectionInSchedule)) {
if (!this.isSectionInSchedule(section)) {
this.setState({
currentSchedule: [...this.state.currentSchedule, section],
});
}
};
isSectionInSchedule = (section: Section) =>
this.state.currentSchedule.find(sectionInSchedule => section === sectionInSchedule);
generateSchedule = () => {
const crns = this.state.currentSchedule.map(section => section.crn);
fetch('http://localhost:3000/api/generate', {
method: 'POST',
body: JSON.stringify(crns),
headers: {
'Content-Type': 'text/plain',
'Content-Type': 'application/json',
},
})
.then(response => response.text())
......
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