-
-
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.
feat: Improved workspace join process (#522)
Signed-off-by: Johannes Groß <mail@gross-johannes.de>
- Loading branch information
Showing
20 changed files
with
1,192 additions
and
218 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,129 @@ | ||
import { Field, Formik, FormikProps } from 'formik'; | ||
import React, { useContext } from 'react'; | ||
import { UserContext } from '../../lib/context/UserContextProvider'; | ||
import { ModalContext } from '../../lib/context/ModalContextProvider'; | ||
import { alertService } from '../../lib/alertService'; | ||
import { useRouter } from 'next/router'; | ||
|
||
interface AddWorkspaceJoinCodeModalProps { | ||
onCreated?: () => void; | ||
} | ||
|
||
export default function AddWorkspaceJoinCodeModal(props: AddWorkspaceJoinCodeModalProps) { | ||
const userContext = useContext(UserContext); | ||
const modalContext = useContext(ModalContext); | ||
|
||
const router = useRouter(); | ||
|
||
const { workspaceId } = router.query; | ||
|
||
const formRef = React.useRef< | ||
FormikProps<{ | ||
code: string; | ||
expires: string | undefined; | ||
onlyUseOnce: boolean; | ||
}> | ||
>(null); | ||
|
||
return ( | ||
<div className={'flex flex-col gap-2'}> | ||
<div className={'text-2xl font-bold'}>Einladungscode hinzufügen</div> | ||
<Formik | ||
innerRef={formRef} | ||
initialValues={{ | ||
code: 'abcdef', | ||
// code: Math.random().toString(36).slice(2, 8).toLowerCase(), | ||
expires: undefined, | ||
onlyUseOnce: false, | ||
}} | ||
onSubmit={async (values) => { | ||
try { | ||
const body = { | ||
code: values.code, | ||
expires: values.expires, | ||
onlyUseOnce: values.onlyUseOnce, | ||
}; | ||
|
||
const response = await fetch(`/api/workspaces/${workspaceId}/join-codes`, { | ||
method: 'POST', | ||
headers: { 'Content-Type': 'application/json' }, | ||
body: JSON.stringify(body), | ||
}); | ||
if (response.status.toString().startsWith('2')) { | ||
modalContext.closeAllModals(); | ||
props.onCreated?.(); | ||
alertService.success('Beitrittcode erstellt'); | ||
} else { | ||
formRef.current?.setFieldValue('code', Math.random().toString(36).slice(2, 8).toLowerCase()); | ||
alertService.error('Da hat etwas nicht funktioniert, probiere es mit diesem neu generierten Code erneut!', response.status, response.statusText); | ||
} | ||
} catch (error) { | ||
console.error('CocktailRatingModal -> onSubmit', error); | ||
alertService.error('Es ist ein Fehler aufgetreten'); | ||
} | ||
}} | ||
validate={(values) => { | ||
const errors: { [key: string]: string } = {}; | ||
if (values.code.length <= 5) { | ||
errors.code = `Der Code muss länger als 5 Zeichen sein ${values.code.length}`; | ||
} | ||
if (values.expires && new Date(values.expires) < new Date()) { | ||
errors.expires = 'Das Ablaufdatum muss in der Zukunft liegen'; | ||
} | ||
return errors; | ||
}} | ||
> | ||
{({ values, handleChange, handleSubmit, isSubmitting, errors, touched, handleBlur }) => ( | ||
<form onSubmit={handleSubmit} className={'flex flex-col gap-2'}> | ||
<div className={'flex flex-col gap-2'}> | ||
<div className={'form-control'}> | ||
<label className={'label'} htmlFor={'code'}> | ||
<div className={'label-text'}> | ||
Beitrittcode <span className={'italic'}>(unveränderbar)</span> | ||
</div> | ||
<div className={'label-text-alt text-error'}> | ||
<span>{errors.code && touched.code ? errors.code : ''}</span> | ||
</div> | ||
</label> | ||
<input id={'code'} name={'code'} value={values.code} disabled={true} className={`input input-bordered`} /> | ||
</div> | ||
<div className={'form-control'}> | ||
<label className={'label'}> | ||
<div className={'label-text'}>Ablaufdatum</div> | ||
<div className={'label-text-alt text-error'}> | ||
<span>{errors.expires && touched.expires ? errors.expires : ''}</span> | ||
</div> | ||
</label> | ||
<input id={'expires'} name={'expires'} type={'date'} value={values.expires} onChange={handleChange} className={`input input-bordered`} /> | ||
</div> | ||
<div className={'form-control'}> | ||
<label className={'label'}> | ||
<div className={'label-text'}>Einmal-Code</div> | ||
<div className={'label-text-alt text-error'}> | ||
<span>{errors.onlyUseOnce && touched.onlyUseOnce ? errors.onlyUseOnce : ''}</span> | ||
</div> | ||
</label> | ||
<Field type={'checkbox'} name={`onlyUseOnce`} onChange={handleChange} onBlur={handleBlur} className={'toggle toggle-primary'} /> | ||
</div> | ||
</div> | ||
<div className={'flex justify-end gap-2'}> | ||
<button | ||
className={'btn btn-outline btn-error'} | ||
type={'button'} | ||
onClick={() => { | ||
modalContext.closeAllModals(); | ||
}} | ||
> | ||
Abbrechen | ||
</button> | ||
<button className={'btn btn-primary'} type={'submit'}> | ||
{isSubmitting ? <span className={'spinner loading-spinner'} /> : <></>} | ||
Hinzufügen | ||
</button> | ||
</div> | ||
</form> | ||
)} | ||
</Formik> | ||
</div> | ||
); | ||
} |
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,17 @@ | ||
import { NextApiRequest, NextApiResponse } from 'next'; | ||
import { withAuthentication } from '../../../middleware/api/authenticationMiddleware'; | ||
import { User } from '@prisma/client'; | ||
import prisma from '../../../prisma/prisma'; | ||
|
||
export default withAuthentication(async (req: NextApiRequest, res: NextApiResponse, user: User) => { | ||
const openWorkspaceRequests = await prisma.workspaceJoinRequest.findMany({ | ||
where: { | ||
userId: user.id, | ||
}, | ||
include: { | ||
workspace: true, | ||
}, | ||
}); | ||
|
||
return res.json({ data: openWorkspaceRequests }); | ||
}); |
24 changes: 24 additions & 0 deletions
24
pages/api/workspaces/[workspaceId]/join-codes/[code]/index.ts
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,24 @@ | ||
import { withHttpMethods } from '../../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withWorkspacePermission } from '../../../../../../middleware/api/authenticationMiddleware'; | ||
import prisma from '../../../../../../prisma/prisma'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.DELETE]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
try { | ||
await prisma.workspaceJoinCode.delete({ | ||
where: { | ||
workspaceId_code: { | ||
workspaceId: workspace.id, | ||
code: req.query.code as string, | ||
}, | ||
}, | ||
}); | ||
return res.json({ data: 'ok' }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ msg: 'Error' }); | ||
} | ||
}), | ||
}); |
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,29 @@ | ||
import { withHttpMethods } from '../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withWorkspacePermission } from '../../../../../middleware/api/authenticationMiddleware'; | ||
import prisma from '../../../../../prisma/prisma'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.GET]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
const joinCodes = await prisma.workspaceJoinCode.findMany({ | ||
where: { workspaceId: workspace.id }, | ||
}); | ||
|
||
return res.json({ data: joinCodes }); | ||
}), | ||
[HTTPMethod.POST]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
const { code, expires, onlyUseOnce } = req.body; | ||
|
||
const joinCodes = await prisma.workspaceJoinCode.create({ | ||
data: { | ||
code: code, | ||
expires: expires ? new Date(expires).toISOString() : null, | ||
onlyUseOnce: onlyUseOnce, | ||
workspaceId: workspace.id, | ||
}, | ||
}); | ||
|
||
return res.json({ data: joinCodes }); | ||
}), | ||
}); |
35 changes: 35 additions & 0 deletions
35
pages/api/workspaces/[workspaceId]/join-requests/[userId]/accept.ts
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,35 @@ | ||
import { withHttpMethods } from '../../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withWorkspacePermission } from '../../../../../../middleware/api/authenticationMiddleware'; | ||
import prisma from '../../../../../../prisma/prisma'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.POST]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
try { | ||
await prisma.$transaction(async (transaction) => { | ||
await transaction.workspaceJoinRequest.delete({ | ||
where: { | ||
userId_workspaceId: { | ||
workspaceId: workspace.id, | ||
userId: req.query.userId as string, | ||
}, | ||
}, | ||
}); | ||
await transaction.workspaceUser.create({ | ||
data: { | ||
userId: req.query.userId as string, | ||
workspaceId: workspace.id, | ||
role: Role.USER, | ||
}, | ||
}); | ||
|
||
//TODO: Send notification to user | ||
}); | ||
return res.json({ data: 'ok' }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ msg: 'Error' }); | ||
} | ||
}), | ||
}); |
27 changes: 27 additions & 0 deletions
27
pages/api/workspaces/[workspaceId]/join-requests/[userId]/reject.ts
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,27 @@ | ||
import { withHttpMethods } from '../../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withWorkspacePermission } from '../../../../../../middleware/api/authenticationMiddleware'; | ||
import prisma from '../../../../../../prisma/prisma'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.POST]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
try { | ||
await prisma.$transaction(async (transaction) => { | ||
await transaction.workspaceJoinRequest.delete({ | ||
where: { | ||
userId_workspaceId: { | ||
workspaceId: workspace.id, | ||
userId: req.query.userId as string, | ||
}, | ||
}, | ||
}); | ||
//TODO: Send notification to user | ||
}); | ||
return res.json({ data: 'ok' }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ msg: 'Error' }); | ||
} | ||
}), | ||
}); |
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,37 @@ | ||
import { withHttpMethods } from '../../../../../middleware/api/handleMethods'; | ||
import HTTPMethod from 'http-method-enum'; | ||
import { withAuthentication, withWorkspacePermission } from '../../../../../middleware/api/authenticationMiddleware'; | ||
import prisma from '../../../../../prisma/prisma'; | ||
import { Role } from '@prisma/client'; | ||
|
||
export default withHttpMethods({ | ||
[HTTPMethod.GET]: withWorkspacePermission([Role.MANAGER], async (req, res, user, workspace) => { | ||
const joinRequests = await prisma.workspaceJoinRequest.findMany({ | ||
where: { workspaceId: workspace.id }, | ||
include: { | ||
user: true, | ||
}, | ||
}); | ||
|
||
return res.json({ data: joinRequests }); | ||
}), | ||
[HTTPMethod.DELETE]: withAuthentication(async (req, res, user) => { | ||
try { | ||
await prisma.$transaction(async (transaction) => { | ||
const deleteResult = await transaction.workspaceJoinRequest.delete({ | ||
where: { | ||
userId_workspaceId: { | ||
workspaceId: req.query.workspaceId as string, | ||
userId: user.id as string, | ||
}, | ||
}, | ||
}); | ||
return res.json({ data: deleteResult }); | ||
}); | ||
return res.status(500).json({ msg: 'Error' }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ msg: 'Error' }); | ||
} | ||
}), | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.