@@ -5,8 +5,8 @@ import {NoticeView} from '../components/notice'
55import type { TopLevelViewPropsType } from '../types'
66import { FancyMenu } from './components/fancy-menu'
77import 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
4747type Props = TopLevelViewPropsType & {
48- cafeId : string ,
48+ cafe : string | { id : string } ,
4949 ignoreProvidedMenus ?: boolean ,
5050 loadingMessage : string [ ] ,
5151 name : string ,
5252}
5353type 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
6363export 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
0 commit comments