-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from ReseauEntourage/feature/EN-6995-Signaler…
…-un-utilisateur [EN-6995] Signaler un utilisateur
- Loading branch information
Showing
22 changed files
with
409 additions
and
4 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
48 changes: 48 additions & 0 deletions
48
src/components/backoffice/profile/ProfileReportUserModal/ProfileReportUserModal.tsx
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,48 @@ | ||
import React, { useCallback, useMemo } from 'react'; | ||
import { useOnReportUserFormSubmit } from '../useOnReportUserFormSubmit'; | ||
import { Api } from 'src/api'; | ||
import { UserReportDto } from 'src/api/types'; | ||
import { formReportUser } from 'src/components/forms/schemas/formReportUser'; | ||
import { ModalEdit } from 'src/components/modals/Modal/ModalGeneric/ModalEdit'; | ||
import { Actions } from 'src/constants/utils'; | ||
|
||
interface ProfileReportUserModalProps { | ||
userId: string; | ||
} | ||
|
||
export const ProfileReportUserModal = ({ | ||
userId, | ||
}: ProfileReportUserModalProps) => { | ||
const { onSubmit } = useOnReportUserFormSubmit( | ||
async (userReportDto: UserReportDto) => { | ||
return Api.postProfileUserAbuse(userId, userReportDto); | ||
}, | ||
Actions.CREATE | ||
); | ||
|
||
const handleReportUserSubmit = useCallback( | ||
async (fields, closeModal) => { | ||
await onSubmit(fields, closeModal); | ||
}, | ||
[onSubmit] | ||
); | ||
|
||
const updateUserModalProps = useMemo(() => { | ||
return { | ||
formId: 'id', | ||
formSchema: formReportUser, | ||
title: 'Signaler un utilisateur', | ||
description: | ||
'Vous pouvez signaler un utilisateur si vous pensez qu’il ne respecte pas les règles de la plateforme.', | ||
submitText: 'Envoyer', | ||
cancelText: 'Annuler', | ||
onSubmit: handleReportUserSubmit, | ||
defaultValues: { | ||
reason: '', | ||
comment: '', | ||
}, | ||
}; | ||
}, [handleReportUserSubmit]); | ||
|
||
return <ModalEdit {...updateUserModalProps} />; | ||
}; |
46 changes: 46 additions & 0 deletions
46
src/components/backoffice/profile/useOnReportUserFormSubmit.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,46 @@ | ||
import { AxiosResponse } from 'axios'; | ||
import { useCallback } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { UserReportDto } from 'src/api/types'; | ||
import { ExtractFormSchemaValidation } from 'src/components/forms/FormSchema'; | ||
import { formReportUser } from 'src/components/forms/schemas/formReportUser'; | ||
import { Action, ActionsLabels } from 'src/constants/utils'; | ||
import { notificationsActions } from 'src/use-cases/notifications'; | ||
|
||
export function useOnReportUserFormSubmit( | ||
apiCall: (organization: UserReportDto) => Promise<AxiosResponse>, | ||
action: Action | ||
) { | ||
const dispatch = useDispatch(); | ||
const onSubmit = useCallback( | ||
async ( | ||
fields: ExtractFormSchemaValidation<typeof formReportUser>, | ||
closeModal | ||
) => { | ||
try { | ||
const { data } = await apiCall(fields); | ||
closeModal(); | ||
dispatch( | ||
notificationsActions.addNotification({ | ||
type: 'success', | ||
message: `Le signalement a bien été ${ActionsLabels[action].VERB}e`, | ||
}) | ||
); | ||
return data; | ||
} catch (error) { | ||
console.error(error); | ||
dispatch( | ||
notificationsActions.addNotification({ | ||
type: 'danger', | ||
message: `Une erreur s'est produite lors de la ${ActionsLabels[action].NAME} du signalement`, | ||
}) | ||
); | ||
} | ||
}, | ||
[action, apiCall, dispatch] | ||
); | ||
|
||
return { | ||
onSubmit, | ||
}; | ||
} |
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,26 @@ | ||
import { FormSchema } from '../FormSchema'; | ||
import { USER_REPORT_REASONS } from 'src/constants/users'; | ||
|
||
export const formReportUser: FormSchema<{ | ||
reason: string; | ||
comment: string; | ||
}> = { | ||
id: 'form-add-organization', | ||
fields: [ | ||
{ | ||
id: 'reason', | ||
name: 'reason', | ||
component: 'select-simple', | ||
title: 'Raison du signalement *', | ||
options: USER_REPORT_REASONS, | ||
isRequired: true, | ||
}, | ||
{ | ||
id: 'comment', | ||
name: 'comment', | ||
component: 'text-input', | ||
title: 'Commentaire *', | ||
isRequired: true, | ||
}, | ||
], | ||
}; |
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,7 @@ | ||
import React from 'react'; | ||
|
||
export const DropdownContext = React.createContext({ | ||
isOpen: false, | ||
toggleDropdown: () => {}, | ||
closeDropdown: () => {}, | ||
}); |
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,41 @@ | ||
import styled from 'styled-components'; | ||
import { COLORS } from 'src/constants/styles'; | ||
|
||
export const StyledDropdown = styled.div` | ||
position: relative; | ||
display: inline-block; | ||
`; | ||
|
||
export const StyledDropdownMenu = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 15px; | ||
position: absolute; | ||
top: calc(100% + 10px); | ||
background-color: white; | ||
border: 1px solid #ececec; | ||
border-radius: 20px; | ||
box-shadow: 0 4px 4px 0px #0000000d; | ||
padding: 25px; | ||
z-index: 1000; | ||
min-width: 200px; | ||
${({ openDirection }) => { | ||
return openDirection === 'right' ? 'left: 0;' : ''; | ||
}} | ||
${({ openDirection }) => { | ||
return openDirection === 'left' ? 'right: 0;' : ''; | ||
}} | ||
`; | ||
|
||
export const StyledDropdownMenuItem = styled.div` | ||
color: #484848; | ||
font-size: 16px; | ||
cursor: pointer; | ||
:hover { | ||
color: ${COLORS.primaryBlue}; | ||
} | ||
`; | ||
|
||
export const StyledDropdownMenuItemSeparator = styled.div` | ||
border-top: 1px solid #ececec; | ||
`; |
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,47 @@ | ||
import React, { useState, useRef, useEffect } from 'react'; | ||
import { DropdownContext } from './Dropdown.context'; | ||
import { StyledDropdown } from './Dropdown.styles'; | ||
import { DropdownItem } from './DropdownItem'; | ||
import { DropdownItemSeparator } from './DropdownItemSeparator'; | ||
import { DropdownMenu } from './DropdownMenu'; | ||
import { DropdownToggle } from './DropdownToggle'; | ||
|
||
type DropdownProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
const Dropdown = ({ children }: DropdownProps) => { | ||
const [isOpen, setIsOpen] = useState(false); | ||
const dropdownRef = useRef<HTMLDivElement>(null); | ||
|
||
const toggleDropdown = () => setIsOpen((prev) => !prev); | ||
const closeDropdown = () => setIsOpen(false); | ||
|
||
const handleClickOutside = (event) => { | ||
if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { | ||
closeDropdown(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener('mousedown', handleClickOutside); | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', handleClickOutside); | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
|
||
return ( | ||
<DropdownContext.Provider value={{ isOpen, toggleDropdown, closeDropdown }}> | ||
<StyledDropdown ref={dropdownRef}>{children}</StyledDropdown> | ||
</DropdownContext.Provider> | ||
); | ||
}; | ||
|
||
Dropdown.Toggle = DropdownToggle; | ||
Dropdown.Menu = DropdownMenu; | ||
Dropdown.ItemSeparator = DropdownItemSeparator; | ||
Dropdown.Item = DropdownItem; | ||
|
||
export { Dropdown }; |
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,23 @@ | ||
import React from 'react'; | ||
import { DropdownContext } from './Dropdown.context'; | ||
import { StyledDropdownMenuItem } from './Dropdown.styles'; | ||
|
||
type DropdownItemProps = { | ||
children: React.ReactNode; | ||
onClick?: () => void; | ||
}; | ||
|
||
export const DropdownItem = ({ children, onClick }: DropdownItemProps) => { | ||
const { closeDropdown } = React.useContext(DropdownContext); | ||
|
||
const handleClick = () => { | ||
if (onClick) onClick(); | ||
closeDropdown(); | ||
}; | ||
|
||
return ( | ||
<StyledDropdownMenuItem onClick={handleClick} className="dropdown-item"> | ||
{children} | ||
</StyledDropdownMenuItem> | ||
); | ||
}; |
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,6 @@ | ||
import React from 'react'; | ||
import { StyledDropdownMenuItemSeparator } from './Dropdown.styles'; | ||
|
||
export const DropdownItemSeparator = () => { | ||
return <StyledDropdownMenuItemSeparator />; | ||
}; |
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,25 @@ | ||
import React from 'react'; | ||
import { DropdownContext } from './Dropdown.context'; | ||
import { StyledDropdownMenu } from './Dropdown.styles'; | ||
|
||
type DropdownMenuProps = { | ||
children: React.ReactNode; | ||
openDirection?: 'left' | 'right'; | ||
}; | ||
|
||
export const DropdownMenu = ({ | ||
children, | ||
openDirection = 'right', | ||
}: DropdownMenuProps) => { | ||
const { isOpen } = React.useContext(DropdownContext); | ||
|
||
return ( | ||
<> | ||
{isOpen && ( | ||
<StyledDropdownMenu openDirection={openDirection}> | ||
{children} | ||
</StyledDropdownMenu> | ||
)} | ||
</> | ||
); | ||
}; |
Oops, something went wrong.