11import {
2+ Activity ,
23 useCallback ,
34 useEffect ,
5+ useMemo ,
46} from 'react' ;
57import {
68 useNavigate ,
@@ -36,6 +38,7 @@ import {
3638 useDepartmentAndDirectiveQuery ,
3739 useUpdateBlogMutation ,
3840} from '#generated/types/graphql' ;
41+ import useAlert from '#hooks/useAlert' ;
3942
4043type PartialFormType = PartialForm < BlogCreateInput > &
4144{ createdBy : string , modifiedBy : string , slug : string | null } ;
@@ -97,6 +100,8 @@ const defaultEditFormValue: PartialFormType = {
97100
98101function BlogForm ( ) {
99102 const { id } = useParams ( ) ;
103+ const alert = useAlert ( ) ;
104+
100105 const navigate = useNavigate ( ) ;
101106 const [ { data } ] = useBlogDetailQueryQuery ( {
102107 variables : { id : id || '' } , pause : ! id ,
@@ -137,42 +142,53 @@ function BlogForm() {
137142 } ) ;
138143 if ( res . data ?. updateBlog ?. ok ) {
139144 navigate ( '/blog' ) ;
145+ alert . show ( 'Blog updated successfully' , { variant : 'success' } ) ;
140146 } else if ( res . data ?. updateBlog ?. errors ) {
147+ const errorMessages = res . data ?. updateBlog ?. errors ;
141148 setError ( res . data . updateBlog . errors ) ;
149+ alert . show ( errorMessages , { variant : 'danger' } ) ;
142150 }
143151 } else {
144152 const res = await createBlogMutate ( {
145153 data : mutateData ,
146154 } ) ;
147155 if ( res . data ?. createBlog . ok ) {
148156 navigate ( '/blog' ) ;
157+ alert . show ( 'Blog created successfully' , { variant : 'success' } ) ;
149158 } else if ( res . data ?. createBlog ?. errors ) {
159+ const errorMessages = res . data ?. createBlog ?. errors ;
150160 setError ( res . data . createBlog . errors ) ;
161+ alert . show ( errorMessages , { variant : 'danger' } ) ;
151162 }
152163 }
153164 } ,
154165 ) ;
155166 handler ( ) ;
156- } , [ setError , validate , id , updateBlogMutate , createBlogMutate , navigate ] ) ;
167+ } , [ setError , alert , validate , id , updateBlogMutate , createBlogMutate , navigate ] ) ;
157168
158- const departmentOptions = departmentAndDirective ?. departments . results . map (
159- ( dept ) => ( {
169+ const departmentOptions = useMemo (
170+ ( ) => departmentAndDirective ?. departments . results . map ( ( dept ) => ( {
160171 id : dept . id ,
161172 name : dept . title ,
162- } ) ,
163- ) ?? [ ] ;
173+ } ) ) ?? [ ] ,
174+ [ departmentAndDirective ?. departments . results ] ,
175+ ) ;
164176
165- const directiveOptions = departmentAndDirective ?. strategicDirectives . results . map (
166- ( directive ) => ( {
177+ const directiveOptions = useMemo (
178+ ( ) => departmentAndDirective ?. strategicDirectives . results . map ( ( directive ) => ( {
167179 id : directive . id ,
168180 name : directive . title ,
169- } ) ,
170- ) ?? [ ] ;
181+ } ) ) ?? [ ] ,
182+ [ departmentAndDirective ?. strategicDirectives . results ] ,
183+ ) ;
171184
172- const statusOptions = Object . values ( StatusEnum ) . map ( ( status ) => ( {
173- value : status ,
174- label : status ,
175- } ) ) ;
185+ const statusOptions = useMemo (
186+ ( ) => Object . values ( StatusEnum ) . map ( ( status ) => ( {
187+ value : status ,
188+ label : status ,
189+ } ) ) ,
190+ [ ] ,
191+ ) ;
176192
177193 useEffect ( ( ) => {
178194 if ( data ?. blog ) {
@@ -191,11 +207,12 @@ function BlogForm() {
191207 setFieldValue ( `${ blog . createdBy . firstName } ${ blog . createdBy . lastName } ` , 'createdBy' ) ;
192208 }
193209 } , [ data , setFieldValue ] ) ;
210+
194211 return (
195212 < Page >
196213 < ContainerWrapper >
197- < FormSection headingLevel = { 3 } label = " BLOG DETAIL" />
198- { ( value . createdBy && value . modifiedBy ) && (
214+ < FormSection headingLevel = { 3 } label = { id ? ' BLOG DETAIL' : 'CREATE BLOG' } />
215+ < Activity mode = { value . createdBy && value . modifiedBy ? 'visible' : 'hidden' } >
199216 < FormSection >
200217 < Heading level = { 6 } >
201218 Created by:
@@ -208,13 +225,15 @@ function BlogForm() {
208225 { value . createdBy }
209226 </ Heading >
210227 </ FormSection >
211- ) }
228+ </ Activity >
212229 < FormSection label = "Title" description = "Enter the title name of the Blog" withAsteriskOnTitle >
213230 < TextInput
214231 name = "title"
232+ autoFocus
215233 value = { value . title }
216234 error = { error ?. title as string }
217235 onChange = { setFieldValue }
236+ placeholder = "title"
218237 />
219238 </ FormSection >
220239 < FormSection label = "Published Date" description = "This date should be the Published Date of the blog" withAsteriskOnTitle >
@@ -232,6 +251,7 @@ function BlogForm() {
232251 value = { value . author }
233252 onChange = { setFieldValue }
234253 error = { error ?. author as string }
254+ placeholder = "author"
235255 />
236256 </ FormSection >
237257 < FormSection label = "Cover photo" description = "Add a cover photo, which will be displayed on top" withAsteriskOnTitle >
@@ -240,6 +260,7 @@ function BlogForm() {
240260 onChange = { ( files ) => setFieldValue ( files , 'coverImage' ) }
241261 accept = "audio/*"
242262 value = { value . coverImage }
263+ error = { error ?. coverImage as string }
243264 />
244265 </ FormSection >
245266 < FormSection label = "Featured" description = "Click on the checkbox if the blog is to be featured" withAsteriskOnTitle >
@@ -263,16 +284,17 @@ function BlogForm() {
263284 error = { error ?. status }
264285 />
265286 </ FormSection >
266- { value . slug && (
287+ < Activity mode = { value . slug ? 'visible' : 'hidden' } >
267288 < FormSection label = "Slug" description = "Unique URL identifier for the blog" >
268289 < TextInput
269290 name = "slug"
270291 value = { value . slug ?? '' }
271292 onChange = { ( val ) => setFieldValue ( val || null , 'slug' ) }
272293 error = { error ?. slug }
294+ readOnly
273295 />
274296 </ FormSection >
275- ) }
297+ </ Activity >
276298 < FormSection label = "Strategic Directive (NS)" description = "Select under which strategic directive it belongs" >
277299 < SelectInput
278300 name = "directive"
@@ -302,6 +324,7 @@ function BlogForm() {
302324 < RichTextEditor
303325 value = { value . content }
304326 onChange = { ( val ) => setFieldValue ( val , 'content' ) }
327+ error = { error ?. content }
305328 />
306329 </ FormSection >
307330 < FormSection >
0 commit comments