Skip to content

Commit

Permalink
fix(datastore): always use put for datastore
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Sep 6, 2023
1 parent fbb1a20 commit d3c4625
Showing 1 changed file with 2 additions and 44 deletions.
46 changes: 2 additions & 44 deletions src/lib/dataStore/useDataStore.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
FetchError,
useDataEngine,
useDataMutation,
useDataQuery,
} from '@dhis2/app-runtime'
import { useMemo } from 'react'
import { useDataEngine } from '@dhis2/app-runtime'
import {
useQuery,
useMutation,
Expand All @@ -23,23 +17,14 @@ type GetMutationTypeUnion<MutationType extends string> = {
}
type UpdateMutationTypeUnion = GetMutationTypeUnion<'update'>
type UpdateMutation = Extract<Mutation, UpdateMutationTypeUnion>
type CreateMutation = Extract<Mutation, GetMutationTypeUnion<'create'>>
type UpdateMutationData = UpdateMutation['data']
// type UpdateMutation = { type: 'update' | 'replace' | 'json-patch'; /* other properties */ };
// type DeleteMutation = { type: 'delete'; /* other properties */ };

//type Mutation = UpdateMutation | DeleteMutation;

type DataStoreOptions = {
namespace: string
key?: string
global?: boolean
}

type WrapInResult<TResult> = {
result: TResult
}

type ObjectResult = Record<string, any>

Check warning on line 28 in src/lib/dataStore/useDataStore.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type

const createBoundQueryFn =
Expand Down Expand Up @@ -71,17 +56,6 @@ export const queryCreators = {
id: `${namespace}/${key}`,
},
}),
createKey: <TData>({
namespace,
global,
key,
data,
}: SetValuesOptions<TData>): CreateMutation => ({
resource: `${getDataStoreResource(global)}/${namespace}/${key}`,
type: 'create',
// engine enforces data to be an object with keys, but can actually store any JSON-value
data: data as CreateMutation['data'],
}),
setValues: <TData>({
namespace,
global,
Expand Down Expand Up @@ -149,23 +123,7 @@ export const useMutateDataStoreValues = (options: ValuesOptions) => {
global: mergedOptions.global,
data,
})
try {
return await engine.mutate(mutation)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// if namespace or key does not exist, create it
if (error.details?.httpStatusCode === 404) {
const mut = queryCreators.createKey({
namespace: mergedOptions.namespace,
key: mergedOptions.key,
global: mergedOptions.global,
data,
})
return engine.mutate(mut)
} else {
throw error
}
}
return await engine.mutate(mutation)
}
const mutation = useMutation({
mutationFn,
Expand Down

0 comments on commit d3c4625

Please sign in to comment.