@@ -7,15 +7,22 @@ import { useStateValue } from "./context/state";
77import LinkCard from "./linkcard" ;
88
99import styles from "../styles/form.module.css" ;
10+ import { useForm } from "react-hook-form" ;
1011
1112const endpoint =
1213 process . env . NODE_ENV === "production" ? `` : "http://localhost:3000" ;
1314
1415const LinksForm = ( { pagedataid } ) => {
1516 const [ { links } , dispatch ] = useStateValue ( ) ;
1617 const [ loading , setloading ] = useState ( false ) ;
18+ const [ commonSettingsCollapse , setcommonSettingsCollapse ] = useState ( false ) ;
1719 const [ isNewLinkInList , setisNewLinkInList ] = useState ( false ) ;
1820
21+ const {
22+ register,
23+ handleSubmit,
24+ formState : { errors, dirtyFields } ,
25+ } = useForm ( ) ;
1926 const addNewLink = ( ) => {
2027 // console.log(links.length);
2128 // console.log(links[links.length - 1]);
@@ -185,6 +192,66 @@ const LinksForm = ({ pagedataid }) => {
185192 }
186193 } ;
187194
195+ const updateCommonSettings = async ( data ) => {
196+ // console.log(data);
197+ // console.log(dirtyFields);
198+ // only get user changed data
199+
200+ if ( Object . keys ( dirtyFields ) . length === 0 ) {
201+ return ;
202+ }
203+
204+ Object . keys ( data ) . forEach ( ( element ) => {
205+ if ( ! dirtyFields [ element ] ) {
206+ delete data [ element ] ;
207+ }
208+ } ) ;
209+
210+ console . log ( data ) ;
211+
212+ let confirm = await Swal . fire ( {
213+ title : "Apply common settings" ,
214+ text : "All your links will apply these common settings" ,
215+ icon : "warning" ,
216+ showCancelButton : true ,
217+ confirmButtonColor : "#3085d6" ,
218+ cancelButtonColor : "#d33" ,
219+ confirmButtonText : "Yes, apply it!" ,
220+ } ) ;
221+
222+ if ( ! confirm . isConfirmed ) {
223+ return ;
224+ }
225+
226+ setloading ( true ) ;
227+
228+ try {
229+ let res = await fetch ( `${ endpoint } /api/pages/applycommonsettigns` , {
230+ method : "POST" ,
231+ body : JSON . stringify ( data ) ,
232+ headers : { "Content-Type" : "application/json" } ,
233+ } ) . then ( ( res ) => res . json ( ) ) ;
234+
235+ console . log ( res ) ;
236+
237+ if ( ! res . success ) {
238+ toast . error ( `Error ${ res . message } ` , { autoClose : 5000 } ) ;
239+ setloading ( false ) ;
240+ return ;
241+ }
242+
243+ dispatch ( { type : "updateLink" , linkdata : res . updatedLinkData } ) ;
244+
245+ toast . success ( `Successfully applied common settings` , {
246+ autoClose : 1000 ,
247+ } ) ;
248+ } catch ( error ) {
249+ console . log ( error ) ;
250+ toast . error ( `Error : ${ error . message } ` , { autoClose : 5000 } ) ;
251+ }
252+ setloading ( false ) ;
253+ } ;
254+
188255 return (
189256 < >
190257 < div className = { styles . Wrapper } >
@@ -210,6 +277,107 @@ const LinksForm = ({ pagedataid }) => {
210277 >
211278 Add new link
212279 </ button >
280+ < div >
281+ < div className = "mt-3 py-2" >
282+ < div className = "accordion" >
283+ < div className = "accordion-item" >
284+ < h2 className = "accordion-header" id = "headingOne" >
285+ < button
286+ className = { `accordion-button ${
287+ commonSettingsCollapse ? "collapsed" : ""
288+ } `}
289+ type = "button"
290+ onClick = { ( e ) => {
291+ setcommonSettingsCollapse ( ! commonSettingsCollapse ) ;
292+ } }
293+ >
294+ Common Settings
295+ </ button >
296+ </ h2 > { " " }
297+ < div
298+ className = { `accordion-collapse collapse ${
299+ commonSettingsCollapse ? "show" : ""
300+ } `}
301+ >
302+ < div className = "accordion-body" >
303+ < form onSubmit = { handleSubmit ( updateCommonSettings ) } >
304+ { " " }
305+ < div className = "mb-3 " >
306+ < div className = "input-group mb-3" >
307+ < input
308+ type = "number"
309+ className = {
310+ errors . borderRadius
311+ ? "form-control is-invalid"
312+ : "form-control"
313+ }
314+ placeholder = "Enter Border Radius"
315+ { ...register ( "borderRadius" , {
316+ min : {
317+ message : "Border Radius must be above 0px" ,
318+ value : 0 ,
319+ } ,
320+ } ) }
321+ />
322+ < span className = "input-group-text" > px</ span >
323+ { errors . borderRadius && (
324+ < div className = "invalid-feedback" >
325+ { errors . borderRadius . message }
326+ </ div >
327+ ) }
328+ </ div >
329+ </ div >
330+ < div className = "row" >
331+ < div className = "col" >
332+ < div className = "mb-1 small " >
333+ < label className = "form-label small " >
334+ Link Display Text Font color
335+ </ label >
336+ < input
337+ type = "color"
338+ className = "form-control form-control-sm mb-2 form-control-color"
339+ title = "Choose Link text color"
340+ placeholder = "Choose Link text color"
341+ { ...register ( "textColor" ) }
342+ />
343+ </ div >
344+ </ div >
345+ < div className = "col" >
346+ < div className = "mb-1 small " >
347+ < label className = "form-label small" >
348+ Link background color
349+ </ label >
350+ < input
351+ type = "color"
352+ className = "form-control form-control-sm mb-2 form-control-color"
353+ title = "Choose Link background color"
354+ placeholder = "Choose Link background color"
355+ { ...register ( "bgColor" , { required : "" } ) }
356+ />
357+ </ div >
358+ </ div >
359+ </ div > { " " }
360+ < button
361+ type = "submit"
362+ className = "btn btn-primary btn-block"
363+ disabled = { loading }
364+ >
365+ { loading && (
366+ < span
367+ className = "spinner-border spinner-border-sm me-1"
368+ role = "status"
369+ aria-hidden = "true"
370+ > </ span >
371+ ) }
372+ Save
373+ </ button >
374+ </ form >
375+ </ div >
376+ </ div >
377+ </ div >
378+ </ div >
379+ </ div >
380+ </ div >
213381 < DragDropContext onDragEnd = { dragEndHnadler } >
214382 < Droppable droppableId = "links" isDropDisabled = { isNewLinkInList } >
215383 { ( provided ) => (
0 commit comments