diff --git a/web/components/dashboard/ReceiveSMS.tsx b/web/components/dashboard/ReceiveSMS.tsx new file mode 100644 index 0000000..b879969 --- /dev/null +++ b/web/components/dashboard/ReceiveSMS.tsx @@ -0,0 +1,170 @@ +import { + Alert, + AlertIcon, + Grid, + GridItem, + Spinner, + Stack, + Tab, + TabList, + TabPanel, + TabPanels, + Table, + TableContainer, + Tabs, + Tbody, + Td, + Th, + Thead, + Tr, +} from '@chakra-ui/react' +import { useEffect, useMemo, useState } from 'react' +import { useSelector } from 'react-redux' +import { + fetchReceivedSMSList, + selectDeviceList, + selectReceivedSMSList, +} from '../../store/deviceSlice' +import { useAppDispatch } from '../../store/hooks' +import { selectAuthUser } from '../../store/authSlice' + +export default function ReceiveSMS() { + return ( + <> + + + + + + + + + + ) +} + +const ReceiveSMSNotifications = () => { + return ( + + + + You can now receive SMS and view them in the dashboard, or retreive them + via the API + + + + + To receive SMS, you need to have an active device that has receive SMS + option enabled (Turn on the switch in the app) + + + + + Webhooks will be available soon 😉 + + + ) +} + +const ReceivedSMSList = () => { + const dispatch = useAppDispatch() + + const [tabIndex, setTabIndex] = useState(0) + + const { loading: receivedSMSListLoading, data: receivedSMSListData } = + useSelector(selectReceivedSMSList) + const deviceList = useSelector(selectDeviceList) + + const authUser = useSelector(selectAuthUser) + + const activeDeviceId = useMemo(() => { + return deviceList[tabIndex]?._id + }, [tabIndex, deviceList]) + + useEffect(() => { + if (authUser) { + dispatch(fetchReceivedSMSList(activeDeviceId)) + } + }, [dispatch, authUser, activeDeviceId]) + + if (!receivedSMSListLoading && (!deviceList || deviceList.length == 0)) { + return ( + + + You dont have any devices yet. Please register a device to receive SMS + + ) + } + + return ( + <> + + + {deviceList.map(({ _id, brand, model }) => ( + {`${brand} ${model}`} + ))} + + + {deviceList.map(({ _id, brand, model }) => ( + + + + + + + + + + + + {receivedSMSListLoading && ( + + + + )} + + {!receivedSMSListLoading && + receivedSMSListData.length == 0 && ( + + )} + + {!receivedSMSListLoading && + receivedSMSListData.length > 0 && + receivedSMSListData.map( + ({ _id, sender, message, receivedAt }) => ( + + + + + + + ) + )} + +
sendermessagereceived at
+ +
+ No SMS received +
{sender} + {message} + + {new Date(receivedAt).toLocaleString('en-US', { + month: 'long', + day: 'numeric', + year: 'numeric', + hour: 'numeric', + minute: 'numeric', + hour12: true, + })} +
+
+
+ ))} +
+
+ + ) +} diff --git a/web/pages/dashboard.tsx b/web/pages/dashboard.tsx index e8b4d68..534b4da 100644 --- a/web/pages/dashboard.tsx +++ b/web/pages/dashboard.tsx @@ -54,13 +54,11 @@ const DashboardTabView = () => { - {/* Get Started */} API Key and Devices Send SMS Receive SMS - {/* Get Started */} diff --git a/web/services/gatewayService.ts b/web/services/gatewayService.ts index e361aeb..b7603d8 100644 --- a/web/services/gatewayService.ts +++ b/web/services/gatewayService.ts @@ -34,6 +34,11 @@ class GatewayService { ) return res.data.data } + + async getReceivedSMSList(deviceId: string) { + const res = await httpClient.get(`/gateway/devices/${deviceId}/getReceivedSMS`) + return res.data.data + } } export const gatewayService = new GatewayService() diff --git a/web/store/deviceSlice.ts b/web/store/deviceSlice.ts index d9d3b7d..83cf040 100644 --- a/web/store/deviceSlice.ts +++ b/web/store/deviceSlice.ts @@ -11,6 +11,10 @@ const initialState = { item: null, list: [], sendingSMS: false, + receivedSMSList: { + loading: false, + data: [], + }, } export const fetchDevices = createAsyncThunk( @@ -29,6 +33,22 @@ export const fetchDevices = createAsyncThunk( } ) +export const fetchReceivedSMSList = createAsyncThunk( + 'device/fetchReceivedSMSList', + async (id: string, { rejectWithValue }) => { + try { + const res = await gatewayService.getReceivedSMSList(id) + return res + } catch (e) { + toast({ + title: e.response.data.error || 'Failed to Fetch received sms list', + status: 'error', + }) + return rejectWithValue(e.response.data) + } + } +) + export const deleteDevice = createAsyncThunk( 'device/deleteDevice', async (id: string, { rejectWithValue, dispatch }) => { @@ -100,6 +120,19 @@ export const deviceSlice = createSlice({ .addCase(sendSMS.rejected, (state) => { state.sendingSMS = false }) + .addCase(fetchReceivedSMSList.pending, (state) => { + state.receivedSMSList.loading = true + }) + .addCase( + fetchReceivedSMSList.fulfilled, + (state, action: PayloadAction) => { + state.receivedSMSList.loading = false + state.receivedSMSList.data = action.payload + } + ) + .addCase(fetchReceivedSMSList.rejected, (state) => { + state.receivedSMSList.loading = false + }) }, }) @@ -109,5 +142,7 @@ export const selectDeviceList = (state: RootState) => state.device.list export const selectDeviceItem = (state: RootState) => state.device.item export const selectDeviceLoading = (state: RootState) => state.device.loading export const selectSendingSMS = (state: RootState) => state.device.sendingSMS +export const selectReceivedSMSList = (state: RootState) => + state.device.receivedSMSList export default deviceSlice.reducer