This repository has been archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
TS-ify use-load-more and use TS and defineComponent in layouts and pages #1473
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { computed, ComputedRef } from '@nuxtjs/composition-api' | ||
|
||
import { useMediaStore } from '~/stores/media' | ||
|
||
/** | ||
* Fetches media on 'Load More' button click. | ||
*/ | ||
export const useLoadMore = (searchTerm: ComputedRef<string>) => { | ||
const canLoadMore = computed(() => searchTerm.value.trim() !== '') | ||
|
||
const onLoadMore = async () => { | ||
const mediaStore = useMediaStore() | ||
if (canLoadMore.value) { | ||
await mediaStore.fetchMedia({ | ||
shouldPersistMedia: true, | ||
}) | ||
} | ||
} | ||
|
||
return { canLoadMore, onLoadMore } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -72,8 +72,8 @@ export default defineComponent({ | |||||
focusFilters.focusFilterSidebar(event, Focus.Last) | ||||||
} | ||||||
} | ||||||
|
||||||
const { canLoadMore, onLoadMore } = useLoadMore(props) | ||||||
const searchTermRef = computed(() => props.searchTerm) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FWIW you can also use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! I finally understood what |
||||||
const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef) | ||||||
|
||||||
return { | ||||||
results, | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -25,7 +25,9 @@ export default defineComponent({ | |||||
useMeta({ title: `${props.searchTerm} | Openverse` }) | ||||||
|
||||||
const results = computed(() => props.resultItems.image) | ||||||
const { canLoadMore, onLoadMore } = useLoadMore(props) | ||||||
|
||||||
const searchTermRef = computed(() => props.searchTerm) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef) | ||||||
|
||||||
const focusFilters = useFocusFilters() | ||||||
const handleShiftTab = () => { | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,7 +3,7 @@ | |||||
</template> | ||||||
|
||||||
<script> | ||||||
import { useMeta, defineComponent } from '@nuxtjs/composition-api' | ||||||
import { computed, defineComponent, useMeta } from '@nuxtjs/composition-api' | ||||||
|
||||||
import { useLoadMore } from '~/composables/use-load-more' | ||||||
import { propTypes } from '~/pages/search/search-page.types' | ||||||
|
@@ -17,7 +17,9 @@ export default defineComponent({ | |||||
setup(props) { | ||||||
useMeta({ title: `${props.searchTerm} | Openverse` }) | ||||||
|
||||||
const { canLoadMore, onLoadMore } = useLoadMore(props) | ||||||
const searchTermRef = computed(() => props.searchTerm) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
const { canLoadMore, onLoadMore } = useLoadMore(searchTermRef) | ||||||
|
||||||
return { canLoadMore, onLoadMore } | ||||||
}, | ||||||
head: { | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we include this page and the others in the
tsconfig.json
file?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great question, @krysal !
If we include this page in
tsconfig
, then it will be typechecked. And if the types are incorrect, it will not build the app. And because we have some dependencies on Aria-kit that uses some React types internally, we cannot typecheck many of the pages/components that have popups or modals in them.When we only add
lang="ts"
, but do not add the files totsconfig
, we can use Typescript in the code, but type errors will not prevent the app from building. So, we can use the TS type annotations and declare types instead of using the JSDoc comments in these components. I feel it is easier to read the code when all components use the same type annotation style, you don't have to context-switch as much.