Skip to content

Commit 10a9332

Browse files
committed
feat: add dynamic routes configuration for generate
1 parent 1a7bd4c commit 10a9332

10 files changed

+169
-48
lines changed

nuxt.config.js

+60-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,68 @@
1-
const DEFAULT_BASE_URL = 'http://staging.pycon.tw/prs'
1+
import axios from 'axios'
2+
const DEFAULT_BASE_URL = 'https://tw.pycon.org/temp'
23
const DEFAULT_ROUTER_BASE = '/2022/'
34
const DEFAULT_BUILD_TARGET = 'static'
45
const DEFAULT_VUE_DEVTOOL = false
56

67
export default {
8+
generate: {
9+
async routes() {
10+
const config = {
11+
headers: {
12+
authorization: `Token ${process.env.AUTH_TOKEN}`,
13+
},
14+
}
15+
const talks = await axios.get(
16+
`${DEFAULT_BASE_URL}/api/events/speeches/?event_types=talk,sponsored`,
17+
config,
18+
)
19+
const tutorials = await axios.get(
20+
`${DEFAULT_BASE_URL}/api/events/speeches/?event_types=tutorial`,
21+
config,
22+
)
23+
const getAllDetailTalks = async () => {
24+
const data = await Promise.all(
25+
talks.data.map(async (talk) => {
26+
return await axios
27+
.get(
28+
`${DEFAULT_BASE_URL}/api/events/speeches/${talk.event_type}/${talk.id}/`,
29+
config,
30+
)
31+
.then((response) => response.data)
32+
}),
33+
)
34+
return data
35+
}
36+
const getAllDetailTutorials = async () => {
37+
const data = await Promise.all(
38+
tutorials.data.map(async (tutorial) => {
39+
return await axios
40+
.get(
41+
`${DEFAULT_BASE_URL}/api/events/speeches/${tutorial.event_type}/${tutorial.id}/`,
42+
config,
43+
)
44+
.then((response) => response.data)
45+
}),
46+
)
47+
return data
48+
}
49+
50+
const detailTalks = await getAllDetailTalks()
51+
const detailTutorials = await getAllDetailTutorials()
52+
53+
const routes = [
54+
...detailTalks.map((talk) => ({
55+
route: `/conference/${talk.event_type}/${talk.id}`,
56+
payload: talk,
57+
})),
58+
...detailTutorials.map((tutorial) => ({
59+
route: `/conference/${tutorial.event_type}/${tutorial.id}`,
60+
payload: tutorial,
61+
})),
62+
]
63+
return routes
64+
},
65+
},
766
vue: {
867
config: {
968
devtools: process.env.VUE_DEVTOOL || DEFAULT_VUE_DEVTOOL,

package-lock.json

+34-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@nuxt/http": "^0.6.2",
2121
"@nuxtjs/markdownit": "^2.0.0",
2222
"@tailwindcss/aspect-ratio": "^0.2.1",
23+
"axios": "^0.27.2",
2324
"core-js": "^3.6.5",
2425
"dayjs": "^1.10.6",
2526
"nuxt": "^2.15.3",

pages/conference/_eventType/_id.vue

+13-4
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ export default {
184184
MarkdownRenderer,
185185
RelatedCardCollection,
186186
},
187+
async asyncData({ store, params, payload }) {
188+
if (payload && Object.keys(payload).length !== 0) {
189+
return {
190+
speechData: payload,
191+
}
192+
}
193+
await store.dispatch('$getSpeechData', {
194+
eventType: params.eventType,
195+
eventId: params.id,
196+
})
197+
const speechData = store.state.speechData
198+
return { speechData }
199+
},
187200
data() {
188201
return {
189202
eventType: '',
@@ -213,10 +226,6 @@ export default {
213226
...mapState(['speechData']),
214227
},
215228
async created() {
216-
await this.$store.dispatch('$getSpeechData', {
217-
eventType: this.$route.params.eventType,
218-
eventId: this.$route.params.id,
219-
})
220229
await this.processData()
221230
this.$root.$emit('initTabs')
222231
await this.$store.dispatch('$getRelatedData', this.data.category)

pages/conference/_eventType/index.vue

+12-11
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,25 @@ export default {
7373
SpeechCardCollection,
7474
Banner,
7575
},
76-
asyncData({ redirect, params }) {
76+
async asyncData({ redirect, params, payload, store }) {
7777
const eventType = params.eventType
7878
if (!['talks', 'tutorials'].includes(eventType)) {
7979
redirect('/')
8080
}
81+
if (payload && Object.keys(payload).length !== 0)
82+
return { eventType, speechesData: payload }
83+
await store.dispatch('$getSpeechesData', eventType)
84+
const speechesData = store.state.speechesData.map((speech) => ({
85+
...speech,
86+
begin_time: speech.begin_time ? new Date(speech.begin_time) : null,
87+
}))
88+
return {
89+
eventType,
90+
speechesData,
91+
}
8192
},
8293
data() {
8394
return {
84-
eventType: '',
85-
speechesData: [],
8695
checkedCategories: [],
8796
aboutBanner: AboutBanner,
8897
}
@@ -133,14 +142,6 @@ export default {
133142
return false
134143
},
135144
},
136-
async mounted() {
137-
this.eventType = this.$route.params.eventType
138-
await this.$store.dispatch('$getSpeechesData', this.eventType)
139-
this.speechesData = this.$store.state.speechesData.map((speech) => ({
140-
...speech,
141-
begin_time: speech.begin_time ? new Date(speech.begin_time) : null,
142-
}))
143-
},
144145
methods: {
145146
metaInfo() {
146147
return {

pages/conference/keynotes.vue

+12-8
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,19 @@ export default {
141141
GithubIcon,
142142
TwitterIcon,
143143
},
144+
async asyncData({ store, app, payload }) {
145+
if (payload) return { keynotesData: payload }
146+
await store.dispatch('$getKeynotesData')
147+
const keynotesData = store.state.keynotesData.map((keynote) => ({
148+
...keynote,
149+
id: app.$makeId(),
150+
}))
151+
return {
152+
keynotesData,
153+
}
154+
},
144155
data() {
145156
return {
146-
keynotesData: [],
147157
aboutBanner: AboutBanner,
148158
}
149159
},
@@ -159,13 +169,7 @@ export default {
159169
}
160170
},
161171
},
162-
async mounted() {
163-
await this.$store.dispatch('$getKeynotesData')
164-
this.keynotesData = this.$store.state.keynotesData.map((keynote) => ({
165-
...keynote,
166-
id: this.$makeId(),
167-
}))
168-
},
172+
169173
methods: {
170174
getKeynoteId(keynote) {
171175
return keynote.speaker.name_en_us

pages/conference/schedule.vue

+9-2
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ export default {
101101
ScheduleEvent,
102102
ScheduleTick,
103103
},
104+
async asyncData({ store, payload }) {
105+
if (payload) return { schedulesData: payload }
106+
await store.dispatch('$getSchedulesData')
107+
const schedulesData = store.state.schedulesData
108+
return { schedulesData }
109+
},
104110
data() {
105111
return {
106112
selectedDayIndex: 0,
@@ -118,6 +124,7 @@ export default {
118124
},
119125
}
120126
},
127+
121128
computed: {
122129
...mapState(['schedulesData']),
123130
table() {
@@ -132,8 +139,8 @@ export default {
132139
this.makeRooms()
133140
},
134141
},
135-
async created() {
136-
await this.$store.dispatch('$getSchedulesData')
142+
143+
created() {
137144
this.processData()
138145
},
139146
activated() {

pages/events/jobs-gather.vue

+10-7
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,22 @@ export default {
4545
JobsCardCollection,
4646
JobsPanel,
4747
},
48+
async asyncData({ store, app, payload }) {
49+
if (payload) return { jobsData: payload }
50+
await store.dispatch('$getJobsData')
51+
const jobsData = store.state.jobsData.map((sponsor) => ({
52+
...sponsor,
53+
id: app.$makeId(),
54+
}))
55+
return { jobsData }
56+
},
4857
data() {
4958
return {
5059
selectedSponsor: {},
51-
jobsData: [],
5260
pivot: 0,
5361
}
5462
},
55-
async mounted() {
56-
await this.$store.dispatch('$getJobsData')
57-
this.jobsData = this.$store.state.jobsData.map((sponsor) => ({
58-
...sponsor,
59-
id: this.$makeId(),
60-
}))
63+
mounted() {
6164
this.setSelectedSponsor(this.jobsData[0])
6265
this.setPivot()
6366
},

pages/events/jobs.vue

+10-7
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@ export default {
3939
JobsCardCollection,
4040
JobsPanel,
4141
},
42+
async asyncData({ store, app, payload }) {
43+
if (payload) return { jobsData: payload }
44+
await store.dispatch('$getJobsData')
45+
const jobsData = store.state.jobsData.map((sponsor) => ({
46+
...sponsor,
47+
id: app.$makeId(),
48+
}))
49+
return { jobsData }
50+
},
4251
data() {
4352
return {
4453
selectedSponsor: {},
45-
jobsData: [],
4654
pivot: 0,
4755
}
4856
},
49-
async mounted() {
50-
await this.$store.dispatch('$getJobsData')
51-
this.jobsData = this.$store.state.jobsData.map((sponsor) => ({
52-
...sponsor,
53-
id: this.$makeId(),
54-
}))
57+
mounted() {
5558
this.setSelectedSponsor(this.jobsData[0])
5659
this.setPivot()
5760
},

pages/index.vue

+8-3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ export default {
120120
I18nPageWrapper,
121121
Intro,
122122
},
123+
async asyncData({ store, payload }) {
124+
if (payload) return { sponsorsData: payload }
125+
await store.dispatch('$getSponsorsData')
126+
const sponsorsData = store.state.sponsorsData
127+
return {
128+
sponsorsData,
129+
}
130+
},
123131
data() {
124132
return {
125133
isOpened: false,
@@ -140,9 +148,6 @@ export default {
140148
return true
141149
},
142150
},
143-
created() {
144-
this.$store.dispatch('$getSponsorsData')
145-
},
146151
methods: {
147152
showModal(sponsor) {
148153
this.isOpened = true

0 commit comments

Comments
 (0)