1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 14:54:22 +00:00

🔧 Create useApplyChanges hook

This commit is contained in:
Steffo 2021-10-15 16:51:34 +02:00 committed by Stefano Pigozzi
parent a06acf6bfb
commit e58c8d80e9
4 changed files with 43 additions and 72 deletions

View file

@ -2,6 +2,7 @@ import {Box, Details, Form, Idiomatic as I, useFormState} from "@steffo/bluelib-
import * as React from "react" import * as React from "react"
import {useAuthorizationContext} from "../../contexts/authorization" import {useAuthorizationContext} from "../../contexts/authorization"
import {useCacheContext} from "../../contexts/cache" import {useCacheContext} from "../../contexts/cache"
import {useApplyChanges} from "../../hooks/useApplyChanges"
import {useFormSlug} from "../../hooks/useFormSlug" import {useFormSlug} from "../../hooks/useFormSlug"
import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet" import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet"
import {SophonResearchGroup} from "../../types/SophonTypes" import {SophonResearchGroup} from "../../types/SophonTypes"
@ -97,29 +98,13 @@ export function GroupCreateBox({viewSet, resource}: GroupCreateBoxProps): JSX.El
) )
const applyChanges = const applyChanges =
React.useCallback( useApplyChanges(viewSet, resource, {
async () => { name: name.value,
if(resource) { slug: slug,
await resource.update({ description: description.value,
name: name.value, members: members.value,
slug: slug, access: access.value,
description: description.value, })
members: members.value,
access: access.value,
})
}
else {
await viewSet!.create({
name: name.value,
slug: slug,
description: description.value,
members: members.value,
access: access.value,
})
}
},
[viewSet, resource, name, slug, description, members, access],
)
const canApply = const canApply =
React.useMemo( React.useMemo(

View file

@ -2,6 +2,7 @@ import {Box, Details, Form, Idiomatic as I, useFormState} from "@steffo/bluelib-
import * as React from "react" import * as React from "react"
import {useAuthorizationContext} from "../../contexts/authorization" import {useAuthorizationContext} from "../../contexts/authorization"
import {useProjectContext} from "../../contexts/project" import {useProjectContext} from "../../contexts/project"
import {useApplyChanges} from "../../hooks/useApplyChanges"
import {useFormSlug} from "../../hooks/useFormSlug" import {useFormSlug} from "../../hooks/useFormSlug"
import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet" import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet"
import {SophonNotebook} from "../../types/SophonTypes" import {SophonNotebook} from "../../types/SophonTypes"
@ -33,33 +34,13 @@ export function NotebookCreateBox({viewSet, resource}: NotebookCreateBoxProps):
Validators.alwaysValid, Validators.alwaysValid,
) )
// TODO: Fix this
const applyChanges = const applyChanges =
React.useCallback( useApplyChanges(viewSet, resource, {
async () => { name: name.value,
if(!project) { slug: slug,
return container_image: image.value,
} project: project!.value.slug,
})
if(resource) {
await resource.update({
name: name.value,
slug: slug,
container_image: image.value,
project: project?.value.slug,
})
}
else {
await viewSet!.create({
name: name.value,
slug: slug,
container_image: image.value,
project: project?.value.slug,
})
}
},
[viewSet, resource, name, slug, image, project],
)
const canApply = const canApply =
React.useMemo( React.useMemo(

View file

@ -2,6 +2,7 @@ import {Box, Details, Form, Idiomatic as I, useFormState} from "@steffo/bluelib-
import * as React from "react" import * as React from "react"
import {useAuthorizationContext} from "../../contexts/authorization" import {useAuthorizationContext} from "../../contexts/authorization"
import {useGroupContext} from "../../contexts/group" import {useGroupContext} from "../../contexts/group"
import {useApplyChanges} from "../../hooks/useApplyChanges"
import {useFormSlug} from "../../hooks/useFormSlug" import {useFormSlug} from "../../hooks/useFormSlug"
import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet" import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet"
import {SophonResearchProject} from "../../types/SophonTypes" import {SophonResearchProject} from "../../types/SophonTypes"
@ -67,29 +68,13 @@ export function ProjectCreateBox({viewSet, resource}: ProjectCreateBoxProps): JS
) )
const applyChanges = const applyChanges =
React.useCallback( useApplyChanges(viewSet, resource, {
async () => { name: name.value,
if(resource) { slug: slug,
await resource.update({ description: description.value,
name: name.value, visibility: visibility.value,
slug: slug, group: group!.value.slug,
description: description.value, })
visibility: visibility.value,
group: group!.value.slug,
})
}
else {
await viewSet!.create({
name: name.value,
slug: slug,
description: description.value,
visibility: visibility.value,
group: group!.value.slug,
})
}
},
[viewSet, resource, name, slug, description, visibility, group],
)
const canApply = const canApply =
React.useMemo( React.useMemo(

View file

@ -0,0 +1,20 @@
import * as React from "react"
import {ManagedResource, ManagedViewSet} from "./useManagedViewSet"
export function useApplyChanges<T>(viewSet: ManagedViewSet<T> | undefined, resource: ManagedResource<T> | undefined, values: Partial<T>) {
return React.useCallback(
async () => {
if(resource) {
await resource.update(values)
}
else if(viewSet) {
await viewSet.create(values)
}
else {
console.error("applyChanges called when both viewSet and resources were falsy, ignoring the call...")
}
},
[viewSet, resource, values],
)
}