Skip to content

Commit

Permalink
Refactor FetchState to use discriminated union
Browse files Browse the repository at this point in the history
  • Loading branch information
obulat committed Jan 10, 2025
1 parent 43ec351 commit ba60e0a
Show file tree
Hide file tree
Showing 21 changed files with 409 additions and 461 deletions.
9 changes: 3 additions & 6 deletions frontend/shared/types/fetch-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ export interface FetchingError {
details?: Record<string, string>
}

export interface FetchState {
isFetching: boolean
hasStarted?: boolean
isFinished?: boolean
fetchingError: FetchingError | null
}
export type FetchState =
| { status: "idle" | "fetching" | "success"; error: null }
| { status: "error"; error: FetchingError }
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const showCollectionExternalLink = computed(() => {
const { getI18nCollectionResultCountLabel } = useI18nResultsCount()
const resultsLabel = computed(() => {
if (mediaStore.resultCount === 0 && mediaStore.fetchState.isFetching) {
if (mediaStore.resultCount === 0 && mediaStore.isFetching) {
return ""
}
const resultsCount = mediaStore.results[props.mediaType].count
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/VHeader/VHeaderDesktop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const uiStore = useUiStore()
const isSidebarVisible = inject<Ref<boolean>>(IsSidebarVisibleKey)
const isFetching = computed(() => mediaStore.fetchState.isFetching)
const isFetching = computed(() => mediaStore.isFetching)
const { $sendCustomEvent } = useNuxtApp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const isInputFocused = ref(false)
const contentSettingsOpen = ref(false)
const appliedFilterCount = computed(() => searchStore.appliedFilterCount)
const isFetching = computed(() => mediaStore.fetchState.isFetching)
const isFetching = computed(() => mediaStore.isFetching)
/**
* The selection range of the input field. Used to make sure that the cursor
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/VMediaInfo/VRelatedMedia.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ watch(
{ immediate: true }
)
const isFetching = computed(() => relatedMediaStore.fetchState.isFetching)
const isFetching = computed(() => relatedMediaStore.isFetching)
const showRelated = computed(
() => results.value.items.length > 0 || isFetching.value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const Template: Story = {
components: { VLoadMore },
setup() {
const mediaStore = useMediaStore()
mediaStore._startFetching("image")
mediaStore.results.image.count = 1
mediaStore.results.image.page = 1
mediaStore.results.image.pageCount = 12
return () =>
h("div", { class: "flex p-4", id: "wrapper" }, [
h(VLoadMore, {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/composables/use-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useCollection = <T extends SupportedMediaType>({
const searchStore = useSearchStore()

const collectionParams = computed(() => searchStore.collectionParams)
const isFetching = computed(() => mediaStore.fetchState.isFetching)
const isFetching = computed(() => mediaStore.isFetching)

const media = ref(mediaStore.resultItems[mediaType]) as Ref<ResultType[]>
const creatorUrl = ref<string>()
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/composables/use-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const useSearch = (
return navigateTo(searchPath)
}

const isFetching = computed(() => mediaStore.fetchState.isFetching)
const isFetching = computed(() => mediaStore.isFetching)
const resultsCount = computed(() => mediaStore.resultCount)

const { getI18nCount, getLoading } = useI18nResultsCount()
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/middleware/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export const searchMiddleware = defineNuxtRouteMiddleware(async (to) => {
const mediaStore = useMediaStore()
const results = await mediaStore.fetchMedia()

const fetchingError = mediaStore.fetchState.fetchingError
const fetchingError = mediaStore.fetchState.error
if (!results.length && fetchingError && !handledClientSide(fetchingError)) {
showError(createError(fetchingError))
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/middleware/single-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default defineNuxtRouteMiddleware(async (to, from) => {
relatedMediaStore.fetchMedia(mediaType, mediaId),
])

const fetchingError = singleResultStore.fetchState.fetchingError
const fetchingError = singleResultStore.fetchState.error

if (
!singleResultStore.mediaItem &&
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/audio/[id]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const audio = ref<AudioDetail | null>(
? singleResultStore.audio
: null
)
const fetchingError = computed(() => singleResultStore.fetchState.fetchingError)
const fetchingError = computed(() => singleResultStore.fetchState.error)
const { isHidden, reveal } = useSensitiveMedia(audio.value)
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/image/[id]/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const image = ref<ImageDetail | null>(
? singleResultStore.image
: null
)
const fetchingError = computed(() => singleResultStore.fetchState.fetchingError)
const fetchingError = computed(() => singleResultStore.fetchState.error)
const isLoadingOnClient = computed(
() => !(import.meta.server || nuxtApp.isHydrating)
)
Expand Down
10 changes: 3 additions & 7 deletions frontend/src/pages/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const {
apiSearchQueryParams: query,
} = storeToRefs(searchStore)
const { fetchState } = storeToRefs(mediaStore)
const fetchingError = computed(() => mediaStore.fetchState.error)
const isFetching = computed(() => mediaStore.isFetching)
const pageTitle = ref(`${searchTerm.value} | Openverse`)
watch(searchTerm, () => {
Expand Down Expand Up @@ -81,9 +82,7 @@ const fetchMedia = async (payload: { shouldPersistMedia?: boolean } = {}) => {
* and there is an error status that will not change if retried, don't re-fetch.
*/
const shouldNotRefetch =
fetchState.value.hasStarted &&
fetchingError.value !== null &&
!isRetriable(fetchingError.value)
fetchingError.value !== null && !isRetriable(fetchingError.value)
if (shouldNotRefetch) {
return
}
Expand All @@ -100,9 +99,6 @@ const fetchMedia = async (payload: { shouldPersistMedia?: boolean } = {}) => {
return fetchingError.value
}
const fetchingError = computed(() => fetchState.value.fetchingError)
const isFetching = computed(() => fetchState.value.isFetching)
/**
* This watcher fires even when the queries are equal. We update the path only
* when the queries change.
Expand Down
Loading

0 comments on commit ba60e0a

Please sign in to comment.