forked from dedis/d-voting
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
148 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { FC, useContext, useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import { Dialog } from '@headlessui/react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { ENDPOINT_LOGOUT } from '../../components/utils/Endpoints'; | ||
import { AuthContext, FlashContext, FlashLevel } from '../../index'; | ||
import handleLogin from '../../pages/session/HandleLogin'; | ||
|
||
type ChangeIdModalProps = { | ||
isShown: boolean; | ||
setIsShown: (isShown: boolean) => void; | ||
}; | ||
|
||
const logout = async (t, authCtx, fctx, navigate) => { | ||
const opts = { method: 'POST' }; | ||
|
||
const res = await fetch(ENDPOINT_LOGOUT, opts); | ||
if (res.status !== 200) { | ||
fctx.addMessage(t('logOutError', { error: res.statusText }), FlashLevel.Error); | ||
} else { | ||
fctx.addMessage(t('logOutSuccessful'), FlashLevel.Info); | ||
} | ||
// TODO: should be a setAuth function passed to AuthContext rather than | ||
// changing the state directly | ||
authCtx.isLogged = false; | ||
authCtx.firstName = undefined; | ||
authCtx.lastName = undefined; | ||
navigate('/'); | ||
}; | ||
|
||
const ChangeIdModal: FC<ChangeIdModalProps> = ({ isShown, setIsShown }) => { | ||
const { t } = useTranslation(); | ||
const [newId, setNewId] = useState(''); | ||
const authCtx = useContext(AuthContext); | ||
|
||
const navigate = useNavigate(); | ||
|
||
const fctx = useContext(FlashContext); | ||
|
||
async function changeId() { | ||
logout(t, authCtx, fctx, navigate).then(() => { | ||
handleLogin(fctx, newId).catch(console.error); | ||
}); | ||
} | ||
|
||
if (isShown) { | ||
return ( | ||
<Dialog open={isShown} onClose={() => {}}> | ||
<Dialog.Overlay className="fixed inset-0 bg-black opacity-30" /> | ||
<div className="fixed inset-0 flex items-center justify-center"> | ||
<div className="bg-white content-center rounded-lg shadow-lg p-3 w-80"> | ||
<Dialog.Title as="h3" className="text-lg font-medium leading-6 text-gray-900"> | ||
{t('changeIdTitle')} | ||
</Dialog.Title> | ||
<Dialog.Description className="mt-2 mx-auto text-sm text-center text-gray-500"> | ||
{t('changeIdDialog')} | ||
</Dialog.Description> | ||
<div className="mt-4 sm:mt-6 sm:grid sm:grid-cols-2 sm:gap-3 sm:grid-flow-row-dense"> | ||
<input | ||
onChange={(e) => setNewId(e.target.value)} | ||
type="number" | ||
placeholder={t('changeIdPlaceholder')} | ||
/> | ||
<button | ||
type="button" | ||
className="inline-flex justify-center px-4 py-2 text-sm font-medium text-white bg-indigo-500 border border-transparent rounded-md hover:bg-gray-300 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-indigo-500" | ||
onClick={() => setIsShown(false)}> | ||
{t('cancel')} | ||
</button> | ||
<button | ||
type="button" | ||
className="inline-flex justify-center px-4 py-2 text-sm font-medium text-white bg-red-600 border border-transparent rounded-md hover:bg-red-700 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-red-500" | ||
onClick={() => { | ||
setIsShown(false); | ||
changeId(); | ||
}}> | ||
{t('changeIdContinue')} | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
</Dialog> | ||
); | ||
} else { | ||
return <></>; | ||
} | ||
}; | ||
|
||
export default ChangeIdModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters