11import * as Common from "@frontend/common" ;
2- import { Add , Delete , Edit } from "@mui/icons-material" ;
2+ import { Add , Close , Delete , Edit } from "@mui/icons-material" ;
33import {
44 Box ,
55 Button ,
66 ButtonProps ,
77 CircularProgress ,
8+ IconButton ,
89 Stack ,
910 Tab ,
1011 Table ,
@@ -34,6 +35,9 @@ type onSubmitType = (data: Record<string, string>, event: React.FormEvent<unknow
3435type AppResourceType = { app : string ; resource : string } ;
3536type AppResourceIdType = AppResourceType & { id ?: string } ;
3637type AdminEditorPropsType = React . PropsWithChildren < {
38+ hidingFields ?: string [ ] ;
39+ context ?: Record < string , string > ;
40+ onClose ?: ( ) => void ;
3741 beforeSubmit ?: onSubmitType ;
3842 afterSubmit ?: onSubmitType ;
3943 notModifiable ?: boolean ;
@@ -125,7 +129,7 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
125129 { fallback : Common . Components . ErrorFallback } ,
126130 Suspense . with (
127131 { fallback : < CircularProgress /> } ,
128- ( { app, resource, id, beforeSubmit, afterSubmit, extraActions, notModifiable, notDeletable, children } ) => {
132+ ( { app, resource, id, hidingFields , context , onClose , beforeSubmit, afterSubmit, extraActions, notModifiable, notDeletable, children } ) => {
129133 const navigate = useNavigate ( ) ;
130134 const formRef = React . useRef < Form < Record < string , string > , RJSFSchema , { [ k in string ] : unknown } > | null > ( null ) ;
131135 const [ editorState , setEditorState ] = React . useState < InnerAdminEditorStateType > ( {
@@ -136,11 +140,8 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
136140 const { data : schemaInfo } = Common . Hooks . BackendAdminAPI . useSchemaQuery ( backendAdminClient , app , resource ) ;
137141
138142 const setTab = ( _ : React . SyntheticEvent , tab : number ) => setEditorState ( ( ps ) => ( { ...ps , tab } ) ) ;
139- const setFormDataState = ( newFormData ?: Record < string , string > ) =>
140- setEditorState ( ( ps ) => {
141- const formData = newFormData ? { ...ps . formData , ...newFormData } : ps . formData ;
142- return { ...ps , formData } ;
143- } ) ;
143+ const setFormData = ( formData ?: Record < string , string > ) => setEditorState ( ( ps ) => ( { ...ps , formData } ) ) ;
144+ const appendFormDataState = ( data ?: Record < string , string > ) => setEditorState ( ( ps ) => ( { ...ps , formData : { ...ps . formData , ...data } } ) ) ;
144145 const selectedLanguage = editorState . tab === 0 ? "ko" : "en" ;
145146 const notSelectedLanguage = editorState . tab === 0 ? "en" : "ko" ;
146147
@@ -152,14 +153,15 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
152153 React . useEffect ( ( ) => {
153154 ( async ( ) => {
154155 if ( ! id ) {
155- setFormDataState ( { } ) ;
156+ setFormData ( context || { } ) ;
156157 return ;
157158 }
158159
159- setFormDataState ( ( await Common . BackendAdminAPIs . retrieve < Record < string , string > > ( backendAdminClient , app , resource , id ) ( ) ) || { } ) ;
160+ const initialData = await Common . BackendAdminAPIs . retrieve < Record < string , string > > ( backendAdminClient , app , resource , id ) ( ) ;
161+ setFormData ( { ...initialData , ...context } ) ;
160162 } ) ( ) ;
161163 // eslint-disable-next-line react-hooks/exhaustive-deps
162- } , [ app , resource , id ] ) ;
164+ } , [ app , resource , id , context ] ) ;
163165
164166 const onSubmitButtonClick : React . MouseEventHandler < HTMLButtonElement > = ( ) => formRef . current && formRef . current . submit ( ) ;
165167
@@ -194,6 +196,12 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
194196
195197 const goToCreateNew = ( ) => navigate ( `/${ app } /${ resource } /create` ) ;
196198
199+ if ( R . isNonNullish ( hidingFields ) && R . isObjectType ( schemaInfo . schema . properties ) ) {
200+ schemaInfo . schema . properties = Object . entries ( schemaInfo . schema . properties || { } )
201+ . filter ( ( [ key ] ) => ! hidingFields . includes ( key ) )
202+ . reduce ( ( acc , [ key , value ] ) => ( { ...acc , [ key ] : value } ) , { } as RJSFSchema ) ;
203+ }
204+
197205 const writableSchema = Common . Utils . filterPropertiesByLanguageInJsonSchema (
198206 Common . Utils . filterWritablePropertiesInJsonSchema ( schemaInfo . schema ) ,
199207 schemaInfo . translation_fields ,
@@ -236,7 +244,10 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
236244
237245 return (
238246 < Box sx = { { flexGrow : 1 , width : "100%" , minHeight : "100%" } } >
239- < Typography variant = "h5" > { title } </ Typography >
247+ < Stack direction = "row" justifyContent = "space-between" >
248+ < Typography variant = "h5" > { title } </ Typography >
249+ { onClose && < Box children = { < IconButton children = { < Close /> } /> } onClick = { onClose } /> }
250+ </ Stack >
240251 < Stack direction = "row" spacing = { 2 } sx = { { width : "100%" , height : "100%" , maxWidth : "100%" } } >
241252 < Tabs orientation = "vertical" value = { editorState . tab } onChange = { setTab } scrollButtons = { false } >
242253 < Tab wrapped label = "한국어" />
@@ -278,7 +289,7 @@ const InnerAdminEditor: React.FC<AppResourceIdType & AdminEditorPropsType> = Err
278289 liveValidate
279290 focusOnFirstError
280291 formContext = { { readonlyAsDisabled : true } }
281- onChange = { ( { formData } ) => setFormDataState ( formData ) }
292+ onChange = { ( { formData } ) => appendFormDataState ( formData ) }
282293 onSubmit = { onSubmitFunc }
283294 disabled = { disabled }
284295 showErrorList = { false }
0 commit comments