@ -1,18 +1,18 @@
import { useIsAnyActionRunning , useLanguageProfiles } from "apis/hooks" ;
import React , { FunctionComponent , use Memo, useState } from "react" ;
import React , { FunctionComponent , use Effect, use Memo, useState } from "react" ;
import { Container , Form } from "react-bootstrap" ;
import { UseMutationResult } from "react-query" ;
import { GetItemId } from "utilities" ;
import { AsyncButton , Selector } from "../" ;
import BaseModal , { BaseModalProps } from "./BaseModal" ;
import { useModalInformation } from "./hooks" ;
interface Props {
submit : ( form : FormType.ModifyItem ) = > Promise < void > ;
onSuccess ? : ( item : Item.Base ) = > void ;
mutation : UseMutationResult < void , unknown , FormType.ModifyItem , unknown > ;
}
const Editor : FunctionComponent < Props & BaseModalProps > = ( props ) = > {
const { onSuccess, submit , . . . modal } = props ;
const { mutati on, . . . modal } = props ;
const { data : profiles } = useLanguageProfiles ( ) ;
@ -20,6 +20,8 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
modal . modalKey
) ;
const { mutateAsync , isLoading } = mutation ;
const hasTask = useIsAnyActionRunning ( ) ;
const profileOptions = useMemo < SelectorOption < number > [ ] > (
@ -29,14 +31,16 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
} ) ? ? [ ] ,
[ profiles ]
) ;
const [ id , setId ] = useState < Nullable < number > > ( null ) ;
const [ updating , setUpdating ] = useState ( false ) ;
const [ id , setId ] = useState < Nullable < number > > ( payload ? . profileId ? ? null ) ;
useEffect ( ( ) = > {
setId ( payload ? . profileId ? ? null ) ;
} , [ payload ] ) ;
const footer = (
< AsyncButton
noReset
onChange = { setUpdating }
disabled = { hasTask }
promise = { ( ) = > {
if ( payload ) {
@ -45,7 +49,7 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
return null ;
}
return submit ( {
return mutateAsync ( {
id : [ itemId ] ,
profileid : [ id ] ,
} ) ;
@ -53,10 +57,7 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
return null ;
}
} }
onSuccess = { ( ) = > {
closeModal ( ) ;
onSuccess && payload && onSuccess ( payload ) ;
} }
onSuccess = { ( ) = > closeModal ( ) }
>
Save
< / AsyncButton >
@ -64,7 +65,7 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
return (
< BaseModal
closeable = { ! updat ing}
closeable = { ! isLoad ing}
footer = { footer }
title = { payload ? . title }
{ . . . modal }
@ -87,7 +88,7 @@ const Editor: FunctionComponent<Props & BaseModalProps> = (props) => {
clearable
disabled = { hasTask }
options = { profileOptions }
defaultValue= { payload ? . profileI d}
value= { i d}
onChange = { ( v ) = > setId ( v === undefined ? null : v ) }
> < / Selector >
< / Form.Group >