forked from seanpdoyle/select-your-own-seat
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
258 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { createAction } from '@reduxjs/toolkit' | ||
import { SAVE_RESPONSE, BEFORE_VISIT, UPDATE_FRAGMENTS } from '@thoughtbot/superglue' | ||
|
||
export const saveResponse = createAction(SAVE_RESPONSE) | ||
export const beforeVisit = createAction(BEFORE_VISIT) | ||
export const updateFragments = createAction(UPDATE_FRAGMENTS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,48 @@ | ||
// Entry point for the build script in your package.json | ||
import React from 'react'; | ||
import thunk from 'redux-thunk'; | ||
import { Provider } from 'react-redux'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { ApplicationBase } from '@thoughtbot/superglue'; | ||
import { buildVisitAndRemote } from './application_visit'; | ||
import { pageIdentifierToPageComponent } from './page_to_page_mapping'; | ||
import { buildStore } from './store' | ||
|
||
class Application extends ApplicationBase { | ||
mapping() { | ||
return pageIdentifierToPageComponent; | ||
} | ||
|
||
visitAndRemote(navRef, store) { | ||
return buildVisitAndRemote(navRef, store); | ||
} | ||
|
||
buildStore(initialState, { superglue, pages}) { | ||
return buildStore(initialState, superglue, pages); | ||
} | ||
} | ||
|
||
if (typeof window !== "undefined") { | ||
document.addEventListener("DOMContentLoaded", function () { | ||
const appEl = document.getElementById("app"); | ||
const location = window.location; | ||
|
||
if (appEl) { | ||
const root = createRoot(appEl); | ||
root.render( | ||
<Application | ||
appEl={appEl} | ||
// The base url prefixed to all calls made by the `visit` | ||
// and `remote` thunks. | ||
baseUrl={location.origin} | ||
// The global var SUPERGLUE_INITIAL_PAGE_STATE is set by your erb | ||
// template, e.g., index.html.erb | ||
initialPage={window.SUPERGLUE_INITIAL_PAGE_STATE} | ||
// The initial path of the page, e.g., /foobar | ||
path={location.pathname + location.search + location.hash} | ||
/> | ||
); | ||
} | ||
}); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { visit, remote } from '@thoughtbot/superglue/action_creators' | ||
|
||
export function buildVisitAndRemote(ref, store) { | ||
const appRemote = (...args) => { | ||
return store.dispatch(remote(...args)) | ||
} | ||
|
||
const appVisit = (...args) => { | ||
// Do something before | ||
// e.g, show loading state, you can access the current pageKey | ||
// via store.getState().superglue.currentPageKey | ||
let { action } = args | ||
|
||
return store | ||
.dispatch(visit(...args)) | ||
.then((meta) => { | ||
// The assets fingerprints changed, instead of transitioning | ||
// just go to the URL directly to retrieve new assets | ||
if (meta.needsRefresh) { | ||
window.location = meta.url | ||
return | ||
} | ||
|
||
ref.current.navigateTo(meta.pageKey, { | ||
action: meta.suggestedAction, | ||
}) | ||
|
||
// always return meta | ||
return meta | ||
}) | ||
.finally(() => { | ||
// Do something after | ||
// e.g, hide loading state, you can access the changed pageKey | ||
// via getState().superglue.currentPageKey | ||
}) | ||
.catch((err) => { | ||
const response = err.response | ||
|
||
if (!response) { | ||
console.error(err) | ||
return | ||
} | ||
|
||
if (response.ok) { | ||
// err gets thrown, but if the response is ok, | ||
// it must be an html body that | ||
// superglue can't parse, just go to the location | ||
window.location = response.url | ||
} else { | ||
if (response.status >= 400 && response.status < 500) { | ||
window.location = '/400.html' | ||
return | ||
} | ||
|
||
if (response.status >= 500) { | ||
window.location = '/500.html' | ||
return | ||
} | ||
} | ||
}) | ||
} | ||
|
||
return { visit: appVisit, remote: appRemote } | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// import your page component | ||
// e.g import PostsEdit from '../views/posts/edit' | ||
|
||
|
||
// Mapping between your props template to Component, you must add to this | ||
// to register any new page level component you create. If you are using the | ||
// scaffold, it will auto append the identifers for you. | ||
// | ||
// e.g {'posts/new': PostNew} | ||
export const pageIdentifierToPageComponent = { | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
import { saveResponse, beforeVisit } from '../actions' | ||
|
||
export const flashSlice = createSlice({ | ||
name: 'flash', | ||
initialState: {}, | ||
extraReducers: (builder) => { | ||
builder.addCase(beforeVisit, (state, action) => { | ||
return {} | ||
}) | ||
builder.addCase(saveResponse, (state, action) => { | ||
const { page } = action.payload; | ||
|
||
return { | ||
...state, ...page.slices.flash | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { createSlice } from '@reduxjs/toolkit' | ||
import { saveResponse, beforeVisit } from '../actions' | ||
|
||
export const pagesSlice = createSlice({ | ||
name: 'pages', | ||
// extraReducers: (builder) => { | ||
// builder.addCase(beforeVisit, (state, action) => { | ||
// const {currentPageKey} = action.payload | ||
// | ||
// const currentPage = draft[currentPageKey] | ||
// delete currentPage.error | ||
// }) | ||
// } | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { configureStore } from '@reduxjs/toolkit' | ||
import { pagesSlice } from "./slices/pages" | ||
import { flashSlice } from "./slices/flash" | ||
import { | ||
BEFORE_VISIT, | ||
BEFORE_FETCH, | ||
BEFORE_REMOTE, | ||
fragmentMiddleware | ||
} from '@thoughtbot/superglue' | ||
|
||
export const buildStore = (initialState, superglueReducer, supergluePagesReducer) => { | ||
|
||
return configureStore({ | ||
preloadedState: initialState, | ||
devTools: process.env.NODE_ENV !== 'production', | ||
middleware: (getDefaultMiddleware) => | ||
getDefaultMiddleware({ | ||
serializableCheck: { | ||
ignoredActions: [BEFORE_VISIT, BEFORE_FETCH, BEFORE_REMOTE], | ||
}, | ||
}).concat(fragmentMiddleware), | ||
reducer: { | ||
superglue: superglueReducer, | ||
pages: (state, action) => { | ||
const nextState = supergluePagesReducer(state, action) | ||
return pagesSlice.reducer(nextState, action) | ||
}, | ||
flash: flashSlice.reducer | ||
}, | ||
}); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
class ApplicationRecord < ActiveRecord::Base | ||
def self.member_at(index) | ||
offset(index).limit(1).first | ||
end | ||
|
||
def self.member_by(attr, value) | ||
find_by(Hash[attr, value]) | ||
end | ||
self.abstract_class = true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
path = request.format.json? ? param_to_search_path(params[:props_at]) : nil | ||
|
||
json.data(search: path) do | ||
yield json | ||
end | ||
|
||
json.component_identifier local_assigns[:virtual_path_of_template] | ||
json.defers json.deferred! | ||
json.fragments json.fragments! | ||
json.assets [ asset_path('application.js') ] | ||
|
||
if protect_against_forgery? | ||
json.csrf_token form_authenticity_token | ||
end | ||
|
||
if path | ||
json.action 'graft' | ||
json.path search_path_to_camelized_param(path) | ||
end | ||
|
||
json.restore_strategy 'fromCacheAndRevisitInBackground' | ||
|
||
json.rendered_at Time.now.to_i | ||
|
||
json.slices do | ||
json.flash flash.to_h | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require "props_template/core_ext" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters