Skip to content

Commit

Permalink
Merge pull request #172 from SocialGouv/feat-contact-whatsapp
Browse files Browse the repository at this point in the history
feat: Add chat with whatsapp
  • Loading branch information
benguedj authored May 13, 2024
2 parents 99a6408 + a569cbd commit be914b1
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
3 changes: 3 additions & 0 deletions apollo-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ export const GET_ACTIVATION_TILE_STATUS = gql`
query activationTile {
activationTile {
activation_tile
rdv
sms
whatsapp
}
}
`
Expand Down
2 changes: 2 additions & 0 deletions pages/contact/contact-confirmed.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export default function ContactConfirmed() {
"Vous devriez recevoir un email de notre équipe dans les 48h. Pensez bien à vérifier dans vos spams."}
{contactType === RequestContact.type.sms &&
"Vous devriez recevoir un SMS de notre équipe dans les 48h en fonction des disponibilités sélectionnées."}
{contactType === RequestContact.type.whatsapp &&
"Vous devriez recevoir un message de notre équipe dans les 48h."}
{contactType === RequestContact.type.rendezvous &&
"Vous serez contacté sur le numéro de téléphone que vous avez communiqué."}
</p>
Expand Down
94 changes: 79 additions & 15 deletions pages/contact/to-be-contacted.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { ContactForm } from "../../src/components/contact/ContactForm"
import * as StorageUtils from "../../src/utils/storage.utils"
import * as TrackerUtils from "../../src/utils/tracker.utils"
import * as ContactUtils from "../../src/utils/contact.utils"
import { useQuery } from "@apollo/client"
import { client, GET_ACTIVATION_TILE_STATUS } from "../../apollo-client"

export default function ToBeContacted() {
const router = useRouter()
Expand All @@ -36,6 +38,7 @@ export default function ToBeContacted() {
const [contactHours, setContactHours] = useState(defaultContactHours)
const [itemValueType, setItemValueType] = useState()
const [isSmsSelected, setSmsSelected] = useState(false)
const [isWhatsappSelected, setWhatsappSelected] = useState(false)
const [isPhoneValid, setIsPhoneValide] = useState(false)
const [websiteSource, setWebsiteSource] = useState(false)
const [canSend, setCanSend] = useState(false)
Expand All @@ -55,6 +58,8 @@ export default function ToBeContacted() {

useEffect(() => {
setSmsSelected(itemValueType == RequestContact.type.sms)
setWhatsappSelected(itemValueType == RequestContact.type.whatsapp)
console.log(itemValueType)
}, [itemValueType])

const cancel = () => {
Expand All @@ -74,9 +79,9 @@ export default function ToBeContacted() {
})
}

const onClickSelector = () => {
const onClickSelector = (type) => {
TrackerUtils.trackerForContact("Choix effectué")
TrackerUtils.trackerForContact("Choix sms")
TrackerUtils.trackerForContact("Choix " + type.id)
}

const sendTrackerContactType = (typeContact) => {
Expand All @@ -92,6 +97,10 @@ export default function ToBeContacted() {
if (itemValueType === RequestContact.type.sms) {
sendTrackerContactType(itemValueType)
goToContactValidation("/contact/contact-confirmed")
}
if (itemValueType === RequestContact.type.whatsapp) {
sendTrackerContactType(itemValueType)
goToContactValidation("/contact/contact-confirmed")
} else if (itemValueType === RequestContact.type.rendezvous) {
TrackerUtils.trackerForContact("Choix effectué")
TrackerUtils.trackerForContact("Choix rendezvous")
Expand All @@ -110,7 +119,7 @@ export default function ToBeContacted() {
checked={itemValueType === type.id}
onChange={(e) => {
setItemValueType(e.currentTarget.value)
onClickSelector()
onClickSelector(type)
}}
>
<Row className="card-center-img">
Expand All @@ -134,6 +143,16 @@ export default function ToBeContacted() {
</fieldset>
)

const WhatsappComponent = () => (
<fieldset>
<Row>
{defaultContactTypes.byWhatsapp.map((type) => (
<Col key={type.id}>{CustomToggleButton(type)}</Col>
))}
</Row>
</fieldset>
)

const CalendlyComponent = () => {
return (
<>
Expand All @@ -146,14 +165,28 @@ export default function ToBeContacted() {
)
}

const ButtonGroupType = () => (
<ButtonGroup className="be-contacted-button-group">
<Col>
<CalendlyComponent />
<SmsComponent />
</Col>
</ButtonGroup>
)
const ButtonGroupType = (params) => {
const _activationTile = params.activationTile
if (_activationTile) {
return (
<ButtonGroup className="be-contacted-button-group">
<Col>
{activationTile.rdv ? <CalendlyComponent /> : null}
{activationTile.sms ? <SmsComponent /> : null}
{activationTile.whatsapp ? <WhatsappComponent /> : null}
</Col>
</ButtonGroup>
)
}
return (
<ButtonGroup className="be-contacted-button-group">
<Col>
<CalendlyComponent />
<SmsComponent />
</Col>
</ButtonGroup>
)
}

const buttonGroupHours = () => (
<ToggleButtonGroup
Expand Down Expand Up @@ -200,6 +233,14 @@ export default function ToBeContacted() {
)
}

const { loading, error, data } = useQuery(GET_ACTIVATION_TILE_STATUS, {
client: client,
})

if (loading) return <></>
if (error) return <p>Error</p>
const activationTile = data.activationTile

return (
<ContentLayout>
<WidgetHeader title="Je veux être accompagné.e" locale={localeSelected} />
Expand All @@ -208,7 +249,7 @@ export default function ToBeContacted() {
imageUrl="/img/image-wanda.png"
/>
<p>Je préfére être contacté.e par :</p>
<ButtonGroupType />
<ButtonGroupType activationTile={activationTile} />

{isSmsSelected ? (
<>
Expand All @@ -225,7 +266,17 @@ export default function ToBeContacted() {
/>
</>
) : null}
{!isSmsSelected && (
{isWhatsappSelected ? (
<>
<ContactForm
contactType={itemValueType}
setPropsPhoneValid={setIsPhoneValide}
canSend={canSend}
contactHours={horaire}
/>
</>
) : null}
{!isSmsSelected && !isWhatsappSelected && (
<Col className="be-contacted-bottom-buttons">
{websiteSource !== OPEN_CONTACT_FROM_EMAIL && (
<button className="fr-btn fr-btn--secondary" onClick={cancel}>
Expand All @@ -249,6 +300,15 @@ export default function ToBeContacted() {
}

const defaultContactTypes = {
byWhatsapp: [
{
icon: "../img/contact/chat.svg",
iconSelected: "../img/contact/chat-selected.svg",
id: RequestContact.type.whatsapp,
isChecked: false,
text: "Whatsapp",
},
],
byAvailabilities: [
{
icon: "../img/contact/sms.svg",
Expand Down Expand Up @@ -317,12 +377,16 @@ export const isValidButtonEnabled = (itemValueType, contactHours, canSend) => {

return (
itemValueType == RequestContact.type.rendezvous ||
(itemValueType == RequestContact.type.sms && isHoursSelected && canSend)
(itemValueType == RequestContact.type.sms && isHoursSelected && canSend) ||
(itemValueType == RequestContact.type.whatsapp && canSend)
)
}

const isValidForm = (contactType, isPhoneValid) => {
if (contactType == RequestContact.type.sms) {
if (
contactType == RequestContact.type.sms ||
contactType == RequestContact.type.whatsapp
) {
return isPhoneValid
}
return false
Expand Down
11 changes: 9 additions & 2 deletions src/components/contact/ContactForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
STORAGE_SOURCE,
STORAGE_TEST_DEMOGRAPHIC_DPT_CODE,
STORAGE_TEST_DEMOGRAPHIC_DPT_LIBELLE,
URL_CHAT_WHATSAPP,
} from "../../constants/constants"
import { Form } from "../../constants/specificLabels"
import { useMutation } from "@apollo/client"
Expand Down Expand Up @@ -159,7 +160,8 @@ export const ContactForm = ({

return (
<>
{contactType === RequestContact.type.sms && (
{(contactType === RequestContact.type.sms ||
contactType === RequestContact.type.whatsapp) && (
<form className="contact-form" onSubmit={sendForm}>
<div className={`form-group fr-input-group`}>
<label htmlFor="inputName">Votre prénom :</label>
Expand All @@ -181,7 +183,12 @@ export const ContactForm = ({
className="fr-btn"
type="submit"
disabled={!canSend}
onClick={() => sendTrackerContactType(contactType)}
onClick={() => {
sendTrackerContactType(contactType)
if (contactType === RequestContact.type.whatsapp) {
window.open(URL_CHAT_WHATSAPP, "_blank")
}
}}
>
Valider
</button>
Expand Down
3 changes: 3 additions & 0 deletions src/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ export const OPEN_CONTACT_FROM_EMAIL = "fromEmail"

export const PATTERN_EMAIL = "[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+.[a-zA-Z.]{2,15}"

export const URL_CHAT_WHATSAPP = "https://wa.me/message/ZMCTRIQWA7OKD1"

export const RequestContact = {
type: {
sms: "sms",
rendezvous: "rendezvous",
whatsapp: "whatsapp",
},
hours: {
morning: "matin",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/contact.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const trackerContactName = (typeContact) => {
switch (typeContact) {
case RequestContact.type.sms:
return TrackerUtils.CONTACT_SENT.sms
case RequestContact.type.whatsapp:
return TrackerUtils.CONTACT_SENT.whatsapp
case RequestContact.type.rendezvous:
return TrackerUtils.CONTACT_SENT.rendezvous
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/tracker.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export const ACTION = {

export const CONTACT_SENT = {
sms: "Confirmation sms",
whatsapp: "Confirmation whatsapp",
rendezvous: "Confirmation rendezvous",
}

Expand Down

0 comments on commit be914b1

Please sign in to comment.