Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ListItemContact): Add default actions
call, sms, email, modify and view in contacts app
- Loading branch information
Showing
11 changed files
with
248 additions
and
16 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
29 changes: 29 additions & 0 deletions
29
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/call.js
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 React from 'react' | ||
|
||
import { useI18n } from '../../../../../I18n' | ||
import TelephoneIcon from '../../../../../Icons/Telephone' | ||
import withListItemLocales from '../../../hoc/withListItemLocales' | ||
import ActionMenuItemWrapper from '../ActionMenuItemWrapper' | ||
|
||
export const call = () => { | ||
return { | ||
name: 'call', | ||
action: docs => { | ||
const phoneNumber = docs?.[0]?.phone?.[0]?.number | ||
!!phoneNumber && window.open(`tel:${phoneNumber}`, '_self') | ||
}, | ||
Component: withListItemLocales(({ className, onClick }) => { | ||
const { t } = useI18n() | ||
|
||
return ( | ||
<ActionMenuItemWrapper | ||
className={className} | ||
icon={TelephoneIcon} | ||
onClick={onClick} | ||
> | ||
{t('ListItem.actions.call')} | ||
</ActionMenuItemWrapper> | ||
) | ||
}) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/emailTo.js
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 React from 'react' | ||
|
||
import { useI18n } from '../../../../../I18n' | ||
import EmailIcon from '../../../../../Icons/Email' | ||
import withListItemLocales from '../../../hoc/withListItemLocales' | ||
import ActionMenuItemWrapper from '../ActionMenuItemWrapper' | ||
|
||
export const emailTo = () => { | ||
return { | ||
name: 'emailTo', | ||
action: docs => { | ||
const emailAddress = docs?.[0]?.email?.[0]?.address | ||
!!emailAddress && window.open(`mailto:${emailAddress}`, '_self') | ||
}, | ||
Component: withListItemLocales(({ className, onClick }) => { | ||
const { t } = useI18n() | ||
|
||
return ( | ||
<ActionMenuItemWrapper | ||
className={className} | ||
icon={EmailIcon} | ||
onClick={onClick} | ||
> | ||
{t('ListItem.actions.emailTo')} | ||
</ActionMenuItemWrapper> | ||
) | ||
}) | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/hr.js
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,10 @@ | ||
import React from 'react' | ||
|
||
export const hr = () => { | ||
return { | ||
name: 'hr', | ||
Component: function hr() { | ||
return <hr /> | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/modify.js
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 React from 'react' | ||
|
||
import { generateWebLink, useClient } from 'cozy-client' | ||
|
||
import Link from '../../../../../Link' | ||
import { useI18n } from '../../../../../I18n' | ||
import PenIcon from '../../../../../Icons/Pen' | ||
import withListItemLocales from '../../../hoc/withListItemLocales' | ||
import ActionMenuItemWrapper from '../ActionMenuItemWrapper' | ||
|
||
export const modify = () => { | ||
return { | ||
name: 'modify', | ||
Component: withListItemLocales(({ className, docs, onClick }) => { | ||
const { t } = useI18n() | ||
const client = useClient() | ||
|
||
const contactId = docs[0]._id | ||
|
||
const webLink = generateWebLink({ | ||
slug: 'contacts', | ||
cozyUrl: client.getStackClient().uri, | ||
subDomainType: client.getInstanceOptions().subdomain, | ||
pathname: '/', | ||
hash: `/${contactId}/edit` | ||
}) | ||
|
||
return ( | ||
<ActionMenuItemWrapper | ||
className={className} | ||
icon={PenIcon} | ||
onClick={onClick} | ||
> | ||
<Link className="u-p-0" href={webLink} target="_blank"> | ||
{t('ListItem.actions.modify')} | ||
</Link> | ||
</ActionMenuItemWrapper> | ||
) | ||
}) | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/smsTo.js
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 React from 'react' | ||
|
||
import { useI18n } from '../../../../../I18n' | ||
import CommentIcon from '../../../../../Icons/Comment' | ||
import withListItemLocales from '../../../hoc/withListItemLocales' | ||
import ActionMenuItemWrapper from '../ActionMenuItemWrapper' | ||
|
||
export const smsTo = () => { | ||
return { | ||
name: 'smsTo', | ||
action: docs => { | ||
const phoneNumber = docs?.[0]?.phone?.[0]?.number | ||
!!phoneNumber && window.open(`sms:${phoneNumber}`, '_self') | ||
}, | ||
Component: withListItemLocales(({ className, onClick }) => { | ||
const { t } = useI18n() | ||
|
||
return ( | ||
<ActionMenuItemWrapper | ||
className={className} | ||
icon={CommentIcon} | ||
onClick={onClick} | ||
> | ||
{t('ListItem.actions.smsTo')} | ||
</ActionMenuItemWrapper> | ||
) | ||
}) | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
react/MuiCozyTheme/ListItem/ListItemBase/ActionsMenu/Actions/viewInContacts.js
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 React from 'react' | ||
|
||
import { generateWebLink, useClient } from 'cozy-client' | ||
|
||
import Link from '../../../../../Link' | ||
import { useI18n } from '../../../../../I18n' | ||
import OpenappIcon from '../../../../../Icons/Openapp' | ||
import withListItemLocales from '../../../hoc/withListItemLocales' | ||
import ActionMenuItemWrapper from '../ActionMenuItemWrapper' | ||
|
||
export const viewInContacts = () => { | ||
return { | ||
name: 'viewInContacts', | ||
Component: withListItemLocales(({ className, docs, onClick }) => { | ||
const { t } = useI18n() | ||
const client = useClient() | ||
|
||
const contactId = docs[0]._id | ||
|
||
const webLink = generateWebLink({ | ||
slug: 'contacts', | ||
cozyUrl: client.getStackClient().uri, | ||
subDomainType: client.getInstanceOptions().subdomain, | ||
pathname: '/', | ||
hash: `/${contactId}` | ||
}) | ||
|
||
return ( | ||
<ActionMenuItemWrapper | ||
className={className} | ||
icon={OpenappIcon} | ||
onClick={onClick} | ||
> | ||
<Link className="u-p-0" href={webLink} target="_blank"> | ||
{t('ListItem.actions.viewInContacts')} | ||
</Link> | ||
</ActionMenuItemWrapper> | ||
) | ||
}) | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
react/MuiCozyTheme/ListItem/ListItemContact/useActions.jsx
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,39 @@ | ||
import { useMemo } from 'react' | ||
|
||
import { makeActions } from '../ListItemBase/ActionsMenu/helpers' | ||
import { hr } from '../ListItemBase/ActionsMenu/Actions/hr' | ||
import { emailTo } from '../ListItemBase/ActionsMenu/Actions/emailTo' | ||
import { smsTo } from '../ListItemBase/ActionsMenu/Actions/smsTo' | ||
import { call } from '../ListItemBase/ActionsMenu/Actions/call' | ||
import { modify } from '../ListItemBase/ActionsMenu/Actions/modify' | ||
import { viewInContacts } from '../ListItemBase/ActionsMenu/Actions/viewInContacts' | ||
|
||
const makeOptionalActions = contact => { | ||
const optionalActions = [hr, call, smsTo, emailTo] | ||
|
||
const hasPhoneAction = contact.phone?.length > 0 | ||
const hasEmailAction = contact.email?.length > 0 | ||
const hasMessageActions = hasPhoneAction || hasEmailAction | ||
|
||
const conditions = { | ||
hr: hasMessageActions, | ||
call: hasPhoneAction, | ||
smsTo: hasPhoneAction, | ||
emailTo: hasEmailAction | ||
} | ||
|
||
return optionalActions.filter(action => conditions[action.name]) | ||
} | ||
|
||
const useActions = contact => { | ||
const optionalActions = useMemo(() => makeOptionalActions(contact), [contact]) | ||
const finalActions = useMemo( | ||
() => [modify, viewInContacts].concat(optionalActions), | ||
[optionalActions] | ||
) | ||
const actions = useMemo(() => makeActions(finalActions), [finalActions]) | ||
|
||
return actions | ||
} | ||
|
||
export default useActions |
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