@@ -153,7 +154,7 @@
class="hidden grid-cols-1 xl:grid"
>
@@ -211,6 +212,17 @@ watch(() => props.highlightedSpecies, () => {
fillExistingSpeciesSlug()
})
+const speciesList = ref
([])
+const isLoadingSpecies = ref(false)
+const hasFetchedAll = ref(false)
+const LIMIT = 100
+
+watch(() => props.toggleShowModal, async () => {
+ fillExistingSpeciesSlug()
+ speciesList.value = []
+ await fetchProjectsSpecies(LIMIT, 0)
+})
+
const store = useStore()
const apiClientBio = inject(apiClientKey) as AxiosInstance
@@ -219,26 +231,14 @@ const selectedProjectId = computed(() => store.selectedProject?.id)
const searchKeyword = ref('')
const searchRisk = ref('')
const showHaveReachedLimit = ref(false)
-const isLoadingSpecies = ref(false)
-const hasFetchedAll = ref(false)
-const selectedSpecies = ref([])
+const selectedSpeciesSlug = ref([])
const PAGE_SIZE = 10
const currentPage = ref(1)
-const totalProjectsSpecies = ref(0)
-
-const speciesList = ref([])
-watch(() => props.toggleShowModal, async () => {
- fillExistingSpeciesSlug()
- speciesList.value = []
- await fetchProjectsSpecies(PAGE_SIZE, 0)
-})
-const handleCurrentChange = async (page: number) => {
- speciesList.value = []
- await fetchProjectsSpecies(PAGE_SIZE, (page - 1) * PAGE_SIZE)
-}
+const { isPending: isLoadingPostSpecies, mutate: mutatePostSpecies } = usePostSpeciesHighlighted(apiClientBio, selectedProjectId)
+const { isPending: isLoadingDeleteSpecies, mutate: mutateDeleteSpecie } = useDeleteSpecieHighlighted(apiClientBio, selectedProjectId)
const fetchProjectsSpecies = async (limit: number, offset: number) => {
isLoadingSpecies.value = true
@@ -253,9 +253,7 @@ const fetchProjectsSpecies = async (limit: number, offset: number) => {
}
const s = projectSpecies as ProjectSpeciesResponse
- hasFetchedAll.value = s.species.length < limit // check if reaching the end
- totalProjectsSpecies.value = s.total
-
+ hasFetchedAll.value = s.species.length < LIMIT // check if reaching the end
s.species.forEach(sp => {
const { slug, taxonSlug, scientificName, commonName, photoUrl, riskId } = sp as DashboardSpecies
speciesList.value.push({
@@ -268,13 +266,15 @@ const fetchProjectsSpecies = async (limit: number, offset: number) => {
})
})
isLoadingSpecies.value = false
-}
-const { isPending: isLoadingPostSpecies, mutate: mutatePostSpecies } = usePostSpeciesHighlighted(apiClientBio, selectedProjectId)
-const { isPending: isLoadingDeleteSpecies, mutate: mutateDeleteSpecie } = useDeleteSpecieHighlighted(apiClientBio, selectedProjectId)
+ if (!hasFetchedAll.value) {
+ await fetchProjectsSpecies(LIMIT, speciesList.value.length)
+ }
+}
// Filtered list of species by search, risk or both
const speciesListFiltered = computed(() => {
+ if (!hasFetchedAll.value) return []
if (!searchKeyword.value && searchRisk.value) {
resetPagination()
return speciesList.value
@@ -304,11 +304,16 @@ const speciesListFiltered = computed(() => {
})
const speciesLength = computed(() => {
- return totalProjectsSpecies.value !== 0 ? totalProjectsSpecies.value : speciesListFiltered.value.length
+ return speciesListFiltered.value.length
})
const speciesForCurrentPage = computed(() => {
- return speciesListFiltered.value
+ return speciesListFiltered.value.slice((currentPage.value - 1) * PAGE_SIZE, currentPage.value * PAGE_SIZE)
+})
+
+const preSelectedSpecies = computed(() => {
+ if (!hasFetchedAll.value) return []
+ return speciesList.value.length ? selectedSpeciesSlug.value.map((slug) => speciesList.value.filter((specie) => specie.slug === slug)[0]) ?? [] : []
})
const existingRiskCode = computed(() => {
@@ -317,11 +322,11 @@ const existingRiskCode = computed(() => {
const newSpeciesToAdd = computed(() => {
const existingSlugsInDB = props.highlightedSpecies.map(sp => sp.slug)
- return selectedSpecies.value.filter(sp => !existingSlugsInDB.includes(sp.slug))
+ return preSelectedSpecies.value.filter(sp => !existingSlugsInDB.includes(sp.slug))
})
const speciesToRemove = computed(() => {
- const preSelectedSpeciesSlug = selectedSpecies.value.map(sp => sp.slug)
+ const preSelectedSpeciesSlug = preSelectedSpecies.value.map(sp => sp.slug)
return props.highlightedSpecies.filter(sp => !preSelectedSpeciesSlug.includes(sp.slug))
})
@@ -335,18 +340,18 @@ const resetSearch = (): void => {
}
const findIndexToRemove = (slug: string): void => {
- const index = selectedSpecies.value.findIndex(sl => sl.slug === slug)
- selectedSpecies.value.splice(index, 1)
+ const index = selectedSpeciesSlug.value.findIndex(sl => sl === slug)
+ selectedSpeciesSlug.value.splice(index, 1)
}
const selectSpecie = async (specie: HighlightedSpeciesRow): Promise => {
if (isSpecieSelected(specie)) {
findIndexToRemove(specie.slug)
- showHaveReachedLimit.value = selectedSpecies.value.length >= 5
+ showHaveReachedLimit.value = selectedSpeciesSlug.value.length >= 5
} else {
// only 5 species might be highlighted
- if (selectedSpecies.value.length < 5) {
- selectedSpecies.value.push(specie)
+ if (selectedSpeciesSlug.value.length < 5) {
+ selectedSpeciesSlug.value.push(specie.slug)
} else {
showHaveReachedLimit.value = true
}
@@ -354,7 +359,7 @@ const selectSpecie = async (specie: HighlightedSpeciesRow): Promise => {
}
const isSpecieSelected = (specie: HighlightedSpeciesRow): boolean => {
- return selectedSpecies.value.find(sp => sp.slug === specie.slug) !== undefined
+ return selectedSpeciesSlug.value.find(slug => slug === specie.slug) !== undefined
}
const removeSpecieFromList = async (specie: SpecieRow): Promise => {
@@ -363,7 +368,9 @@ const removeSpecieFromList = async (specie: SpecieRow): Promise => {
}
const fillExistingSpeciesSlug = (): void => {
- selectedSpecies.value = props.highlightedSpecies.length ? props.highlightedSpecies : []
+ if (props.highlightedSpecies.length) {
+ selectedSpeciesSlug.value = props.highlightedSpecies.map(sp => sp.slug)
+ } else selectedSpeciesSlug.value = []
}
const filterByCode = (code: string): void => {