11import { useEffect , useState , useContext } from "react" ;
22import { useTranslation } from "react-i18next" ;
3- import get from "lodash.get" ;
4- import set from "lodash.set" ;
53import { ulid } from "ulid" ;
64import { useDispatch } from "react-redux" ;
75import dayjs from "dayjs" ;
@@ -155,7 +153,7 @@ function SlideManager({
155153 */
156154 const handleInput = ( { target } ) => {
157155 const localFormStateObject = { ...formStateObject } ;
158- set ( localFormStateObject , target . id , target . value ) ;
156+ localFormStateObject [ target . id ] = target . value ;
159157 setFormStateObject ( localFormStateObject ) ;
160158 } ;
161159
@@ -208,7 +206,7 @@ function SlideManager({
208206 const value =
209207 target . type === "number" ? target . valueAsNumber : target . value ;
210208 const localFormStateObject = { ...formStateObject } ;
211- set ( localFormStateObject . content , target . id , value ) ;
209+ localFormStateObject . content [ target . id ] = value ;
212210 setFormStateObject ( localFormStateObject ) ;
213211 } ;
214212
@@ -353,12 +351,12 @@ function SlideManager({
353351 // It is an already added temp file.
354352 if ( entry . tempId ) {
355353 newField . push ( entry . tempId ) ;
356- set ( localMediaData , entry . tempId , entry ) ;
354+ localMediaData [ entry . tempId ] = entry ;
357355 }
358356 // It is a new temp file.
359357 else {
360358 if ( ! Array . isArray ( localFormStateObject . content [ fieldId ] ) ) {
361- set ( localFormStateObject . content , fieldId , [ ] ) ;
359+ localFormStateObject . content [ fieldId ] = [ ] ;
362360 }
363361
364362 // Create a tempId for the media.
@@ -369,7 +367,7 @@ function SlideManager({
369367
370368 const newEntry = { ...entry } ;
371369 newEntry . tempId = tempId ;
372- set ( localMediaData , tempId , newEntry ) ;
370+ localMediaData [ tempId ] = newEntry ;
373371 }
374372 }
375373 // Previously selected file.
@@ -383,18 +381,16 @@ function SlideManager({
383381 if (
384382 ! Object . prototype . hasOwnProperty . call ( localMediaData , entry [ "@id" ] )
385383 ) {
386- set ( localMediaData , entry [ "@id" ] , entry ) ;
384+ localMediaData [ entry [ "@id" ] ] = entry ;
387385
388386 localFormStateObject . media . push ( entry [ "@id" ] ) ;
389387 }
390388 }
391389 } ) ;
392390 }
393391
394- set ( localFormStateObject . content , fieldId , newField ) ;
395- set ( localFormStateObject , "media" , [
396- ...new Set ( [ ...localFormStateObject . media ] ) ,
397- ] ) ;
392+ localFormStateObject . content [ fieldId ] = newField ;
393+ localFormStateObject . media = [ ...new Set ( [ ...localFormStateObject . media ] ) ] ;
398394
399395 setFormStateObject ( localFormStateObject ) ;
400396 setMediaData ( localMediaData ) ;
@@ -406,7 +402,7 @@ function SlideManager({
406402
407403 // Setup submittingMedia list.
408404 mediaFields . forEach ( ( fieldName ) => {
409- const fieldData = get ( formStateObject . content , fieldName ) ;
405+ const fieldData = formStateObject . content [ fieldName ] ;
410406
411407 if ( fieldData ) {
412408 if ( Array . isArray ( fieldData ) ) {
@@ -548,33 +544,25 @@ function SlideManager({
548544
549545 /** Submitted media is successful. */
550546 useEffect ( ( ) => {
551- if ( submitting ) {
552- if ( isSaveMediaSuccess ) {
553- const newSubmittingMedia = [ ...submittingMedia ] ;
554- const submittedMedia = newSubmittingMedia . shift ( ) ;
555-
556- const newFormStateObject = { ...formStateObject } ;
557- newFormStateObject . media . push ( savedMediaData [ "@id" ] ) ;
558-
559- // Replace TEMP-- id with real id.
560- set (
561- newFormStateObject . content ,
562- submittedMedia . fieldName ,
563- get ( newFormStateObject . content , submittedMedia . fieldName ) . map (
564- ( mediaId ) =>
565- mediaId === submittedMedia . tempId
566- ? savedMediaData [ "@id" ]
567- : mediaId ,
568- ) ,
547+ if ( submitting && isSaveMediaSuccess ) {
548+ const newSubmittingMedia = [ ...submittingMedia ] ;
549+ const submittedMedia = newSubmittingMedia . shift ( ) ;
550+
551+ const newFormStateObject = { ...formStateObject } ;
552+ newFormStateObject . media . push ( savedMediaData [ "@id" ] ) ;
553+
554+ // Replace TEMP-- id with real id.
555+ newFormStateObject . content [ submittedMedia . fieldName ] =
556+ newFormStateObject . content [ submittedMedia . fieldName ] . map ( ( mediaId ) =>
557+ mediaId === submittedMedia . tempId ? savedMediaData [ "@id" ] : mediaId ,
569558 ) ;
570559
571- const newMediaData = { ...mediaData } ;
572- newMediaData [ savedMediaData [ "@id" ] ] = savedMediaData ;
573- setMediaData ( newMediaData ) ;
560+ const newMediaData = { ...mediaData } ;
561+ newMediaData [ savedMediaData [ "@id" ] ] = savedMediaData ;
562+ setMediaData ( newMediaData ) ;
574563
575- // Save new list.
576- setSubmittingMedia ( newSubmittingMedia ) ;
577- }
564+ // Save the new list.
565+ setSubmittingMedia ( newSubmittingMedia ) ;
578566 }
579567 } , [ isSaveMediaSuccess ] ) ;
580568
0 commit comments