Skip to content

Commit

Permalink
fixs eslint
Browse files Browse the repository at this point in the history
  • Loading branch information
juanNH committed Jan 11, 2025
1 parent e08e9b6 commit 7d6432d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ const defaultForm: {
declare const urlCreateInstitution: string
declare const urlEditInstitution: string


interface Props {
institutionToEdit: Nullable<DjangoInstitution>,
handleResetInstitutionToEdit: (callbackToCancel: () => void) => void,
Expand All @@ -37,8 +36,10 @@ const InstitutionForm = (props: Props) => {
/**
* Handle form state data
* @param e event
* @param param1 key value to edit field in form
* @param param.name key name to edit field in form
* @param param.value key value to edit field in form
*/

const handleChange = (e, { name, value }) => {
setFormData({ ...formData, [name]: value })
}
Expand All @@ -48,20 +49,21 @@ const InstitutionForm = (props: Props) => {
*/
const handleSubmit = () => {
const myHeaders = getDjangoHeader()

if (props.institutionToEdit?.id) {
const jsonParams = {
"id": formData.id,
"name": formData.name,
"location": formData.location,
"email": formData.email,
"telephone_number": formData.telephone_number
id: formData.id,
name: formData.name,
location: formData.location,
email: formData.email,
telephone_number: formData.telephone_number
}
const editUrl = `${urlEditInstitution}/${formData.id}/`

ky.patch(editUrl, { headers: myHeaders, json: jsonParams }).then((response) => {
setFormData(prevState => ({ ...prevState, isLoading: true }))
response.json().then((jsonResponse: DjangoInstitution) => {
props.handleUpdateAlert(true, CustomAlertTypes.SUCCESS, `Institution ${jsonResponse.name} Updated!`, () => setFormData(defaultForm),true)
props.handleUpdateAlert(true, CustomAlertTypes.SUCCESS, `Institution ${jsonResponse.name} Updated!`, () => setFormData(defaultForm), true)
}).catch((err) => {
setFormData(prevState => ({ ...prevState, isLoading: false }))
props.handleUpdateAlert(true, CustomAlertTypes.ERROR, 'Error creating an institution!', () => setFormData(prevState => ({ ...prevState, isLoading: false })))
Expand All @@ -73,15 +75,14 @@ const InstitutionForm = (props: Props) => {
})
} else {
const jsonParams = {
"name": formData.name,
"location": formData.location,
"email": formData.email,
"telephone_number": formData.telephone_number
name: formData.name,
location: formData.location,
email: formData.email,
telephone_number: formData.telephone_number
}
ky.post(urlCreateInstitution, { headers: myHeaders, json: jsonParams }).then((response) => {
setFormData(prevState => ({ ...prevState, isLoading: true }))
response.json().then((jsonResponse: DjangoInstitution) => {
//setFormData(defaultForm)
props.handleUpdateAlert(true, CustomAlertTypes.SUCCESS, `Institution ${jsonResponse.name} created!`, () => setFormData(defaultForm))
}).catch((err) => {
props.handleUpdateAlert(true, CustomAlertTypes.ERROR, 'Error creating an institution!', () => setFormData(prevState => ({ ...prevState, isLoading: false })))
Expand All @@ -105,6 +106,7 @@ const InstitutionForm = (props: Props) => {
setFormData(defaultForm)
}
}

/**
* use effect to handle if a institution for edit is sent
*/
Expand All @@ -113,7 +115,7 @@ const InstitutionForm = (props: Props) => {
setFormData({
...props.institutionToEdit,
id: props.institutionToEdit?.id,
isLoading: false,
isLoading: false
})
}
}, [props.institutionToEdit])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ interface Props extends InstitutionModalState {
* @returns Component
*/
export const InstitutionModal = (props: Props) => {
const [userIdToAdd, setUserIdToAdd] = useState<number>(0);
const [userList, setUserList] = useState<SemanticListItem[]>([]);
const [userIdToAdd, setUserIdToAdd] = useState<number>(0)
const [userList, setUserList] = useState<SemanticListItem[]>([])
const abortController = useRef(new AbortController())
const currentUser = useContext(CurrentUserContext)

Expand All @@ -58,7 +58,7 @@ export const InstitutionModal = (props: Props) => {
institutionId: props.institution?.id
}
ky.post(urlAddRemoveUserToInstitution, { headers: myHeaders, signal: abortController.current.signal, json: body }).then((response) => {
response.json().then((jsonResponse: any) => {
response.json().then(() => {
usersListNonInInstitution()
}).catch((err) => {
console.error('Error parsing JSON ->', err)
Expand All @@ -81,7 +81,7 @@ export const InstitutionModal = (props: Props) => {
institutionId: props.institution?.id
}
ky.post(urlAddRemoveUserToInstitution, { headers: myHeaders, signal: abortController.current.signal, json: body }).then((response) => {
response.json().then((jsonResponse: any) => {
response.json().then(() => {
usersListNonInInstitution()
}).catch((err) => {
console.error('Error parsing JSON ->', err)
Expand All @@ -97,14 +97,11 @@ export const InstitutionModal = (props: Props) => {
* @param idInstitution institution id.
*/
const handleSwitchUserAdmin = (adminSwitched: boolean, idInstitution: number) => {

const myHeaders = getDjangoHeader()

const editUrl = `${urlEditInstitutionAdmin}/${idInstitution}/`


ky.patch(editUrl, { headers: myHeaders }).then((response) => {
response.json().then((jsonResponse: any) => {
response.json().then(() => {
props.handleUpdateAlert(true, CustomAlertTypes.SUCCESS, adminSwitched ? 'User is admin!' : 'User is not admin!', null)
}).catch((err) => {
props.handleUpdateAlert(true, CustomAlertTypes.ERROR, 'Error for switch role!', null)
Expand All @@ -114,7 +111,6 @@ export const InstitutionModal = (props: Props) => {
props.handleUpdateAlert(true, CustomAlertTypes.ERROR, 'Error for switch role!', null)
console.error('Error adding new Institution ->', err)
})

}

/**
Expand All @@ -134,19 +130,18 @@ export const InstitutionModal = (props: Props) => {
}).catch((err) => {
console.error('Error getting users ->', err)
})

}

useEffect(() => {
if (props.institution?.is_user_admin && props.institution?.id) {
usersListNonInInstitution()
}

return () => {
abortController.current.abort()
}
}, [props.institution?.id])


return (
<Modal
onClose={props.handleCloseModal}
Expand All @@ -172,14 +167,17 @@ export const InstitutionModal = (props: Props) => {
</Button>
<PaginatedTable<DjangoInstitutionUser>
headerTitle={props.institution?.name + ' users'}
headers={props.institution?.is_user_admin ? [
{ name: 'User name', serverCodeToSort: 'user__username' as any, width: 3 },
{ name: 'Admin', width: 1 },
{ name: 'Actions', width: 1 }
] : [
{ name: 'User name', serverCodeToSort: 'user__username' as any, width: 3 },
{ name: 'Admin', width: 1 },
]}
headers={props.institution?.is_user_admin
? [
{ name: 'User name', serverCodeToSort: 'user__username' as any, width: 3 },
{ name: 'Admin', width: 1 },
{ name: 'Actions', width: 1 }
]
: [
{ name: 'User name', serverCodeToSort: 'user__username' as any, width: 3 },
{ name: 'Admin', width: 1 }
]
}
showSearchInput
searchLabel='User name'
searchPlaceholder='Search by User name'
Expand All @@ -189,56 +187,60 @@ export const InstitutionModal = (props: Props) => {
return (
<Table.Row key={userCandidate.user.id}>
<TableCellWithTitle value={userCandidate.user.username} />
<Table.Cell value={userCandidate.is_institution_admin ? "true" : "false"}>
<Table.Cell value={userCandidate.is_institution_admin ? 'true' : 'false'}>
{
userCandidate.is_institution_admin ?
<Icon
name='check'
className={`clickable margin-left-5`}
color='teal'
title={'Institution admin'}
/>
:
<Icon
name='close'
className={`clickable margin-left-5`}
color='red'
title={'Non Institution admin'}
/>
userCandidate.is_institution_admin
? (
<Icon
name='check'
className='clickable margin-left-5'
color='teal'
title='Institution admin'
/>
)
: (
<Icon
name='close'
className='clickable margin-left-5'
color='red'
title='Non Institution admin'
/>
)
}
</Table.Cell>
{props.institution?.is_user_admin &&
<Table.Cell width={1}>
{/* Edit button */}
{
userCandidate.user.id !== currentUser?.id && (
userCandidate.is_institution_admin ?
userCandidate.user.id !== currentUser?.id && userCandidate.is_institution_admin
? (
<Icon
name='close'
className={`clickable margin-left-5`}
className='clickable margin-left-5'
color='olive'
title={'Switch to non Institution admin'}
title='Switch to non Institution admin'
onClick={() => props.handleChangeConfirmModalState(true, 'Manage admin', 'Are you sure to remove admin to user?', () => handleSwitchUserAdmin(false, userCandidate.id))}
/>
:
)
: (
<Icon
name='star'
className={`clickable margin-left-5`}
className='clickable margin-left-5'
color='teal'
title={'Switch to Institution admin'}
title='Switch to Institution admin'
onClick={() => props.handleChangeConfirmModalState(true, 'Manage admin', 'Are you sure to make user admin?', () => handleSwitchUserAdmin(true, userCandidate.id))}
/>
)
)
}

{
(props.institution?.is_user_admin && userCandidate.user.id !== currentUser?.id) &&
<Icon
name='close'
className={`clickable margin-left-5`}
className='clickable margin-left-5'
color='red'
onClick={() => props.handleChangeConfirmModalState(true, 'Remove user', 'Are you sure to remove user?', () => handleRemoveUser(userCandidate.user.id))}
title={'Remove user from Institution'}
title='Remove user from Institution'
/>
}
</Table.Cell>
Expand All @@ -249,6 +251,6 @@ export const InstitutionModal = (props: Props) => {
/>
</Segment>
</ModalContent>
</Modal >
</Modal>
)
}
Loading

0 comments on commit 7d6432d

Please sign in to comment.