-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import ky from 'ky/umd' | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
|
||
const MAP_SERVER_URL = 'http://127.0.0.1:' + window.mapServerPort | ||
|
||
/** | ||
* @param {string} resourcePath | ||
* @param {'post' | 'put' | 'delete'} | ||
*/ | ||
export function useMapServerMutation (resourcePath, mutationType) { | ||
const queryClient = useQueryClient() | ||
const kyFunction = path => | ||
mutationType === 'post' | ||
? ky.post(path) | ||
: mutationType === 'put' | ||
? ky.put(path) | ||
: ky.delete(path) | ||
|
||
return useMutation({ | ||
mutationFn: () => kyFunction(MAP_SERVER_URL + resourcePath), | ||
onSuccess: queryClient.invalidateQueries({ queryKey: resourcePath }) | ||
}) | ||
} |
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,17 +1,22 @@ | ||
// @ts-check | ||
import ky from 'ky/umd' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
// local host | ||
// global port number | ||
|
||
const MAP_SERVER_URL = 'http://127.0.0.1:' + window.mapServerPort | ||
|
||
/** | ||
* @param {'/styles' | `/styles/${string}` | '/tiles' | `/tiles/${string}`} resourcePath URL path to resource on Map Server (needs to start with `/`) | ||
* @param {'/styles' | `/styles/${string}` | '/tilesets' | `/tilesets/${string}`} resourcePath URL path to resource on Map Server (needs to start with `/`) | ||
*/ | ||
export default function useMapServerQuery (resourcePath) { | ||
export function useMapServerQuery (resourcePath) { | ||
return useQuery({ | ||
queryKey: [resourcePath], | ||
queryFn: () => ky.get(MAP_SERVER_URL + resourcePath) | ||
queryFn: () => ky.get(MAP_SERVER_URL + resourcePath).json() | ||
}) | ||
} | ||
|
||
/** | ||
* @type {import('@tanstack/react-query').UseQueryResult<ReturnType<import('@mapeo/map-server/dist/api/styles').StylesApi["listStyles"]>>} | ||
*/ | ||
const { data } = useMapServerQuery('/styles') |
26 changes: 26 additions & 0 deletions
26
src/renderer/hooks/useMapServerQueryWithImplicitTypings.js
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,26 @@ | ||
// @ts-check | ||
import ky from 'ky/umd' | ||
import { useQuery } from '@tanstack/react-query' | ||
|
||
// local host | ||
// global port number | ||
const MAP_SERVER_URL = 'http://127.0.0.1:' + window.mapServerPort | ||
|
||
const apiLayer = () => { | ||
function getStylesFn () { | ||
/** | ||
* @type {Promise<ReturnType<import('@mapeo/map-server/dist/api/styles').StylesApi["listStyles"]>>} | ||
*/ | ||
const listSyles = ky.get(MAP_SERVER_URL + '/styles').json() | ||
return listSyles | ||
} | ||
|
||
return { | ||
getStyle: getStylesFn | ||
} | ||
} | ||
|
||
const { data } = useQuery({ | ||
queryKey: ['listStyle'], | ||
queryFn: apiLayer().getStyle | ||
}) |
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