-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #331 from RolnickLab/web-ui-entities
Introduce related entities
- Loading branch information
Showing
30 changed files
with
965 additions
and
42 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,6 @@ | ||
export interface EntityFieldValues { | ||
description: string | ||
name: string | ||
projectId: string | ||
customFields?: { [key: string]: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import axios from 'axios' | ||
import { API_URL } from 'data-services/constants' | ||
import { getAuthHeader } from 'data-services/utils' | ||
import { useUser } from 'utils/user/userContext' | ||
import { EntityFieldValues } from './types' | ||
import { convertToServerFieldValues } from './utils' | ||
|
||
const SUCCESS_TIMEOUT = 1000 // Reset success after 1 second | ||
|
||
export const useCreateEntity = (collection: string, onSuccess?: () => void) => { | ||
const { user } = useUser() | ||
const queryClient = useQueryClient() | ||
|
||
const { mutateAsync, isLoading, isSuccess, reset, error } = useMutation({ | ||
mutationFn: (fieldValues: EntityFieldValues) => | ||
axios.post( | ||
`${API_URL}/${collection}/`, | ||
convertToServerFieldValues(fieldValues), | ||
{ | ||
headers: getAuthHeader(user), | ||
} | ||
), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries([collection]) | ||
onSuccess?.() | ||
setTimeout(reset, SUCCESS_TIMEOUT) | ||
}, | ||
}) | ||
|
||
return { createEntity: mutateAsync, isLoading, isSuccess, error } | ||
} |
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 { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import axios from 'axios' | ||
import { API_URL } from 'data-services/constants' | ||
import { getAuthHeader } from 'data-services/utils' | ||
import { useUser } from 'utils/user/userContext' | ||
|
||
export const useDeleteEntity = (collection: string, onSuccess?: () => void) => { | ||
const { user } = useUser() | ||
const queryClient = useQueryClient() | ||
|
||
const { mutateAsync, isLoading, isSuccess, error } = useMutation({ | ||
mutationFn: (id: string) => | ||
axios.delete(`${API_URL}/${collection}/${id}/`, { | ||
headers: getAuthHeader(user), | ||
}), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries([collection]) | ||
onSuccess?.() | ||
}, | ||
}) | ||
|
||
return { deleteEntity: mutateAsync, isLoading, isSuccess, error } | ||
} |
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,49 @@ | ||
import { Entity, ServerEntity } from 'data-services/models/entity' | ||
import { Storage } from 'data-services/models/storage' | ||
import { FetchParams } from 'data-services/types' | ||
import { getFetchUrl } from 'data-services/utils' | ||
import { useMemo } from 'react' | ||
import { useAuthorizedQuery } from '../auth/useAuthorizedQuery' | ||
|
||
const convertServerRecord = (collection: string, record: ServerEntity) => { | ||
if (collection === 'storage') { | ||
return new Storage(record) | ||
} | ||
|
||
return new Entity(record) | ||
} | ||
|
||
export const useEntities = ( | ||
collection: string, | ||
params?: FetchParams | ||
): { | ||
entities?: Entity[] | ||
total: number | ||
isLoading: boolean | ||
isFetching: boolean | ||
error?: unknown | ||
} => { | ||
const fetchUrl = getFetchUrl({ collection, params }) | ||
|
||
const { data, isLoading, isFetching, error } = useAuthorizedQuery<{ | ||
results: ServerEntity[] | ||
count: number | ||
}>({ | ||
queryKey: [collection, params], | ||
url: fetchUrl, | ||
}) | ||
|
||
const entities = useMemo( | ||
() => | ||
data?.results.map((record) => convertServerRecord(collection, record)), | ||
[data] | ||
) | ||
|
||
return { | ||
entities, | ||
total: data?.count ?? 0, | ||
isLoading, | ||
isFetching, | ||
error, | ||
} | ||
} |
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,36 @@ | ||
import { useMutation, useQueryClient } from '@tanstack/react-query' | ||
import axios from 'axios' | ||
import { API_URL } from 'data-services/constants' | ||
import { getAuthHeader } from 'data-services/utils' | ||
import { useUser } from 'utils/user/userContext' | ||
import { EntityFieldValues } from './types' | ||
import { convertToServerFieldValues } from './utils' | ||
|
||
const SUCCESS_TIMEOUT = 1000 // Reset success after 1 second | ||
|
||
export const useUpdateEntity = ( | ||
id: string, | ||
collection: string, | ||
onSuccess?: () => void | ||
) => { | ||
const { user } = useUser() | ||
const queryClient = useQueryClient() | ||
|
||
const { mutateAsync, isLoading, isSuccess, reset, error } = useMutation({ | ||
mutationFn: (fieldValues: EntityFieldValues) => | ||
axios.patch( | ||
`${API_URL}/${collection}/${id}/`, | ||
convertToServerFieldValues(fieldValues), | ||
{ | ||
headers: getAuthHeader(user), | ||
} | ||
), | ||
onSuccess: () => { | ||
queryClient.invalidateQueries([collection]) | ||
onSuccess?.() | ||
setTimeout(reset, SUCCESS_TIMEOUT) | ||
}, | ||
}) | ||
|
||
return { updateEntity: mutateAsync, isLoading, error, isSuccess } | ||
} |
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,10 @@ | ||
import { EntityFieldValues } from './types' | ||
|
||
export const convertToServerFieldValues = (fieldValues: EntityFieldValues) => { | ||
return { | ||
description: fieldValues.description, | ||
name: fieldValues.name, | ||
project: fieldValues.projectId, | ||
...(fieldValues.customFields ?? {}), | ||
} | ||
} |
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,44 @@ | ||
import { getFormatedDateTimeString } from 'utils/date/getFormatedDateTimeString/getFormatedDateTimeString' | ||
import { UserPermission } from 'utils/user/types' | ||
|
||
export type ServerEntity = any // TODO: Update this type | ||
|
||
export class Entity { | ||
protected readonly _data: ServerEntity | ||
|
||
public constructor(entity: ServerEntity) { | ||
this._data = entity | ||
} | ||
|
||
get canDelete(): boolean { | ||
return this._data.user_permissions.includes(UserPermission.Delete) | ||
} | ||
|
||
get createdAt(): string { | ||
return getFormatedDateTimeString({ | ||
date: new Date(this._data.created_at), | ||
}) | ||
} | ||
|
||
get description(): string { | ||
return this._data.description | ||
} | ||
|
||
get id(): string { | ||
return `${this._data.id}` | ||
} | ||
|
||
get name(): string { | ||
return this._data.name | ||
} | ||
|
||
get updatedAt(): string | undefined { | ||
if (!this._data.updated_at) { | ||
return undefined | ||
} | ||
|
||
return getFormatedDateTimeString({ | ||
date: new Date(this._data.updated_at), | ||
}) | ||
} | ||
} |
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,21 @@ | ||
import { Entity } from './entity' | ||
|
||
export type ServerStorage = any // TODO: Update this type | ||
|
||
export class Storage extends Entity { | ||
public constructor(entity: Storage) { | ||
super(entity) | ||
} | ||
|
||
get bucket(): string { | ||
return this._data.bucket | ||
} | ||
|
||
get endpointUrl(): string { | ||
return this._data.endpoint_url | ||
} | ||
|
||
get publicBaseUrl(): string { | ||
return this._data.public_base_url | ||
} | ||
} |
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
Oops, something went wrong.