feat: medusa-react admin hooks (#978)

* add: medusa admin hooks + tests

* fix: remove unneeded props

* fix: deps

* fix: deps

* fix: deps

* fix: failing tests

* fix: failing tests

* fix: query key

* add: yarn workspaces

* fix: linting medusa-react

* fix: add prepare script

* fix: buildOptions

* fix: useAdminShippingOptions query

* fix: use qs instead for query params (#1019)

* fix: formatting

* debug: ci pipeline

* debug: log node_modules structure

* debug: use lerna bootstrap

* debug: update node version

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: print pkgs in workspace

* debug: add explicit build step

* fix: jsdoc

* debug: run build step

* debug: fix build errors

* debug: add build step to integration tests

* fix: failing test

* cleanup

Co-authored-by: Sebastian Rindom <seb@medusajs.com>
Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
Zakaria El Asri
2022-02-02 17:10:56 +01:00
committed by GitHub
parent 24175025d1
commit 2e384842d5
209 changed files with 27917 additions and 2727 deletions

View File

@@ -0,0 +1,28 @@
import { QueryClient, QueryKey, UseMutationOptions } from "react-query"
export const buildOptions = <
TData,
TError,
TVariables,
TContext,
TKey extends Array<QueryKey>
>(
queryClient: QueryClient,
queryKey: TKey[] | TKey,
options?: UseMutationOptions<TData, TError, TVariables, TContext>
): UseMutationOptions<TData, TError, TVariables, TContext> => {
return {
...options,
onSuccess: (...args) => {
if (options?.onSuccess) {
return options.onSuccess(...args)
}
if (queryKey.filter(Array.isArray).length > 0) {
queryKey.forEach(key => queryClient.invalidateQueries(key))
} else {
queryClient.invalidateQueries(queryKey)
}
},
}
}

View File

@@ -1,62 +1,2 @@
import * as React from "react"
type TQueryKey<TKey, TListQuery = any, TDetailQuery = string> = {
all: [TKey]
lists: () => [...TQueryKey<TKey>["all"], "list"]
list: (
query?: TListQuery
) => [
...ReturnType<TQueryKey<TKey>["lists"]>,
{ query: TListQuery | undefined }
]
details: () => [...TQueryKey<TKey>["all"], "detail"]
detail: (
id: TDetailQuery
) => [...ReturnType<TQueryKey<TKey>["details"]>, TDetailQuery]
}
export const makeKeysFactory = <
T,
TListQueryType = any,
TDetailQueryType = string
>(
globalKey: T
) => {
const queryKeyFactory: TQueryKey<T, TListQueryType, TDetailQueryType> = {
all: [globalKey],
lists: () => [...queryKeyFactory.all, "list"],
list: (query?: TListQueryType) => [...queryKeyFactory.lists(), { query }],
details: () => [...queryKeyFactory.all, "detail"],
detail: (id: TDetailQueryType) => [...queryKeyFactory.details(), id],
}
return queryKeyFactory
}
export const useLocalStorage = (key: string, initialState: string) => {
const [item, setItem] = React.useState(() => {
try {
const item =
typeof window !== "undefined" && window.localStorage.getItem(key)
return item || initialState
} catch (err) {
return initialState
}
})
const save = (data: string) => {
setItem(data)
if (typeof window !== "undefined") {
window.localStorage.setItem(key, data)
}
}
const remove = () => {
if (typeof window !== "undefined") {
window.localStorage.removeItem(key)
}
}
return [item, save, remove] as const
}
export * from "./queryKeysFactory"
export * from "./useLocalStorage"

View File

@@ -0,0 +1,18 @@
import { TQueryKey } from "../../types"
export const queryKeysFactory = <
T,
TListQueryType = any,
TDetailQueryType = string
>(
globalKey: T
) => {
const queryKeyFactory: TQueryKey<T, TListQueryType, TDetailQueryType> = {
all: [globalKey],
lists: () => [...queryKeyFactory.all, "list"],
list: (query?: TListQueryType) => [...queryKeyFactory.lists(), { query }],
details: () => [...queryKeyFactory.all, "detail"],
detail: (id: TDetailQueryType) => [...queryKeyFactory.details(), id],
}
return queryKeyFactory
}

View File

@@ -0,0 +1,30 @@
import * as React from "react"
export const useLocalStorage = (key: string, initialState: string) => {
const [item, setItem] = React.useState(() => {
try {
const item =
typeof window !== "undefined" && window.localStorage.getItem(key)
return item || initialState
} catch (err) {
return initialState
}
})
const save = (data: string) => {
setItem(data)
if (typeof window !== "undefined") {
window.localStorage.setItem(key, data)
}
}
const remove = () => {
if (typeof window !== "undefined") {
window.localStorage.removeItem(key)
}
}
return [item, save, remove] as const
}