import { CloseCircleFilled, DeleteOutlined, ReloadOutlined, UserSwitchOutlined } from '@ant-design/icons' import { Button, Col, Form, Input, Layout, notification, Popconfirm, Row, Space, Switch, Table, Tag, Tooltip, Typography } from 'antd' import moment from 'moment' import QueryString from 'qs' import { FC, useEffect, useState } from 'react' import useSWR from 'swr' import { fetcher, req } from '../../utils/Fetcher' interface Props { me?: any, errorMe?: any } const Admin: FC = ({ me, errorMe }) => { const { data: dataConfig, mutate: refetchConfig } = useSWR('/config', fetcher) const [configForm] = Form.useForm() const [loading, setLoading] = useState() const [selectedRows, setSelectedRows] = useState() const PAGE_SIZE = 10 const [params, setParams] = useState>() const { data: dataUsers, error, mutate: refetchUsers } = useSWR(params ? `/users?${QueryString.stringify(params)}` : null, fetcher) useEffect(() => { if (me?.user && me.user.role !== 'admin') { window.location.replace('/dashboard') } }, [me]) useEffect(() => { if (errorMe) { window.localStorage.clear() window.location.replace('/login') } }, [errorMe]) useEffect(() => { if (dataConfig?.config) { setLoading(false) configForm.setFieldsValue({ ...dataConfig.config, invitation_code: dataConfig.config.invitation_code ? `${location.host}/login?code=${dataConfig.config.invitation_code || ''}` : null }) } }, [dataConfig]) useEffect(() => { if (dataUsers?.users) { dataUsers.users = dataUsers.users.map((user: any) => ({ ...user, key: user.id })) } }, [dataUsers]) useEffect(() => { setParams({ offset: 0, limit: PAGE_SIZE, sort: 'created_at:desc', }) }, []) const updateConfig = async () => { const values = configForm.getFieldsValue() try { const { data } = await req.patch('/config', { config: { ...values, invitation_code: values.invitation_code || null } }) refetchConfig() return notification.success({ key: 'update', message: 'Updated', description: data.config.disable_signup ? 'Signup is disabled for everyone' : data.config.invitation_code ? 'Signup is enabled by invitation code' : 'Signup is enabled for everyone', }) } catch (error: any) { return notification.error({ message: error?.response?.status || 'Something error', ...error?.response?.data ? { description: <> {error?.response?.data?.error || error.message || 'Something error'} {JSON.stringify(error?.response?.data || error?.data || error, null, 2)} } : {} }) } } return Users Management
updateConfig()} /> {!dataConfig?.config.disable_signup && {dataConfig?.config.invitation_code ? } onClick={() => { setLoading(true) req.patch('/config', { config: { clear_invitation_code: true } }).then(({ data }) => { refetchConfig() notification.success({ key: 'update', message: 'Updated', description: data.config.disable_signup ? 'Signup is disabled for everyone' : data.config.invitation_code ? 'Signup is enabled by invitation code' : 'Signup is enabled for everyone', }) }) }} />} loading={loading} enterButton={<> Generate} onSearch={() => { setLoading(true) req.post('/config/resetInvitationCode').then(({ data }) => { refetchConfig() notification.success({ key: 'update', message: 'Updated', description: data.config.disable_signup ? 'Signup is disabled for everyone' : data.config.invitation_code ? 'Signup is enabled by invitation code' : 'Signup is enabled for everyone', }) }) }} /> : } }
{ setParams({ ...params, offset: 0, search: val || undefined }) }} /> { Promise.all((selectedRows || [])?.map(async (user: any) => { try { await req.delete(`/users/${user.id}`) } catch (error) { // } })).then(() => { refetchUsers() setSelectedRows(undefined) notification.success({ message: `Delete ${selectedRows?.length} users` }) }) }}> <>{{record.role}} }, { title: 'Username', dataIndex: 'username', key: 'username', render: (value: string, record) => <>{value} }, { title: 'Name', dataIndex: 'name', key: 'name', // responsive: ['md'], }, { title: 'Registered At', dataIndex: 'created_at', key: 'created_at', width: 230, sorter: true, // responsive: ['md'], render: (value: any) => moment(value).local().format('llll') }, { title: '', dataIndex: 'actions', key: 'actions', // responsive: ['md'], render: (_, record) =>