Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ RUN npm install --legacy-peer-deps

WORKDIR /app/admin
COPY admin .
ARG REACT_APP_GOOGLE_MAPS_API_KEY
ENV REACT_APP_GOOGLE_MAPS_API_KEY=$REACT_APP_GOOGLE_MAPS_API_KEY
RUN npm run build

WORKDIR /app/server
Expand Down
85 changes: 83 additions & 2 deletions admin/src/components/EntryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,81 @@ const mapContainerStyle = {
height: '300px',
};

type FrontendConfigResponse = {
googleMapsApiKey?: string;
};

function MapEntryFormBox(props: {
form: MapEntryForm;
allForms?: EntryForm[];
}) {
const [googleMapsApiKey, setGoogleMapsApiKey] = useState(
process.env.REACT_APP_GOOGLE_MAPS_API_KEY || '',
);
const [hasLoadedConfig, setHasLoadedConfig] = useState(
Boolean(process.env.REACT_APP_GOOGLE_MAPS_API_KEY),
);

useEffect(() => {
if (googleMapsApiKey) {
setHasLoadedConfig(true);
return;
}

let isCancelled = false;

fetch('/frontend-config')
.then(async response => {
if (!response.ok) {
throw new Error(`Failed to load config: ${response.status}`);
}

const config = (await response.json()) as FrontendConfigResponse;

if (isCancelled) {
return;
}

setGoogleMapsApiKey(config.googleMapsApiKey || '');
setHasLoadedConfig(true);
})
.catch(() => {
if (!isCancelled) {
setHasLoadedConfig(true);
}
});

return () => {
isCancelled = true;
};
}, [googleMapsApiKey]);

if (!hasLoadedConfig) {
return <MapBox>Loading map configuration...</MapBox>;
}

if (!googleMapsApiKey) {
return (
<MapBox>
Google Maps is unavailable. Set `REACT_APP_GOOGLE_MAPS_API_KEY` in the
running server environment.
</MapBox>
);
}

return (
<LoadedMapEntryFormBox
form={props.form}
allForms={props.allForms}
googleMapsApiKey={googleMapsApiKey}
/>
);
}

function LoadedMapEntryFormBox(props: {
form: MapEntryForm;
allForms?: EntryForm[];
googleMapsApiKey: string;
}) {
const [lat, setLat] = useState(props.form.latitude);
const [lng, setLng] = useState(props.form.longitude);
Expand All @@ -329,8 +401,8 @@ function MapEntryFormBox(props: {
lng: props.form.longitude,
});

const { isLoaded } = useJsApiLoader({
googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY || '',
const { isLoaded, loadError } = useJsApiLoader({
googleMapsApiKey: props.googleMapsApiKey,
});

// Derive radii from form so map updates when Awarding/Close Distance fields change
Expand Down Expand Up @@ -390,6 +462,15 @@ function MapEntryFormBox(props: {
}
};

if (loadError) {
return (
<MapBox>
Unable to load Google Maps. Verify the API key, referrer restrictions,
and that the Maps JavaScript API is enabled.
</MapBox>
);
}

if (!isLoaded) return <MapBox>Loading map...</MapBox>;

return (
Expand Down
3 changes: 0 additions & 3 deletions heroku.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ setup:
addons:
- plan: heroku-postgresql
build:
config:
REACT_APP_GOOGLE_MAPS_API_KEY:
description: "Google Maps API key for admin dashboard"
docker:
web: Dockerfile
3 changes: 2 additions & 1 deletion server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { EventSyncModule } from './event-sync/event-sync.module';
import { ClubSubmissionModule } from './club-submission/club-submission.module';
import { FeedbackModule } from './feedback/feedback.module';
import { FeatureFlagsController } from './feature-flags/feature-flags.controller';
import { FrontendConfigController } from './frontend-config/frontend-config.controller';

@Module({
imports: [
Expand Down Expand Up @@ -54,7 +55,7 @@ import { FeatureFlagsController } from './feature-flags/feature-flags.controller
ClubSubmissionModule,
FeedbackModule,
],
controllers: [FeatureFlagsController],
controllers: [FeatureFlagsController, FrontendConfigController],
providers: [],
})
export class AppModule {}
11 changes: 11 additions & 0 deletions server/src/frontend-config/frontend-config.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Controller, Get } from '@nestjs/common';

@Controller('frontend-config')
export class FrontendConfigController {
@Get()
getConfig() {
return {
googleMapsApiKey: process.env.REACT_APP_GOOGLE_MAPS_API_KEY ?? '',
};
}
}
Loading