# Gorgias

Gorgias integration for Shogun Frontend.

<table>
  <tbody>
    <tr>
      <td>⚠️</td>
      <td>This package runs on Shogun Frontend and is in customer Beta. It might not currently support all ecommerce platforms or cover all use cases.</td>
    </tr>
  </tbody>
</table>

[Gorgias website →](https://www.gorgias.com/)

## Overview

[Gorgias](https://www.gorgias.com/) is the ecommerce helpdesk that turns
your customer service into a profit center.
![](https://assets-global.website-files.com/5e4ff204e7b6f80e402d407a/5ffd9cdda4dcbda21839b648______header.png)

## Installation

`yarn add @frontend-sdk/gorgias`

`npm install @frontend-sdk/gorgias`

## Connecting to Gorgias Helpdesk

### Finding required values

In order to initialize chat widget, we need to find some required values in Gorgias Helpdesk.

First, navigate to "Settings" -> "Integrations" -> "Chat" page in Gorgias Helpdesk and create a new chat or modify the existing one:
![](https://user-images.githubusercontent.com/1743568/132680840-50afe4f2-51d1-4bcc-a42d-38c8f676a748.png?raw=true)

Next, navigate to "Installation" tab on chat settings page and find a section with JavaScript code:
![](https://user-images.githubusercontent.com/1743568/132680857-56d3a42e-c3e5-43c2-aaa2-be23db7c666b.png?raw=true)

Finally, extract these values from the code: `GORGIAS_CHAT_APP_ID`, `GORGIAS_CHAT_BASE_URL` and `GORGIAS_API_BASE_URL`

Now we have everything to initialize the widget in the app.

We need to execute `useChat` hook and pass extracted values to that hook.

### Executing the hook

```jsx
import { useChat } from '@frontend-sdk/gorgias'

const App = () => {
  // the values below should be extracted from the
  // section with JavaScript code as described above
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return (
    <section>
      <h1>App</h1>
      Chat: {status}
    </section>
  )
}
```

`useChat` may be executed not only once per application
but also inside local components.
The hook will correctly free all allocated resources on unmount.
This may be useful if you want to show chat widget
only on some pages of the app.

```jsx
import { useChat } from '@frontend-sdk/gorgias'

const ContactsPage = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return (
    <section>
      <h1>Contacts</h1>
      Chat: {status}
    </section>
  )
}
```

## Manual opening and closing

In some cases it may be useful to have manual control over
chat popup visibility state in the code.
This package exports `useChatState` and `useChatActions` hooks to control it.

**Note that these hooks should only be used after `useChat` returns `ready` status.**

```jsx
import { useChatActions, useChatState } from '@frontend-sdk/gorgias'

const Chat = () => {
  const { open, close } = useChatActions()
  const { isOpened } = useChatState()
  return (
    <section>
      Chat is {isOpened ? 'opened' : 'closed'}
      <button onClick={open}>Open</button>
      <button onClick={close}>Close</button>
    </section>
  )
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}
```

## Updating context

Gorgias supports passing extra data (e.g. Shopify context) to the chat.
This package exports `useChatActions` hook for that.

**Note that this hook should only be used after `useChat` returns `ready` status.**

### Setting current user email

You can use `setUserEmail` action to send current user email to Gorgias Helpdesk
to identify current chat user.
This action can be used together with `frontend-customer` package.

```jsx
import { useChat, useChatActions } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { setUserEmail } = useChatActions()
  const { email } = useCustomerState()
  useEffect(() => {
    email && setUserEmail(email)
  }, [email, setUserEmail])
  return null
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}
```

### Setting Shopify context

Besides user email, you can also use `setShopifyContext` action to send
store's Shopify domain, current customer ID and current Shopify cart.
Obviously, this is supported only for Shopify stores.
This action can also be used together with `frontend-customer` package.

```typescript jsx
import { useChatActions, useChatState, ShopifyCart } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { setShopifyContext } = useChatActions()
  const { id } = useCustomerState()
  const [cart, setCart] = useState<ShopifyCart | undefined>(undefined)

  useEffect(() => {
    // Shogun Frontend stores proxy these requests to Shopify
    fetch('./cart.js')
      .then((response) => response.json())
      .then(setCart)
  }, [])

  const customerId = typeof id === 'number' ? id : undefined

  useEffect(() => {
    setShopifyContext({
      // this should be the public platform domain of your Shopify store
      domain: 'store.myshopify.com',
      // Shopify only supports numeric customer ids
      // while `frontend-customer` returns `number | string | null`
      customerId,
      cart,
    })
  }, [id, cart, customerId])

  return null
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}
```

## Clearing local data

Gorgias Chat script stores some data in `localStorage` and it can be
useful to clear this.
For this purpose, this package exports another action `clear` in `useChatActions` hook.
This action clears all user data, all Gorgias identifiers
including the current chat token, and everything else.
Essentially, this action creates a brand new chat.

**Note that this hook should only be used after `useChat` returns `ready` status.**

```jsx
import { useChat, useChatActions } from '@frontend-sdk/gorgias'
import { useCustomerState } from 'frontend-customer'
import { useEffect } from 'react'

const Chat = () => {
  const { clear } = useChatActions()
  return <button onClick={clear}>Clear</button>
}

const App = () => {
  const status = useChat({
    chatAppId: '<insert GORGIAS_CHAT_APP_ID here>',
    chatBaseURL: '<insert GORGIAS_CHAT_BASE_URL>',
    apiBaseURL: '<insert GORGIAS_API_BASE_URL>',
  })
  return <section>{status === 'ready' && <Chat />}</section>
}
```
