Skip to content

Commit

Permalink
build(nuxt-generate): add dynamic routes configuration and update to …
Browse files Browse the repository at this point in the history
…use asyncData fetching data
  • Loading branch information
josix committed Aug 11, 2022
1 parent 76af423 commit 503ff81
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 37 deletions.
60 changes: 59 additions & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,69 @@
import axios from 'axios'

const DEFAULT_BASE_URL = 'http://staging.pycon.tw/prs'
const DEFAULT_ROUTER_BASE = '/2022/'
const DEFAULT_BUILD_TARGET = 'static'

export default {
// Target (https://go.nuxtjs.dev/config-target)
target: process.env.BUILD_TARGET || DEFAULT_BUILD_TARGET,

generate: {
async routes() {
const config = {
headers: {
authorization: `Token ${process.env.AUTH_TOKEN}`,
},
}
const talks = await axios.get(
`${DEFAULT_BASE_URL}/api/events/speeches/?event_types=talk,sponsored`,
config,
)
const tutorials = await axios.get(
`${DEFAULT_BASE_URL}/api/events/speeches/?event_types=tutorial`,
config,
)
const getAllDetailTalks = async () => {
const data = await Promise.all(
talks.data.map(async (talk) => {
return await axios
.get(
`${DEFAULT_BASE_URL}/api/events/speeches/${talk.event_type}/${talk.id}/`,
config,
)
.then((response) => response.data)
}),
)
return data
}
const getAllDetailTutorials = async () => {
const data = await Promise.all(
tutorials.data.map(async (tutorial) => {
return await axios
.get(
`${DEFAULT_BASE_URL}/api/events/speeches/${tutorial.event_type}/${tutorial.id}/`,
config,
)
.then((response) => response.data)
}),
)
return data
}
const detailTalks = await getAllDetailTalks()
const detailTutorials = await getAllDetailTutorials()

const routes = [
...detailTalks.map((talk) => ({
route: `/conference/${talk.event_type}/${talk.id}`,
payload: talk,
})),
...detailTutorials.map((tutorial) => ({
route: `/conference/${tutorial.event_type}/${tutorial.id}`,
payload: tutorial,
})),
]
return routes
},
},
// Re-route for GitHub Pages to serve with /assets
router: {
base: process.env.ROUTER_BASE || DEFAULT_ROUTER_BASE,
Expand Down
22 changes: 13 additions & 9 deletions pages/conference/_eventType/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@
</template>

<script>
import { mapState } from 'vuex'
import i18n from '@/i18n/conference/speeches.i18n'
import I18nPageWrapper from '@/components/core/i18n/PageWrapper'
Expand Down Expand Up @@ -174,6 +172,19 @@ export default {
Youtube,
MarkdownRenderer,
},
async asyncData({ store, params, payload }) {
if (payload && Object.keys(payload).length !== 0) {
return {
speechData: payload,
}
}
await store.dispatch('$getSpeechData', {
eventType: params.eventType,
eventId: params.id,
})
const speechData = store.state.speechData
return { speechData }
},
data() {
return {
data: {
Expand All @@ -192,14 +203,7 @@ export default {
},
}
},
computed: {
...mapState(['speechData']),
},
async created() {
await this.$store.dispatch('$getSpeechData', {
eventType: this.$route.params.eventType,
eventId: this.$route.params.id,
})
await this.processData()
this.$root.$emit('initTabs')
},
Expand Down
18 changes: 9 additions & 9 deletions pages/conference/_eventType/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,19 @@ export default {
SpeechCardCollection,
Banner,
},
asyncData({ redirect, params }) {
async asyncData({ redirect, params, store }) {
const eventType = params.eventType
if (!['talks', 'tutorials'].includes(eventType)) {
redirect('/')
}
await store.dispatch('$getSpeechesData', eventType)
const speechesData = store.state.speechesData.map((speech) => ({
...speech,
begin_time: speech.begin_time ? new Date(speech.begin_time) : null,
}))
return {
speechesData,
}
},
data() {
return {
Expand Down Expand Up @@ -133,14 +141,6 @@ export default {
return false
},
},
async mounted() {
this.eventType = this.$route.params.eventType
await this.$store.dispatch('$getSpeechesData', this.eventType)
this.speechesData = this.$store.state.speechesData.map((speech) => ({
...speech,
begin_time: speech.begin_time ? new Date(speech.begin_time) : null,
}))
},
methods: {
metaInfo() {
return {
Expand Down
18 changes: 11 additions & 7 deletions pages/conference/keynotes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ export default {
GithubIcon,
TwitterIcon,
},
async asyncData({ store, app }) {
await store.dispatch('$getKeynotesData')
const keynotesData = store.state.keynotesData.map((keynote) => ({
...keynote,
id: app.$makeId(),
}))
return {
keynotesData,
}
},
data() {
return {
keynotesData: [],
Expand All @@ -150,13 +161,6 @@ export default {
}
},
},
async mounted() {
await this.$store.dispatch('$getKeynotesData')
this.keynotesData = this.$store.state.keynotesData.map((keynote) => ({
...keynote,
id: this.$makeId(),
}))
},
methods: {
getKeynoteId(keynote) {
return keynote.speaker.name_en_us.split(' ').join('_')
Expand Down
17 changes: 11 additions & 6 deletions pages/events/jobs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,24 @@ export default {
JobsCardCollection,
JobsPanel,
},
async asyncData({ store, app }) {
await store.dispatch('$getJobsData')
const jobsData = store.state.jobsData.map((sponsor) => ({
...sponsor,
id: app.$makeId(),
}))
return {
jobsData,
}
},
data() {
return {
selectedSponsor: {},
jobsData: [],
pivot: 0,
}
},
async mounted() {
await this.$store.dispatch('$getJobsData')
this.jobsData = this.$store.state.jobsData.map((sponsor) => ({
...sponsor,
id: this.$makeId(),
}))
mounted() {
this.setSelectedSponsor(this.jobsData[0])
this.setPivot()
},
Expand Down
12 changes: 7 additions & 5 deletions pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
</template>

<script>
import { mapState } from 'vuex'
import i18n from '@/i18n/index.i18n'
import I18nPageWrapper from '@/components/core/i18n/PageWrapper'
import TextButton from '~/components/core/buttons/TextButton'
Expand All @@ -109,6 +108,13 @@ export default {
I18nPageWrapper,
Intro,
},
async asyncData({ store }) {
await store.dispatch('$getSponsorsData')
const sponsorsData = store.state.sponsorsData
return {
sponsorsData,
}
},
data() {
return {
isOpened: false,
Expand All @@ -118,7 +124,6 @@ export default {
},
fetchOnServer: false,
computed: {
...mapState(['sponsorsData']),
isBulleted() {
if (process.client) {
const width = window.innerWidth
Expand All @@ -129,9 +134,6 @@ export default {
return true
},
},
created() {
this.$store.dispatch('$getSponsorsData')
},
methods: {
showModal(sponsor) {
this.isOpened = true
Expand Down

0 comments on commit 503ff81

Please sign in to comment.