Skip to content

Commit

Permalink
Ensure only valid zone are shown
Browse files Browse the repository at this point in the history
  • Loading branch information
LeoColomb authored Aug 14, 2023
1 parent 597f143 commit da2f965
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
16 changes: 16 additions & 0 deletions pages/app/models/routing.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { AppLoadContext } from '@remix-run/cloudflare'
import * as routing from 'sdk/routing'

export async function getRoutingZones(
zones: Zone[] | Promise<Zone[]>,
context: AppLoadContext,
): Promise<Array<Zone>> {
const allZones = await zones
const routingZones: Array<PromiseSettledResult<Routing>> =
await Promise.allSettled(
allZones.map((zone: Zone) => routing.get(zone, context)),
)
return routingZones
.filter((zone) => zone.status === 'fulfilled' && zone.value.enabled)
.map((rZone) => allZones.find((aZone) => aZone.name === rZone.value.name))
}
11 changes: 6 additions & 5 deletions pages/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Await, Form, useLoaderData, useNavigation } from '@remix-run/react'

import { createRule } from '~/models/rule.server'
import { getZones } from '~/models/zone.server'
import { getRoutingZones } from '~/models/routing.server'
import { getAddresses } from '~/models/address.server'

export const action = async ({ request, context }: ActionArgs) => {
Expand All @@ -23,13 +24,13 @@ export const action = async ({ request, context }: ActionArgs) => {

export const loader = async ({ context }: LoaderArgs) => {
return defer({
zones: getZones(context),
routingZones: getRoutingZones(getZones(context), context),
addresses: getAddresses(context),
})
}

export default function Index() {
const { zones, addresses } = useLoaderData<typeof loader>()
const { routingZones, addresses } = useLoaderData<typeof loader>()
const today = new Date().toISOString().split('T')[0]
const navigation = useNavigation()
const isCreating = navigation.state === 'submitting'
Expand Down Expand Up @@ -80,7 +81,7 @@ export default function Index() {
}
>
<Await
resolve={zones}
resolve={routingZones}
errorElement={
<select
id="zone"
Expand All @@ -94,14 +95,14 @@ export default function Index() {
</select>
}
>
{(zones: Zone[]) => (
{(routingZones: Zone[]) => (
<select
id="zone"
name="zone"
aria-label="Address domain"
required
>
{zones.map((zone) => (
{routingZones.map((zone) => (
<option value={JSON.stringify(zone)} key={zone.id}>
{zone.name}
</option>
Expand Down
7 changes: 7 additions & 0 deletions sdk/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ type Zone = {
name: string
}

type Routing = {
enabled: boolean
name: string
status: string
tag: string
}

type Address = {
email: string
tag: string
Expand Down
5 changes: 5 additions & 0 deletions sdk/routing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { fetchAPI } from 'sdk/global'

export async function get(zone: Zone, env: Env): Promise<Routing> {
return fetchAPI(`zones/${zone.id}/email/routing`, 'GET', env)
}

0 comments on commit da2f965

Please sign in to comment.