All files / client/components/Admin/Notification NotificationPage.tsx

0% Statements 0/16
0% Branches 0/6
0% Functions 0/6
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                                                                 
import React, { useContext, useState, useEffect, FC } from 'react'
import Crowi from 'client/util/Crowi'
 
import { AdminContext } from 'components/Admin/AdminPage'
import NotificationSettings from './NotificationSettings'
import NotificationPatterns from './NotificationPatterns'
import Instructions from './Instructions'
import ConnectButton from './ConnectButton'
 
function useFetchNotificationSettings(crowi: Crowi) {
  const [settings, setSettings] = useState({
    settings: [] as {}[],
    slackSetting: {},
    hasSlackConfig: null,
    hasSlackToken: null,
    slackAuthUrl: null,
    appUrl: '',
  })
 
  const fetchSettings = async () => {
    const { settings, slackSetting, hasSlackConfig, hasSlackToken, slackAuthUrl, appUrl } = await crowi.apiGet('/admin/notification')
    setSettings({ settings, slackSetting, hasSlackConfig, hasSlackToken, slackAuthUrl, appUrl })
  }
 
  return [settings, fetchSettings] as const
}
 
export default function NotificationPage() {
  const { crowi, loading } = useContext(AdminContext)
  const [{ settings, slackSetting, hasSlackConfig, hasSlackToken, slackAuthUrl, appUrl }, fetchSettings] = useFetchNotificationSettings(crowi)
 
  const addPattern = async ({ pathPattern, channel }: { pathPattern: string; channel: string }) => {
    await crowi.apiPost('/admin/notification.add', { pathPattern, channel })
    await fetchSettings()
  }
 
  const removePattern = async (id: string) => {
    await crowi.apiPost('/admin/notification.remove', { id })
    await fetchSettings()
  }
 
  useEffect(() => {
    fetchSettings()
  }, [])
 
  return (
    !loading && (
      <>
        <NotificationSettings crowi={crowi} slackSetting={slackSetting} fetchSettings={fetchSettings} />
        {slackAuthUrl && <ConnectButton hasSlackToken={hasSlackToken} slackAuthUrl={slackAuthUrl} />}
        {hasSlackConfig && <NotificationPatterns settings={settings} addPattern={addPattern} removePattern={removePattern} />}
        <Instructions appUrl={appUrl} />
      </>
    )
  )
}