1- import React from "react" ;
1+ import React , { useMemo } from "react" ;
22import { ActionButton } from "@/components/custom/action-button" ;
3- import { Check , XIcon } from "lucide-react" ;
4- import { JobInputUploadFile } from "./job-input-upload-file" ;
3+ import { Check , Upload , XIcon } from "lucide-react" ;
4+ // import { JobInputUploadFile } from "./job-input-upload-file";
55import { ProjectJobInputEditWarningModal } from "./job-input-edit-warning-modal" ;
66import { PathFieldWithAction } from "@/components/custom/path-filed-with-actions" ;
7+ import { useToast } from "@/components/ui/use-toast" ;
8+ import { TJob , useUpdateJob } from "@/services/job" ;
9+ import { FeatureNotImplementedModal } from "@/components/custom/feature-not-implemented-modal" ;
710
811export type TProjectJobInputProps = {
12+ job : TJob ;
913 inputPath : string ;
1014 inputName : string ;
1115 onInputPathChange : ( value : string ) => void ;
1216 onRequestView : ( ) => void ;
1317} ;
1418
1519export function ProjectJobInputEdit ( props : TProjectJobInputProps ) {
16- const { onRequestView, inputName, inputPath, onInputPathChange } = props ;
20+ const { onRequestView, inputName, inputPath, job , onInputPathChange } = props ;
1721 const [ isWarningModalOpen , setIsWarningModalOpen ] = React . useState ( false ) ;
1822 const oldValue = React . useRef ( inputPath ) ;
23+ const { toast } = useToast ( ) ;
1924
2025 // To upload new file.
2126 const [ inputFile , setInputFile ] = React . useState < File | null > ( null ) ;
2227
23- const handleOnCloseEditingModeClick = React . useCallback ( ( ) => {
28+ const handleOnErrorSavingChanges = React . useCallback ( ( ) => {
29+ toast ( {
30+ title : "Unable to save changes. Please try again later." ,
31+ variant : "destructive" ,
32+ } ) ;
33+ } , [ toast ] ) ;
34+
35+ const { isPending, mutate : updateJob } = useUpdateJob ( {
36+ onError : handleOnErrorSavingChanges ,
37+ onSuccess : onRequestView ,
38+ } ) ;
39+
40+ const hasAnyFieldUpdated = useMemo ( ( ) => {
2441 if ( inputFile ) {
25- // If input file is not null, we have some files to upload.
26- // Before closing edit mode, we need to show the warning.
27- setIsWarningModalOpen ( true ) ;
28- return ;
42+ // If input file is not null, meaning we have updated input file.
43+ return true ;
2944 }
3045
3146 if ( oldValue . current !== inputPath ) {
32- // Input has been changed, so we need to show the warning,
33- // before closing the edit mode.
47+ // Input has been changed.
48+ return true ;
49+ }
50+ } , [ inputFile , inputPath ] ) ;
51+
52+ const handleOnCloseEditingModeClick = React . useCallback ( ( ) => {
53+ if ( hasAnyFieldUpdated ) {
3454 setIsWarningModalOpen ( true ) ;
3555 return ;
3656 }
3757
3858 onRequestView ( ) ;
39- } , [ inputFile , onRequestView , inputPath ] ) ;
59+ } , [ hasAnyFieldUpdated , onRequestView ] ) ;
4060
4161 const handleOnWarningModalPrimaryActionClick = React . useCallback ( ( ) => {
4262 setInputFile ( null ) ;
4363 onInputPathChange ( oldValue . current ) ;
4464 onRequestView ( ) ;
4565 } , [ onInputPathChange , onRequestView ] ) ;
4666
67+ const handleOnSaveChanges = React . useCallback ( ( ) => {
68+ if ( ! hasAnyFieldUpdated ) {
69+ // if no filed has been updated, then we don't have any thing to
70+ // updated, hence closing the edit view.
71+ onRequestView ( ) ;
72+ }
73+
74+ const specInputs = [ ...job . spec . inputs ] . map ( ( input ) => {
75+ if ( input . name === inputName ) {
76+ return { ...input , path : inputPath } ;
77+ }
78+ return input ;
79+ } ) ;
80+
81+ updateJob ( {
82+ job : {
83+ ...job ,
84+ spec : {
85+ ...job . spec ,
86+ inputs : [ ...specInputs ] ,
87+ } ,
88+ } ,
89+ } ) ;
90+ } , [ hasAnyFieldUpdated , job , updateJob , onRequestView , inputName , inputPath ] ) ;
91+
4792 return (
4893 < >
4994 < PathFieldWithAction
@@ -61,10 +106,18 @@ export function ProjectJobInputEdit(props: TProjectJobInputProps) {
61106 {
62107 id : "upload-file" ,
63108 children : (
64- < JobInputUploadFile
65- inputFile = { inputFile }
66- updateInputFile = { setInputFile }
67- />
109+ < FeatureNotImplementedModal featureName = "Upload File" >
110+ < ActionButton
111+ message = "Upload File"
112+ icon = { < Upload /> }
113+ > </ ActionButton >
114+ { /* TODO: Implement Upload input file feature.
115+ <JobInputUploadFile
116+ inputFile={inputFile}
117+ updateInputFile={setInputFile}
118+ />
119+ */ }
120+ </ FeatureNotImplementedModal >
68121 ) ,
69122 } ,
70123 {
@@ -82,7 +135,9 @@ export function ProjectJobInputEdit(props: TProjectJobInputProps) {
82135 id : "save-changes" ,
83136 children : (
84137 < ActionButton
85- onClick = { onRequestView }
138+ onClick = { handleOnSaveChanges }
139+ isLoading = { isPending }
140+ disabled = { ! hasAnyFieldUpdated }
86141 icon = { < Check /> }
87142 message = "Save changes"
88143 key = "save-changes"
0 commit comments