Skip to content

Commit 906b595

Browse files
committed
Merge branch 'bonapp-fix-menus' into frogpond-data
* bonapp-fix-menus: prettify globals switch to naming menus switch to naming olaf menus
2 parents 658c510 + e341fbe commit 906b595

File tree

6 files changed

+92
-64
lines changed

6 files changed

+92
-64
lines changed

source/globals.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
import qs from 'querystring'
44

55
const root = 'https://stolaf.api.frogpond.tech/v1'
6-
export const API = (pth: string, query: ?Object=null) => {
6+
export const API = (pth: string, query: ?Object = null) => {
77
if (process.env.NODE_ENV !== 'production') {
88
if (!pth.startsWith('/')) {
99
throw new Error('invalid path requested from the api!')
1010
}
1111
}
12-
let url = root + pth
12+
let url = root + pth
1313
if (query) {
1414
url += `?${qs.stringify(query)}`
1515
}

source/views/menus/carleton-menus.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const CarletonBurtonMenuScreen = ({
1111
navigation,
1212
}: TopLevelViewPropsType) => (
1313
<BonAppHostedMenu
14-
cafeId="35"
14+
cafe="burton"
1515
loadingMessage={['Searching for Schiller…']}
1616
name="Burton"
1717
navigation={navigation}
@@ -24,7 +24,7 @@ CarletonBurtonMenuScreen.navigationOptions = {
2424

2525
export const CarletonLDCMenuScreen = ({navigation}: TopLevelViewPropsType) => (
2626
<BonAppHostedMenu
27-
cafeId="36"
27+
cafe="ldc"
2828
loadingMessage={['Tracking down empty seats…']}
2929
name="LDC"
3030
navigation={navigation}
@@ -39,7 +39,7 @@ export const CarletonWeitzMenuScreen = ({
3939
navigation,
4040
}: TopLevelViewPropsType) => (
4141
<BonAppHostedMenu
42-
cafeId="458"
42+
cafe="weitz"
4343
loadingMessage={['Observing the artwork…', 'Previewing performances…']}
4444
name="Weitz Center"
4545
navigation={navigation}
@@ -54,7 +54,7 @@ export const CarletonSaylesMenuScreen = ({
5454
navigation,
5555
}: TopLevelViewPropsType) => (
5656
<BonAppHostedMenu
57-
cafeId="34"
57+
cafe="sayles"
5858
loadingMessage={['Engaging in people-watching…', 'Checking the mail…']}
5959
name="Sayles Hill"
6060
navigation={navigation}

source/views/menus/dev-bonapp-picker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export class BonAppPickerView extends React.PureComponent<Props, State> {
6262
</Toolbar>
6363
<BonAppHostedMenu
6464
key={this.state.cafeId}
65-
cafeId={this.state.cafeId}
65+
cafe={{id: this.state.cafeId}}
6666
loadingMessage={['Loading…']}
6767
name="BonApp"
6868
navigation={this.props.navigation}

source/views/menus/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const MenusView = TabNavigator({
2020
StavHallMenuView: {
2121
screen: ({navigation}) => (
2222
<BonAppHostedMenu
23-
cafeId="261"
23+
cafe="stav-hall"
2424
loadingMessage={[
2525
'Hunting Ferndale Turkey…',
2626
'Tracking wild vegan burgers…',
@@ -42,7 +42,7 @@ export const MenusView = TabNavigator({
4242
TheCageMenuView: {
4343
screen: ({navigation}) => (
4444
<BonAppHostedMenu
45-
cafeId="262"
45+
cafe="the-cage"
4646
ignoreProvidedMenus={true}
4747
loadingMessage={[
4848
'Checking for vegan cookies…',

source/views/menus/menu-bonapp.js

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import {NoticeView} from '../components/notice'
55
import type {TopLevelViewPropsType} from '../types'
66
import {FancyMenu} from './components/fancy-menu'
77
import type {
8-
BonAppMenuInfoType as MenuInfoType,
9-
BonAppCafeInfoType as CafeInfoType,
8+
EditedBonAppMenuInfoType as MenuInfoType,
9+
EditedBonAppCafeInfoType as CafeInfoType,
1010
StationMenuType,
1111
ProcessedMealType,
1212
DayPartMenuType,
@@ -45,13 +45,13 @@ const DEFAULT_MENU = [
4545
]
4646

4747
type Props = TopLevelViewPropsType & {
48-
cafeId: string,
48+
cafe: string | {id: string},
4949
ignoreProvidedMenus?: boolean,
5050
loadingMessage: string[],
5151
name: string,
5252
}
5353
type State = {
54-
cachedCafeId: string,
54+
cachedCafe: string | {id: string},
5555
errormsg: ?string,
5656
loading: boolean,
5757
refreshing: boolean,
@@ -62,7 +62,7 @@ type State = {
6262

6363
export class BonAppHostedMenu extends React.PureComponent<Props, State> {
6464
state = {
65-
cachedCafeId: this.props.cafeId,
65+
cachedCafe: this.props.cafe,
6666
errormsg: null,
6767
loading: true,
6868
refreshing: false,
@@ -78,7 +78,12 @@ export class BonAppHostedMenu extends React.PureComponent<Props, State> {
7878
}
7979

8080
componentDidUpdate() {
81-
if (this.state.cachedCafeId !== this.props.cafeId) {
81+
if (
82+
(typeof this.state.cachedCafe === 'string' &&
83+
this.state.cachedCafe !== this.props.cafe) ||
84+
(typeof this.state.cachedCafe !== 'string' &&
85+
this.state.cachedCafe.id !== this.props.cafe.id)
86+
) {
8287
this.fetchData(this.props)
8388
}
8489
}
@@ -89,17 +94,26 @@ export class BonAppHostedMenu extends React.PureComponent<Props, State> {
8994
})
9095
}
9196

92-
requestMenu = (cafeId: string) => () => fetchJson(API(`/food/menu/${cafeId}`))
93-
requestCafe = (cafeId: string) => () => fetchJson(API(`/food/cafe/${cafeId}`))
94-
9597
fetchData = async (props: Props) => {
9698
let cafeMenu: ?MenuInfoType = null
9799
let cafeInfo: ?CafeInfoType = null
98100

101+
let menuUrl
102+
let cafeUrl
103+
if (typeof props.cafe === 'string') {
104+
menuUrl = API(`/food/named/menu/${props.cafe}`)
105+
cafeUrl = API(`/food/named/cafe/${props.cafe}`)
106+
} else if (props.cafe.hasOwnProperty('id')) {
107+
menuUrl = API(`/food/menu/${props.cafe.id}`)
108+
cafeUrl = API(`/food/cafe/${props.cafe.id}`)
109+
} else {
110+
throw new Error('invalid cafe passed to BonappMenu!')
111+
}
112+
99113
try {
100114
;[cafeMenu, cafeInfo] = await Promise.all([
101-
retry(this.requestMenu(props.cafeId), {retries: 3}),
102-
retry(this.requestCafe(props.cafeId), {retries: 3}),
115+
retry(() => fetchJson(menuUrl), {retries: 3}),
116+
retry(() => fetchJson(cafeUrl), {retries: 3}),
103117
])
104118
} catch (error) {
105119
if (error.message === "JSON Parse error: Unrecognized token '<'") {
@@ -129,11 +143,8 @@ export class BonAppHostedMenu extends React.PureComponent<Props, State> {
129143
this.setState(() => ({refreshing: false}))
130144
}
131145

132-
findCafeMessage(cafeId: string, cafeInfo: CafeInfoType, now: momentT) {
133-
const actualCafeInfo = cafeInfo.cafes[cafeId]
134-
if (!actualCafeInfo) {
135-
return 'BonApp did not return a menu for that café'
136-
}
146+
findCafeMessage(cafeInfo: CafeInfoType, now: momentT) {
147+
const actualCafeInfo = cafeInfo.cafe
137148

138149
const todayDate = now.format('YYYY-MM-DD')
139150
const todayMenu = actualCafeInfo.days.find(({date}) => date === todayDate)
@@ -199,16 +210,15 @@ export class BonAppHostedMenu extends React.PureComponent<Props, State> {
199210

200211
getMeals(args: {
201212
cafeMenu: MenuInfoType,
202-
cafeId: string,
203213
ignoreProvidedMenus: boolean,
204214
foodItems: MenuItemContainerType,
205215
}) {
206-
const {cafeMenu, cafeId, ignoreProvidedMenus, foodItems} = args
216+
const {cafeMenu, ignoreProvidedMenus, foodItems} = args
207217

208218
// We hard-code to the first day returned because we're only requesting
209219
// one day. `cafes` is a map of cafe ids to cafes, but we only request one
210220
// cafe at a time, so we just grab the one we requested.
211-
const dayparts = cafeMenu.days[0].cafes[cafeId].dayparts
221+
const dayparts = cafeMenu.days[0].cafe.dayparts
212222

213223
// either use the meals as provided by bonapp, or make our own
214224
const mealInfoItems = dayparts[0].length ? dayparts[0] : DEFAULT_MENU
@@ -243,30 +253,31 @@ export class BonAppHostedMenu extends React.PureComponent<Props, State> {
243253
}
244254

245255
if (!this.state.cafeMenu || !this.state.cafeInfo) {
246-
const err = new Error(
247-
`Something went wrong loading BonApp cafe #${this.props.cafeId}`,
248-
)
256+
let cafe =
257+
typeof this.props.cafe === 'string'
258+
? this.props.cafe
259+
: this.props.cafe.id
260+
const err = new Error(`Something went wrong loading BonApp cafe #${cafe}`)
249261
tracker.trackException(err.message)
250262
bugsnag.notify(err)
251263

252264
const msg = 'Something went wrong. Email [email protected] to let them know?'
253265
return <NoticeView text={msg} />
254266
}
255267

256-
const {cafeId, ignoreProvidedMenus = false} = this.props
268+
const {ignoreProvidedMenus = false} = this.props
257269
const {now, cafeMenu, cafeInfo} = this.state
258270

259271
// We grab the "today" info from here because BonApp returns special
260272
// messages in this response, like "Closed for Christmas Break"
261-
const specialMessage = this.findCafeMessage(cafeId, cafeInfo, now)
273+
const specialMessage = this.findCafeMessage(cafeInfo, now)
262274

263275
// prepare all food items from bonapp for rendering
264276
const foodItems = this.prepareFood(cafeMenu)
265277

266278
const meals = this.getMeals({
267279
foodItems,
268280
ignoreProvidedMenus,
269-
cafeId,
270281
cafeMenu,
271282
})
272283

source/views/menus/types.js

Lines changed: 47 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,23 @@ export type MenuForDayType = {
7878
cafes: {[key: string]: CafeMenuType},
7979
}
8080

81+
export type EditedMenuForDayType = {
82+
date: string,
83+
cafe: CafeMenuType,
84+
}
85+
8186
export type BonAppMenuInfoType = {
8287
cor_icons: {[key: string]: Object},
8388
days: MenuForDayType[],
8489
items: MenuItemContainerType,
8590
}
8691

92+
export type EditedBonAppMenuInfoType = {
93+
cor_icons: {[key: string]: Object},
94+
days: EditedMenuForDayType[],
95+
items: MenuItemContainerType,
96+
}
97+
8798
export type CorIconType = {
8899
sort: string,
89100
label: string,
@@ -99,43 +110,49 @@ export type ItemCorIconMapType =
99110
| Array<void>
100111
export type MasterCorIconMapType = {[key: NumericStringType]: CorIconType}
101112

102-
export type BonAppCafeInfoType = {
103-
cafes: {
104-
[key: string]: {
105-
name: string,
106-
address: string,
107-
city: string,
108-
state: string,
109-
zip: string,
110-
latitude: string,
111-
longitude: string,
112-
description: string,
113-
message: string,
114-
eod: string,
115-
timezone: string,
116-
menu_type: string,
117-
menu_html: string,
118-
weekly_schedule: string,
119-
days: [
113+
type BonAppSingleCafeInfo = {
114+
name: string,
115+
address: string,
116+
city: string,
117+
state: string,
118+
zip: string,
119+
latitude: string,
120+
longitude: string,
121+
description: string,
122+
message: string,
123+
eod: string,
124+
timezone: string,
125+
menu_type: string,
126+
menu_html: string,
127+
weekly_schedule: string,
128+
days: [
129+
{
130+
date: string,
131+
dayparts: [
120132
{
121-
date: string,
122-
dayparts: [
123-
{
124-
id: string,
125-
starttime: string,
126-
endtime: string,
127-
message: string,
128-
label: string,
129-
},
130-
],
131-
status: 'open' | 'closed' | string,
132-
message: false | string,
133+
id: string,
134+
starttime: string,
135+
endtime: string,
136+
message: string,
137+
label: string,
133138
},
134139
],
140+
status: 'open' | 'closed' | string,
141+
message: false | string,
135142
},
143+
],
144+
}
145+
146+
export type BonAppCafeInfoType = {
147+
cafes: {
148+
[key: string]: BonAppSingleCafeInfo,
136149
},
137150
}
138151

152+
export type EditedBonAppCafeInfoType = {
153+
cafe: BonAppSingleCafeInfo,
154+
}
155+
139156
export type ProcessedMealType = {|
140157
starttime: MilitaryTimeStringType,
141158
endtime: MilitaryTimeStringType,

0 commit comments

Comments
 (0)