Skip to content

Commit

Permalink
Remove moreKey, dataKey and countKey
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVelezLl committed Dec 17, 2024
1 parent 6471f0b commit 8bfd6cc
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 77 deletions.
78 changes: 7 additions & 71 deletions kolibri/plugins/coach/assets/src/composables/useFetch.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import get from 'lodash/get';
import { ref, computed } from 'vue';

/**
Expand All @@ -15,59 +14,6 @@ import { ref, computed } from 'vue';
* })
*
*
* @param {string} [options.dataKey] The key in the response object where the main data is located.
* * You can use `lodash.get`-compatible keys to access nested objects (e.g., "data.user.details").
* * If `dataKey` is left `undefined`, the entire response object will be treated as the main data.
*
* Example:
* ```js
* // Suppose the response object looks like this:
* const response = {
* status: "success",
* payload: { id: 42, name: "Jane Doe" }
* };
*
* // By specifying `dataKey`, you tell the composable where to find the main data:
* const { data } = useFetch({ dataKey: "payload" });
* console.log(data); // Outputs: { id: 42, name: "Jane Doe" }
* ```
*
*
* @param {string} [options.moreKey] The key in the response object where the
* "more" object is located.
* * This is the object that will be passed as the first argument to the `fetchMoreMethod` param.
* * You can use `lodash.get`-compatible keys to access nested objects (e.g., "data.user.details").
*
* Example:
* ```js
* // Suppose the response object looks like this:
* const response = {
* results: [...],
* more: { page: 2 }
* };
*
* // By specifying `moreKey`, you tell the composable where to find the more object
* // to fetch additional data:
* useFetch({ moreKey: "more" });
* ```
*
* @param {string} [options.countKey] The key in the response object where the count of
* the data is located. Count is the total number of items.
* * You can use `lodash.get`-compatible keys to access nested objects (e.g., "data.count").
*
* Example:
* ```js
* // Suppose the response object looks like this:
* const response = {
* results: [...],
* count: 100
* };
*
* // By specifying `countKey`, you tell the composable where to find the count of the data:
* const { count } = useFetch({ countKey: "count" });
* console.log(count); // Outputs: 100
* ```
*
* @param {(more, ...args) => Promise<any>} [options.fetchMoreMethod] Function to fetch more data.
* * This function receives a "more" object as its first argument. This "more" object is specified
* by the `moreKey` param.
Expand Down Expand Up @@ -98,7 +44,7 @@ import { ref, computed } from 'vue';
* @returns {FetchObject} An object with properties and methods for managing the fetch process.
*/
export default function useFetch(options) {
const { fetchMethod, fetchMoreMethod, dataKey, moreKey, countKey } = options || {};
const { fetchMethod, fetchMoreMethod } = options || {};

const loading = ref(false);
const data = ref(null);
Expand All @@ -109,13 +55,8 @@ export default function useFetch(options) {

const hasMore = computed(() => more.value != null);

const _setFromKeys = (response, loadingMore) => {
let responseData;
if (!dataKey) {
responseData = response;
} else {
responseData = get(response, dataKey);
}
const _setData = (response, loadingMore) => {
const responseData = fetchMoreMethod ? response.results : response;

/**
* For now, loading more just works if the data is an array.
Expand All @@ -126,13 +67,8 @@ export default function useFetch(options) {
data.value = responseData;
}

if (moreKey) {
more.value = get(response, moreKey) || null;
}

if (countKey) {
count.value = get(response, countKey) || null;
}
more.value = response.more || null;
count.value = response.count || null;
};

const fetchData = async (...args) => {
Expand All @@ -141,7 +77,7 @@ export default function useFetch(options) {

try {
const response = await fetchMethod(...args);
_setFromKeys(response);
_setData(response);
} catch (err) {
error.value = err;
}
Expand All @@ -159,7 +95,7 @@ export default function useFetch(options) {

try {
const response = await fetchMoreMethod(more.value, ...args);
_setFromKeys(response, loadingMore.value);
_setData(response, loadingMore.value);
} catch (err) {
error.value = err;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ export default function useResourceSelection() {
ContentNodeResource.fetchBookmarks({
params: more,
}),
dataKey: 'results',
moreKey: 'more',
countKey: 'count',
});

const channelsFetch = useFetch({
Expand All @@ -78,8 +75,6 @@ export default function useResourceSelection() {
const treeFetch = useFetch({
fetchMethod: () => fetchTree({ id: topicId.value, params: { include_coach_content: true } }),
fetchMoreMethod: more => fetchTree(more),
dataKey: 'results',
moreKey: 'more',
});

watch(topicId, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
},
topic: {
type: Object,
required: true,
required: false,
default: null,
},
contentList: {
type: Array,
Expand Down

0 comments on commit 8bfd6cc

Please sign in to comment.