Skip to content

Commit

Permalink
Cancel previous request. Fix #123
Browse files Browse the repository at this point in the history
  • Loading branch information
jlpereira committed Aug 20, 2023
1 parent ddc3ead commit a54ebc8
Show file tree
Hide file tree
Showing 18 changed files with 245 additions and 151 deletions.
36 changes: 20 additions & 16 deletions src/modules/otus/components/Panel/PanelContent/PanelContent.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</template>

<script setup>
import { computed, ref, watch } from 'vue'
import { computed, ref, onBeforeMount, onBeforeUnmount } from 'vue'
import { useOtuPageRequest } from '@/modules/otus/helpers/useOtuPageRequest'
import TaxonWorks from '../../../services/TaxonWorks'
import ContentTopic from './PanelContentTopic.vue'
Expand All @@ -23,6 +23,7 @@ const props = defineProps({
})
const contents = ref([])
const controller = new AbortController()
const contentList = computed(() =>
contents.value.reduce((acc, current) => {
Expand All @@ -36,19 +37,22 @@ const contentList = computed(() =>
}, {})
)
watch(
() => props.otuId,
(id) => {
if (id) {
useOtuPageRequest('panel:content', () =>
TaxonWorks.getOtuContent(id)
).then(({ data }) => {
contents.value = data
})
} else {
contents.value = []
}
},
{ immediate: true }
)
onBeforeMount(() => {
useOtuPageRequest('panel:content', () =>
TaxonWorks.getOtuContent(props.otuId, {
params: {
extend: ['depiction']
},
signal: controller.signal
})
)
.then(({ data }) => {
contents.value = data
})
.catch((e) => {})
})
onBeforeUnmount(() => {
controller.abort()
})
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ watch(
isLoading.value = true
useOtuPageRequest('panel:descendants', () =>
TaxonWorks.getTaxonomy(props.otuId, { max_descendants_depth: 1 })
TaxonWorks.getTaxonomy(props.otuId, {
params: { max_descendants_depth: 1 }
})
)
.then(({ data }) => {
taxonomy.value = data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ const loadDescendants = () => {
return
}
TaxonWorks.getTaxonomy(props.taxonomy.otu_id, {
max_descendants_depth: 1
params: {
max_descendants_depth: 1
}
}).then(({ data }) => {
descendants.value = data.descendants
})
Expand Down
11 changes: 8 additions & 3 deletions src/modules/otus/components/Panel/PanelGallery/Gallery.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</template>

<script setup>
import { computed, onServerPrefetch, onMounted } from 'vue'
import { useOtuStore } from '@/modules/otus/store/store'
import { computed, onServerPrefetch, onMounted, onBeforeUnmount } from 'vue'
import { useImageStore } from '../../../store/useImageStore'
const props = defineProps({
otuId: {
Expand All @@ -17,7 +17,7 @@ const props = defineProps({
}
})
const store = useOtuStore()
const store = useImageStore()
const images = computed(() => store.images || [])
onServerPrefetch(async () => {
Expand All @@ -29,4 +29,9 @@ onMounted(() => {
store.loadImages(props.otuId)
}
})
onBeforeUnmount(() => {
store.resetRequest()
store.$reset()
})
</script>
11 changes: 8 additions & 3 deletions src/modules/otus/components/Panel/PanelMap/PanelMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useOtuStore } from '@/modules/otus/store/store'
import { ref, onMounted, onBeforeUnmount } from 'vue'
import { useDistributionStore } from '@/modules/otus/store/useDistributionStore.js'
import CachedMap from './CachedMap.vue'
import OtuSearch from '../../Search/OtuSearch.vue'
Expand All @@ -83,7 +83,7 @@ const props = defineProps({
const zoom = 2
const isLoading = ref(true)
const isOtuSearchVisible = ref(false)
const store = useOtuStore()
const store = useDistributionStore()
const LEGEND = {
AssertedDistribution: {
Expand Down Expand Up @@ -116,4 +116,9 @@ onMounted(() => {
rankString: props.taxon.rank_string
})
})
onBeforeUnmount(() => {
store.resetRequest()
store.$reset()
})
</script>
3 changes: 2 additions & 1 deletion src/modules/otus/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './typeOrder'
export * from './rankGroups'
export * from './responseError'
export * from './typeOrder'
2 changes: 1 addition & 1 deletion src/modules/otus/constants/overviewLayout.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FAMILY_GROUP, GENUS_GROUP, SPECIES_GROUP } from './index.js'
import { FAMILY_GROUP, GENUS_GROUP, SPECIES_GROUP } from './rankGroups.js'
import PanelGallery from '../components/Panel/PanelGallery/Gallery.vue'
import PanelTypeSpecimen from '../components/Panel/PanelTypeSpecimen/PanelTypeSpecimen.vue'
import PanelTypeDesignation from '../components/Panel/PanelTypeDesignation/PanelTypeDesignation.vue'
Expand Down
3 changes: 3 additions & 0 deletions src/modules/otus/constants/responseError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const RESPONSE_ERROR = {
CanceledError: 'CanceledError'
}
5 changes: 4 additions & 1 deletion src/modules/otus/helpers/useOtuPageRequest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useOtuPageRequestStore } from '../store/request'
import { RESPONSE_ERROR } from '../constants'

export function useOtuPageRequest(key, requestFunction) {
const store = useOtuPageRequestStore()
Expand All @@ -9,7 +10,9 @@ export function useOtuPageRequest(key, requestFunction) {
store.setRequest(key, response)
})
.catch((error) => {
store.setRequest(key, error.response)
if (error.name !== RESPONSE_ERROR.CanceledError) {
store.setRequest(key, error.response)
}
})

return request
Expand Down
38 changes: 16 additions & 22 deletions src/modules/otus/services/TaxonWorks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { makeAPIRequest } from '@/utils/request'

export default class TaxonWorks {
static getTaxonNameCitations(taxonId) {
return makeAPIRequest.get(`/taxon_names/${taxonId}/inventory/catalog`)
static getTaxonNameCitations(taxonId, opt) {
return makeAPIRequest.get(`/taxon_names/${taxonId}/inventory/catalog`, opt)
}

static getOtu(id) {
Expand All @@ -17,12 +17,12 @@ export default class TaxonWorks {
})
}

static getTaxon(id) {
return makeAPIRequest.get(`/taxon_names/${id}`)
static getTaxon(id, opt) {
return makeAPIRequest.get(`/taxon_names/${id}`, opt)
}

static summary(id) {
return makeAPIRequest.get(`/taxon_names/${id}/inventory/summary`)
static summary(id, opt) {
return makeAPIRequest.get(`/taxon_names/${id}/inventory/summary`, opt)
}

static getTaxonTypeDesignation(id) {
Expand All @@ -31,38 +31,32 @@ export default class TaxonWorks {
})
}

static getOtuImages(otuId, params = {}) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/images.json`, {
params
})
static getOtuImages(otuId, opt) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/images.json`, opt)
}

static getTaxonomy(otuId, params) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/taxonomy.json`, {
params
})
static getTaxonomy(otuId, opt) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/taxonomy.json`, opt)
}

static getOtuTypeMaterial(otuId) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/type_material.json`)
}

static getOtuDistribution(otuId) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/distribution.json`)
static getOtuDistribution(otuId, opt = {}) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/distribution.json`, opt)
}

static getOtuGeoJSONDistribution(otuId) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/distribution.geojson`)
}

static getCachedMap(cachedId) {
return makeAPIRequest.get(`/cached_maps/${cachedId}`)
static getCachedMap(cachedId, opt) {
return makeAPIRequest.get(`/cached_maps/${cachedId}`, opt)
}

static getOtuContent(otuId) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/content`, {
extend: ['depiction']
})
static getOtuContent(otuId, opt) {
return makeAPIRequest.get(`/otus/${otuId}/inventory/content`, opt)
}

static getCachedMap(id) {
Expand Down
1 change: 0 additions & 1 deletion src/modules/otus/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './loadCatalog'
export * from './loadDistribution'
export * from './loadTaxonomy'
export * from './loadCachedMap'
4 changes: 2 additions & 2 deletions src/modules/otus/store/actions/loadCatalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import TaxonWorks from '../../services/TaxonWorks'
import { useOtuPageRequest } from '../../helpers/useOtuPageRequest'

export const actionLoadCatalog = {
async loadCatalog(taxonId) {
async loadCatalog(taxonId, { signal }) {
this.catalog.isLoading = true

const response = await useOtuPageRequest('taxonomy', () =>
TaxonWorks.getTaxonNameCitations(taxonId)
TaxonWorks.getTaxonNameCitations(taxonId, { signal })
)

this.catalog = {
Expand Down
56 changes: 0 additions & 56 deletions src/modules/otus/store/actions/loadDistribution.js

This file was deleted.

9 changes: 6 additions & 3 deletions src/modules/otus/store/actions/loadTaxonomy.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import TaxonWorks from '../../services/TaxonWorks'

export const actionLoadTaxonomy = {
async loadTaxonomy(otuId) {
async loadTaxonomy(otuId, { signal }) {
const { data } = await TaxonWorks.getTaxonomy(otuId, {
max_descendants_depth: 0,
extend: ['common_names']
params: {
max_descendants_depth: 0,
extend: ['common_names']
},
signal
})

this.taxonomy = {
Expand Down
Loading

0 comments on commit a54ebc8

Please sign in to comment.