Skip to content

Commit

Permalink
Get user location
Browse files Browse the repository at this point in the history
  • Loading branch information
exu3 committed Nov 6, 2021
1 parent 1b9c642 commit dd95963
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 6 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@hackclub/theme": "^0.3.3",
"@mdx-js/loader": "^1.6.22",
"@next/mdx": "^10.0.4",
"geoip-country": "^4.0.93",
"lodash": "^4.17.21",
"next": "^12.0.2",
"next-compose-plugins": "^2.2.1",
Expand Down
16 changes: 16 additions & 0 deletions pages/api/country.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// hackathons.hackclub.com/api/country gets the country of the user in the format
// { range: [ 28966912, 29097983 ], country: 'IN' }
// If the country is "IN", they will see the banner on the homepage
export default function handler(req, res) {
try {
const geoip = require('geoip-country')
const ip = req.headers['x-forwarded-for']
? req.headers['x-forwarded-for']
: '1.187.255.255' // defaults to IP in APAC
console.log(geoip.lookup(ip))
console.log('IP is ', geoip.pretty(ip))
res.json(geoip.lookup(ip))
} catch (e) {
res.json({ countryNotFound: true })
}
}
20 changes: 16 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import Meta from '@hackclub/meta'
import Signup from '../components/signup'
import Years from '../components/years'
import Regions from '../components/regions'
import { filter, orderBy, slice, last, remove } from 'lodash'
import { filter, orderBy, slice, last } from 'lodash'
import { timeSince, humanizedDateRange } from '../lib/util'
import { getGroupingData } from '../lib/data'
import Banner from '../components/banner'
import { sendJson } from 'next/dist/server/api-utils'

const title = `High School Hackathons in ${new Date().getFullYear()}`
const eventsPreview = events =>
Expand Down Expand Up @@ -53,6 +55,13 @@ export default ({ stats, emailStats, events }) => (
</Link>{' '}
staff.
</Text>
<Banner
copy="Looking for hackathons in the APAC (Asia-Pacific) region?"
caption="They've been moved to a new page!"
href="/apac"
iconRight="enter"
color="primary"
/>
</>
}
events={events}
Expand All @@ -74,6 +83,9 @@ export default ({ stats, emailStats, events }) => (

export const getStaticProps = async () => {
let { events, emailStats } = await getGroupingData()
const location = await fetch('https://localhost:3000/api/country')
const userLocation = JSON.stringify(location)
console.log(userLocation)
let stats = {
total: events.length,
state: new Set(
Expand All @@ -94,7 +106,7 @@ export const getStaticProps = async () => {
filter(events, e => new Date(e.start) >= new Date()),
'start'
)
// Filter out apac hackathons. Moved to /apac as of 2021-11-05
events = filter(events, 'country')
return { props: { events, stats, emailStats }, revalidate: 1 }
// APAC Hackathons are now on the /apac page
events = filter(events, ['apac', false])
return { props: { events, stats, emailStats, userLocation }, revalidate: 1 }
}
Loading

0 comments on commit dd95963

Please sign in to comment.