Move in progress apps temporarily

This commit is contained in:
Darren Clarke 2023-03-07 14:09:49 +00:00
parent ba04aa108c
commit 6eaaf8e9be
360 changed files with 6171 additions and 55 deletions

View file

@ -1,11 +0,0 @@
import { List, Datagrid, TextField } from "react-admin";
const WhatsappAttachmentList = (props) => (
<List {...props} exporter={false}>
<Datagrid rowClick="show">
<TextField source="id" />
</Datagrid>
</List>
);
export default WhatsappAttachmentList;

View file

@ -1,11 +0,0 @@
import { Show, ShowProps, SimpleShowLayout, TextField } from "react-admin";
const WhatsappAttachmentShow = (props: ShowProps) => (
<Show {...props} title="Whatsapp Attachment">
<SimpleShowLayout>
<TextField source="id" />
</SimpleShowLayout>
</Show>
);
export default WhatsappAttachmentShow;

View file

@ -1,10 +0,0 @@
import WhatsappAttachmentIcon from "@material-ui/icons/AttachFile";
import WhatsappAttachmentList from "./WhatsappAttachmentList";
import WhatsappAttachmentShow from "./WhatsappAttachmentShow";
// eslint-disable-next-line import/no-anonymous-default-export
export default {
list: WhatsappAttachmentList,
show: WhatsappAttachmentShow,
icon: WhatsappAttachmentIcon,
};

View file

@ -1,38 +0,0 @@
// import dynamic from "next/dynamic";
import { SimpleForm, Create, TextInput, required } from "react-admin";
import { useSession } from "next-auth/react";
import { validateE164Number } from "../../../lib/phone-numbers";
const WhatsappBotCreate = (props) => {
// const MuiPhoneNumber = dynamic(() => import("material-ui-phone-number"), {
// ssr: false,
// });
const { data: session } = useSession();
return (
<Create {...props} title="Create Whatsapp Bot" redirect="show">
<SimpleForm>
<TextInput
source="userId"
defaultValue={
// @ts-expect-error: non-existent property
session.user.id
}
/>
<TextInput
source="phoneNumber"
validate={[validateE164Number, required()]}
/>
{/* <MuiPhoneNumber
defaultCountry={"us"}
fullWidth
onChange={(e: any) => setFieldValue("phoneNumber", e)}
/> */}
<TextInput source="description" />
</SimpleForm>
</Create>
);
};
export default WhatsappBotCreate;

View file

@ -1,12 +0,0 @@
import { SimpleForm, Edit, TextInput, required, EditProps } from "react-admin";
const WhatsappBotEdit = (props: EditProps) => (
<Edit {...props} title="Edit Bot">
<SimpleForm>
<TextInput source="phoneNumber" validate={[required()]} />
<TextInput source="description" />
</SimpleForm>
</Edit>
);
export default WhatsappBotEdit;

View file

@ -1,22 +0,0 @@
import {
List,
Datagrid,
DateField,
TextField,
BooleanField,
} from "react-admin";
const WhatsappBotList = (props) => (
<List {...props} exporter={false}>
<Datagrid rowClick="show">
<TextField source="phoneNumber" />
<TextField source="description" />
<BooleanField source="isVerified" />
<DateField source="createdAt" />
<DateField source="updatedAt" />
<TextField source="createdBy" />
</Datagrid>
</List>
);
export default WhatsappBotList;

View file

@ -1,177 +0,0 @@
import React, { useEffect, useState } from "react";
import {
Card,
Typography,
Grid,
Button,
TextField as MaterialTextField,
IconButton,
} from "@material-ui/core";
import {
Show,
SimpleShowLayout,
TextField,
ShowProps,
useQuery,
useMutation,
useRefresh,
BooleanField,
} from "react-admin";
import QRCode from "react-qr-code";
import useSWR from "swr";
import RefreshIcon from "@material-ui/icons/Refresh";
const Sidebar = ({ record }) => {
const [receivedMessages, setReceivedMessages] = useState([]);
const [phoneNumber, setPhoneNumber] = useState("");
const handlePhoneNumberChange = (event: any) => {
setPhoneNumber(event.target.value);
};
const [message, setMessage] = useState("");
const handleMessageChange = (event: any) => {
setMessage(event.target.value);
};
const sendMessage = async (phoneNumber: string, message: string) => {
await fetch(`/api/v1/whatsapp/bots/${record.token}/send`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ phoneNumber, message }),
});
};
const receiveMessages = async () => {
const result = await fetch(`/api/v1/whatsapp/bots/${record.token}/receive`);
const msgs = await result.json();
console.log(msgs);
setReceivedMessages(msgs);
};
return (
<Card style={{ width: "33%", marginLeft: 20, padding: 14 }}>
<Grid container direction="column" spacing={2}>
<Grid item>
<Typography variant="h6">Send message</Typography>
</Grid>
<Grid item>
<MaterialTextField
variant="outlined"
label="Phone number"
fullWidth
size="small"
value={phoneNumber}
onChange={handlePhoneNumberChange}
/>
</Grid>
<Grid item>
<MaterialTextField
variant="outlined"
label="Message"
multiline
rows={3}
fullWidth
size="small"
value={message}
onChange={handleMessageChange}
/>
</Grid>
<Grid item container direction="row-reverse">
<Button
variant="contained"
color="primary"
onClick={() => sendMessage(phoneNumber, message)}
>
Send
</Button>
</Grid>
<Grid item container direction="row">
<Grid item>
<Typography variant="h6">Receive messages</Typography>
</Grid>
<Grid item>
<IconButton
onClick={receiveMessages}
color="primary"
style={{ marginTop: -12 }}
>
<RefreshIcon />
</IconButton>
</Grid>
</Grid>
{receivedMessages.map((receivedMessage) => (
<Grid item container direction="column" spacing={1}>
<Grid item style={{ fontWeight: "bold", color: "#999" }}>
{receivedMessage.key.remoteJid.replace("@s.whatsapp.net", "")}
</Grid>
<Grid item style={{ borderBottom: "1px solid #999" }}>
{receivedMessage.message.conversation}
</Grid>
</Grid>
))}
</Grid>
</Card>
);
};
const WhatsappBotShow = (props: ShowProps) => {
const refresh = useRefresh();
const { data } = useQuery({
type: "getOne",
resource: "whatsappBots",
payload: { id: props.id },
});
const [unverify] = useMutation({
type: "update",
resource: "whatsappBots",
payload: {
id: props.id,
data: { isVerified: false, qrCode: null, authInfo: null },
},
});
const { data: registerData, error: registerError } = useSWR(
data && !data?.isVerified
? `/api/v1/whatsapp/bots/${props.id}/register`
: undefined,
{ refreshInterval: 59000 }
);
console.log({ registerData, registerError });
useEffect(() => {
if (data && !data?.isVerified) {
const interval = setInterval(() => {
refresh();
}, 10000);
return () => clearInterval(interval);
}
}, [refresh, data]);
return (
// @ts-expect-error: Missing props
<Show {...props} title="Bot" aside={<Sidebar />}>
<SimpleShowLayout>
<TextField source="phoneNumber" />
<BooleanField source="isVerified" />
<Button
color="primary"
size="small"
style={{ color: "black", backgroundColor: "#ddd" }}
onClick={unverify}
>
Unverify
</Button>
<TextField source="description" />
<TextField source="token" />
{!data?.isVerified && data?.qrCode && data?.qrCode !== "" && (
<QRCode value={data.qrCode} />
)}
</SimpleShowLayout>
</Show>
);
};
export default WhatsappBotShow;

View file

@ -1,14 +0,0 @@
import WhatsappBotIcon from "@material-ui/icons/WhatsApp";
import WhatsappBotList from "./WhatsappBotList";
import WhatsappBotEdit from "./WhatsappBotEdit";
import WhatsappBotCreate from "./WhatsappBotCreate";
import WhatsappBotShow from "./WhatsappBotShow";
// eslint-disable-next-line import/no-anonymous-default-export
export default {
list: WhatsappBotList,
create: WhatsappBotCreate,
edit: WhatsappBotEdit,
show: WhatsappBotShow,
icon: WhatsappBotIcon,
};

View file

@ -1,24 +0,0 @@
import {
SelectInput,
required,
ReferenceInput,
ReferenceField,
TextField,
} from "react-admin";
export const WhatsAppBotSelectInput = (source: string) => () => (
<ReferenceInput
label="WhatsApp Bot"
reference="whatsappBots"
source={source}
validate={[required()]}
>
<SelectInput optionText="phoneNumber" />
</ReferenceInput>
);
export const WhatsAppBotField = (source: string) => () => (
<ReferenceField label="WhatsApp Bot" reference="whatsappBots" source={source}>
<TextField source="phoneNumber" />
</ReferenceField>
);

View file

@ -1,23 +0,0 @@
import {
List,
ListProps,
Datagrid,
DateField,
TextField,
BooleanField,
} from "react-admin";
const WhatsappMessageList = (props: ListProps) => (
<List {...props} exporter={false}>
<Datagrid rowClick="show">
<TextField source="phoneNumber" />
<TextField source="description" />
<BooleanField source="isVerified" />
<DateField source="createdAt" />
<DateField source="updatedAt" />
<TextField source="createdBy" />
</Datagrid>
</List>
);
export default WhatsappMessageList;

View file

@ -1,29 +0,0 @@
import {
Show,
ShowProps,
SimpleShowLayout,
TextField,
ReferenceManyField,
Datagrid,
} from "react-admin";
const WhatsappMessageShow = (props: ShowProps) => (
<Show {...props} title="Whatsapp Message">
<SimpleShowLayout>
<TextField source="waMessage" />
<TextField source="createdAt" />
<ReferenceManyField
label="Attachments"
reference="whatsappAttachments"
target="whatsappMessageId"
>
<Datagrid rowClick="show">
<TextField source="id" />
<TextField source="createdAt" />
</Datagrid>
</ReferenceManyField>
</SimpleShowLayout>
</Show>
);
export default WhatsappMessageShow;

View file

@ -1,10 +0,0 @@
import WhatsappMessageIcon from "@material-ui/icons/Message";
import WhatsappMessageList from "./WhatsappMessageList";
import WhatsappMessageShow from "./WhatsappMessageShow";
// eslint-disable-next-line import/no-anonymous-default-export
export default {
list: WhatsappMessageList,
show: WhatsappMessageShow,
icon: WhatsappMessageIcon,
};