-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(media): upgrade getMediaDetails from v1 to v2 API endpoint (#1580)
* ref(media): upgrade getMediaDetails from v1 to v2 API endpoint * fix(media): adjust string array to Set
- Loading branch information
Showing
10 changed files
with
170 additions
and
162 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { useQuery, UseQueryOptions, UseQueryResult } from "react-query" | ||
|
||
import { MEDIA_MULTIPLE_CONTENT_KEY } from "constants/queryKeys" | ||
|
||
import { apiService } from "services/ApiService" | ||
|
||
import { MediaService } from "services" | ||
import { MediaData } from "types/directory" | ||
import { MultipleMediaParams } from "types/media" | ||
|
||
const getMultipleMedia = (params: MultipleMediaParams) => { | ||
const mediaService = new MediaService({ apiClient: apiService }) | ||
|
||
return Promise.all( | ||
[...params.mediaSrcs].map((mediaSrc) => { | ||
const mediaUrlTokens = mediaSrc.split("/") | ||
// Note: The mediaUrl will always start with a "/" | ||
const mediaDirectoryName = mediaUrlTokens.slice(1, -1).join("%2F") | ||
const fileName = mediaUrlTokens.pop() | ||
|
||
const reqParams = { | ||
siteName: params.siteName, | ||
mediaDirectoryName, | ||
fileName, | ||
} | ||
|
||
return mediaService.get(reqParams).catch((error) => { | ||
// It is possible for the user to specify a media that does not exist | ||
// and we will automatically load the placeholder image for them | ||
// The error needs to be caught here so that other images can still | ||
// load, as Promise.all will fail if any of the promises fail | ||
return error | ||
}) as Promise<MediaData> | ||
}) | ||
) | ||
} | ||
|
||
export const useGetMultipleMediaHook = ( | ||
params: MultipleMediaParams, | ||
queryOptions?: Omit<UseQueryOptions<MediaData[]>, "queryFn" | "queryKey"> | ||
): UseQueryResult<MediaData[]> => { | ||
return useQuery<MediaData[]>( | ||
[MEDIA_MULTIPLE_CONTENT_KEY, params], | ||
() => getMultipleMedia(params), | ||
{ | ||
...queryOptions, | ||
retry: false, | ||
} | ||
) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface MultipleMediaParams { | ||
siteName: string | ||
mediaSrcs: Set<string> | ||
} |
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 |
---|---|---|
@@ -1,14 +1,17 @@ | ||
export * from "./generate" | ||
export * from "./colours" | ||
export * from "./date" | ||
export * from "./decoding" | ||
export * from "./deslugify" | ||
export * from "./directoryUtils" | ||
export * from "./fileNameUtils" | ||
export * from "./files" | ||
export * from "./generate" | ||
export * from "./images" | ||
export * from "./markdownToolbar" | ||
export * from "./misc" | ||
export * from "./siteColorUtils" | ||
export * from "./text" | ||
export * from "./toasts" | ||
export * from "./validators" | ||
|
||
export * from "./legacy" | ||
export * from "./text" | ||
export * from "./date" | ||
export * from "./files" |
Oops, something went wrong.