Skip to content

Commit

Permalink
chore: sampled typings
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSin committed Nov 28, 2022
1 parent 5dc5b81 commit 268e4f7
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/renderer/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { StylesProvider, ThemeProvider } from '@material-ui/styles'
import { IntlProvider } from 'react-intl'
import isDev from 'electron-is-dev'
import CssBaseline from '@material-ui/core/CssBaseline'

import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import logger from '../logger'
import theme from './theme'
import Home from './components/Home'
Expand Down Expand Up @@ -48,6 +48,8 @@ const App = () => {
const [locale, setLocale] = React.useState(initialLocale)
const [backendState, setBackendState] = React.useState('loading')

const queryClient = new QueryClient()

React.useEffect(() => {
ipcRenderer
.invoke('get-backend-state')
Expand Down Expand Up @@ -87,7 +89,9 @@ const App = () => {
<ThemeProvider theme={theme}>
<CssBaseline />
<IntlProvider locale={locale} messages={msgs[locale]}>
<Home onSelectLanguage={handleLanguageChange} />
<QueryClientProvider client={queryClient}>
<Home onSelectLanguage={handleLanguageChange} />
</QueryClientProvider>
</IntlProvider>
</ThemeProvider>
</StylesProvider>
Expand Down
23 changes: 23 additions & 0 deletions src/renderer/hooks/useMapServerMutation.js
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 })
})
}
13 changes: 9 additions & 4 deletions src/renderer/hooks/useMapServerQuery.js
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 src/renderer/hooks/useMapServerQueryWithImplicitTypings.js
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
})
5 changes: 0 additions & 5 deletions src/utils/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,3 @@ export type MapeoCoreOptions = {
export type MapPrinterOptions = {
mapPrinterPort: number
}

export type MapServerOptions = {
mapServerPort: number
mapsdir: string
}

0 comments on commit 268e4f7

Please sign in to comment.