Skip to content

Commit 26c5229

Browse files
committed
fix(species details): use fetch rather than async data
1 parent 2de6d83 commit 26c5229

File tree

1 file changed

+60
-50
lines changed

1 file changed

+60
-50
lines changed

pages/species/_id.vue

+60-50
Original file line numberDiff line numberDiff line change
@@ -225,47 +225,48 @@
225225
import { mdiArrowRight } from '@mdi/js'
226226
import { startCase } from 'lodash-es'
227227
export default {
228-
async asyncData(context) {
229-
try {
230-
const id = context.route.params.id
231-
const result = await context.$content('species').fetch()
232-
const { body } = result
233-
const item = body.find((item) => item.species === id) ?? {}
234-
235-
const { body: waterwiseBody = [] } = await context
236-
.$content('waterwise-plants')
237-
.fetch()
238-
const waterwise = waterwiseBody.find((i) => {
239-
const commonName = startCase(item.species.split('(')[0].trim())
240-
const botanicalName = item.species
241-
.split('(')[1]
242-
.replace(/seasonal$/, '')
243-
.replace(/-/, '')
244-
.trim()
245-
.replace(/\)+$/, '')
246-
.trim()
247-
return (
248-
i['Common Name'] === commonName ||
249-
i['Botanical Name'] === botanicalName ||
250-
i['Previous Name'] === botanicalName ||
251-
i['Botanical Name'].startsWith(
252-
botanicalName.replace(/sp\.$/, '').trim()
253-
)
254-
)
255-
})
256-
if (!item && !item.length) {
257-
context.error({ statusCode: 404 })
258-
}
259-
return { item, waterwise }
260-
} catch {
261-
context.error({ statusCode: 404 })
262-
}
263-
},
264228
data: () => ({
265229
item: {},
266230
waterwise: {},
267231
mdiArrowRight,
268232
}),
233+
async fetch() {
234+
const id = this.$route.params.id
235+
const result = await this.$content('species').fetch()
236+
const { body } = result
237+
const item = body.find((item) => item.species === id) ?? {}
238+
239+
const { body: waterwiseBody = [] } = await this.$content(
240+
'waterwise-plants'
241+
).fetch()
242+
const waterwise = waterwiseBody.find((i) => {
243+
const commonName = startCase(item.species.split('(')[0].trim())
244+
const botanicalName = item.species
245+
.split('(')[1]
246+
.replace(/seasonal$/, '')
247+
.replace(/-/, '')
248+
.trim()
249+
.replace(/\)+$/, '')
250+
.trim()
251+
return (
252+
i['Common Name'] === commonName ||
253+
i['Botanical Name'] === botanicalName ||
254+
i['Previous Name'] === botanicalName ||
255+
i['Botanical Name'].startsWith(
256+
botanicalName.replace(/sp\.$/, '').trim()
257+
)
258+
)
259+
})
260+
if (!item && !item.length) {
261+
if (process.server) {
262+
this.$nuxt.context.res.statusCode = 404
263+
} else {
264+
this.$nuxt.error({ statusCode: 404 })
265+
}
266+
}
267+
this.item = item
268+
this.waterwise = waterwise
269+
},
269270
head() {
270271
return {
271272
title: this.item.species,
@@ -281,48 +282,57 @@ export default {
281282
computed: {
282283
attracts() {
283284
const separator = ','
284-
const data = this.item.attracts || separator
285+
const data = this.item.attracts
285286
return this.split(data, separator).sort()
286287
},
287288
soilType() {
288289
const separator = ','
289-
const data = this.waterwise['Soil Type'] || separator
290+
const data = this.waterwise['Soil Type']
290291
return this.split(data, separator).sort()
291292
},
292293
climateZones() {
293294
const separator = ','
294-
const data = this.waterwise['Climate Zones'] || separator
295+
const data = this.waterwise['Climate Zones']
295296
return this.split(data, separator).sort()
296297
},
297298
foliageColor() {
298299
const separator = ' and '
299-
const data = this.waterwise['Foliage Colour'] || separator
300+
const data = this.waterwise['Foliage Colour']
300301
return this.split(data, separator).sort()
301302
},
302303
flowerColor() {
303304
const separator = ' and '
304-
const data = this.waterwise['Flower colour'] || separator
305+
const data = this.waterwise['Flower colour']
305306
return this.split(data, separator).sort()
306307
},
307308
commonName() {
308309
const separator = '('
309-
const text = this.item.species || separator
310-
const splits = text.split(separator)
311-
const split = splits[0]
312-
return startCase(split.trim())
310+
const text = this.item.species
311+
const splits = this.split(text, separator)
312+
if (splits && splits.length) {
313+
const split = splits[0]
314+
return startCase(split.trim())
315+
}
316+
return null
313317
},
314318
botanicalName() {
315319
const separator = '('
316-
const text = this.item.species || separator
317-
const splits = text.split(separator)
318-
const split = splits[1]
319-
return split.replace(/\)+$/, '').trim()
320+
const text = this.item.species
321+
const splits = this.split(text, separator)
322+
if (splits && splits.length) {
323+
const split = splits[1]
324+
return split.replace(/\)+$/, '').trim()
325+
}
326+
return null
320327
},
321328
},
322329
methods: {
323330
startCase,
324331
split(text, separator = ',') {
325-
return text.split(separator)
332+
if (text) {
333+
return text.split(separator)
334+
}
335+
return []
326336
},
327337
},
328338
}

0 commit comments

Comments
 (0)