@@ -10,7 +10,12 @@ import {
1010} from '@mui/material' ;
1111import Autocomplete from '@mui/material/Autocomplete' ;
1212
13- import { resolve } from '../../api/api.js' ;
13+ import {
14+ getCertifications ,
15+ createCertification ,
16+ updateCertification ,
17+ mergeCertification ,
18+ } from '../../api/certification' ;
1419import { AppContext } from '../../context/AppContext' ;
1520import { selectCsrfToken } from '../../context/selectors' ;
1621import ConfirmationDialog from '../dialogs/ConfirmationDialog' ;
@@ -33,6 +38,7 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
3338 const [ confirmDeleteOpen , setConfirmDeleteOpen ] = useState ( false ) ;
3439 const [ mergeDialogOpen , setMergeDialogOpen ] = useState ( false ) ;
3540 const [ name , setName ] = useState ( '' ) ;
41+ const [ description , setDescription ] = useState ( '' ) ;
3642 const [ selectedCertification , setSelectedCertification ] = useState ( null ) ;
3743 const [ selectedTarget , setSelectedTarget ] = useState ( null ) ;
3844
@@ -48,20 +54,13 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
4854 setAdding ( true ) ;
4955 setBadgeUrl ( '' ) ;
5056 setName ( '' ) ;
57+ setDescription ( '' ) ;
5158 setSelectedCertification ( null ) ;
5259 setSelectedTarget ( null ) ;
5360 } ;
5461
5562 const loadCertifications = useCallback ( async ( ) => {
56- const res = await resolve ( {
57- method : 'GET' ,
58- url : certificationBaseUrl ,
59- headers : {
60- 'X-CSRF-Header' : csrf ,
61- Accept : 'application/json' ,
62- 'Content-Type' : 'application/json;charset=UTF-8'
63- }
64- } ) ;
63+ const res = await getCertifications ( csrf ) ;
6564 if ( res . error ) return ;
6665
6766 const certs = await res . payload . data ;
@@ -78,8 +77,11 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
7877 if ( csrf ) loadCertifications ( ) ;
7978 } , [ csrf ] ) ;
8079
80+ // `exclude` is optional. If supplied, the certification name that matches
81+ // will be removed from the list of options. This is useful when merging
82+ // certifications.
8183 const certificationSelect = useCallback (
82- ( label , setSelected ) => (
84+ ( label , setSelected , exclude ) => (
8385 < Autocomplete
8486 blurOnSelect
8587 clearOnBlur
@@ -97,10 +99,11 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
9799 }
98100 setAdding ( ! Boolean ( foundCert ) ) ;
99101 setName ( foundCert . name ) ;
102+ setDescription ( foundCert . description ) ;
100103 setBadgeUrl ( foundCert . badgeUrl ) ;
101104 setSelected ( foundCert ) ;
102105 } }
103- options = { certifications . map ( cert => cert . name ) }
106+ options = { certifications . map ( cert => cert . name ) . filter ( name => name !== exclude ) }
104107 renderInput = { params => {
105108 return (
106109 < TextField
@@ -120,16 +123,7 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
120123 const deleteCertification = useCallback ( async ( ) => {
121124 selectedCertification . active = false ;
122125 const { id } = selectedCertification ;
123- const res = await resolve ( {
124- method : 'PUT' ,
125- url : certificationBaseUrl + '/' + id ,
126- headers : {
127- 'X-CSRF-Header' : csrf ,
128- Accept : 'application/json' ,
129- 'Content-Type' : 'application/json;charset=UTF-8'
130- } ,
131- data : selectedCertification
132- } ) ;
126+ const res = await updateCertification ( id , selectedCertification , csrf ) ;
133127 if ( res . error ) return ;
134128
135129 setCertificationMap ( map => {
@@ -141,19 +135,10 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
141135 close ( ) ;
142136 } , [ certificationMap , certifications , selectedCertification ] ) ;
143137
144- const mergeCertification = useCallback ( async ( ) => {
138+ const mergeSelectedCertification = useCallback ( async ( ) => {
145139 const sourceId = selectedCertification . id ;
146140 const targetId = selectedTarget . id ;
147- const res = await resolve ( {
148- method : 'POST' ,
149- url : certificationBaseUrl + '/merge' ,
150- headers : {
151- 'X-CSRF-Header' : csrf ,
152- Accept : 'application/json' ,
153- 'Content-Type' : 'application/json;charset=UTF-8'
154- } ,
155- data : { sourceId, targetId }
156- } ) ;
141+ const res = await mergeCertification ( sourceId , targetId , csrf ) ;
157142 if ( res . error ) return ;
158143
159144 setCertifications ( certs => certs . filter ( cert => cert . id !== sourceId ) ) ;
@@ -166,19 +151,13 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
166151 } , [ selectedCertification , selectedTarget ] ) ;
167152
168153 const saveCertification = useCallback ( async ( ) => {
169- const url = adding
170- ? certificationBaseUrl
171- : certificationBaseUrl + '/' + selectedCertification . id ;
172- const res = await resolve ( {
173- method : adding ? 'POST' : 'PUT' ,
174- url,
175- headers : {
176- 'X-CSRF-Header' : csrf ,
177- Accept : 'application/json' ,
178- 'Content-Type' : 'application/json;charset=UTF-8'
179- } ,
180- data : { name, badgeUrl }
181- } ) ;
154+ let res ;
155+ const data = { name, description, badgeUrl } ;
156+ if ( adding ) {
157+ res = await createCertification ( data , csrf ) ;
158+ } else {
159+ res = await updateCertification ( selectedCertification . id , data , csrf ) ;
160+ }
182161 if ( res . error ) return ;
183162
184163 const newCert = res . payload . data ;
@@ -191,7 +170,7 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
191170 ) ;
192171 close ( ) ;
193172 forceUpdate ( ) ;
194- } , [ badgeUrl , certificationMap , name , selectedCertification ] ) ;
173+ } , [ badgeUrl , certificationMap , name , description , selectedCertification ] ) ;
195174
196175 return (
197176 < >
@@ -209,6 +188,12 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
209188 onChange = { e => setName ( e . target . value ) }
210189 value = { name }
211190 />
191+ < TextField
192+ label = "Description"
193+ required
194+ onChange = { e => setDescription ( e . target . value ) }
195+ value = { description }
196+ />
212197 < TextField
213198 label = "Badge URL"
214199 required
@@ -253,9 +238,10 @@ const Certifications = ({ forceUpdate = () => {}, open, onClose }) => {
253238 Merge { selectedCertification ?. name } Certification Into
254239 </ DialogTitle >
255240 < DialogContent >
256- { certificationSelect ( 'Target Certification' , setSelectedTarget ) }
241+ { certificationSelect ( 'Target Certification' ,
242+ setSelectedTarget , selectedCertification ?. name ) }
257243 < div className = "row" >
258- < Button disabled = { ! selectedTarget } onClick = { mergeCertification } >
244+ < Button disabled = { ! selectedTarget } onClick = { mergeSelectedCertification } >
259245 Merge
260246 </ Button >
261247 < Button
0 commit comments