Skip to content

Commit 4755999

Browse files
committed
feat: web destinations
1 parent 0c0e49d commit 4755999

File tree

6 files changed

+122
-3
lines changed

6 files changed

+122
-3
lines changed

lib/components/Destination.tsx

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Typography, TextField, Box } from "@mui/material"
2+
import { Public, Android, PhoneIphone } from "@mui/icons-material"
3+
4+
export interface DestinationProps {
5+
platform: "web" | "ios" | "android"
6+
value: string
7+
onChange: (value: string) => void
8+
}
9+
10+
export const Destination = ({ platform, value, onChange }: DestinationProps) => {
11+
12+
const PlatformIcon = () => {
13+
switch (platform) {
14+
case "web":
15+
return <Public />
16+
case "android":
17+
return <Android />
18+
case "ios":
19+
return <PhoneIphone />
20+
}
21+
}
22+
23+
return (
24+
<Box m={1}>
25+
<Box display="flex" alignItems="center" justifyContent="space-between">
26+
<Typography variant="subtitle2">Platform: {platform}</Typography>
27+
<PlatformIcon />
28+
</Box>
29+
30+
31+
{platform === "web" && <Web value={value} onChange={onChange} />}
32+
</Box>
33+
)
34+
}
35+
36+
37+
const Web = ({ value, onChange }: { value: string, onChange: (value: string) => void }) => {
38+
return (
39+
<TextField
40+
label="Destination"
41+
value={value}
42+
type="text"
43+
helperText="What URL should we send the user to?"
44+
onChange={(e) => onChange(e.target.value)}
45+
/>
46+
)
47+
}

lib/components/LinkConfig/EditModal.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Close } from "@mui/icons-material"
1616

1717
import { LinkConfig } from "@/lib/services/linkConfig"
1818
import { ConfirmButton } from "../ConfirmButton"
19+
import { Destination } from "../Destination"
1920

2021
export interface EditModalProps {
2122
open: boolean
@@ -29,6 +30,7 @@ const EditModal = ({ open, onClose, config }: EditModalProps): JSX.Element => {
2930
const [seoTitle, setSeoTitle] = useState(config?.seo?.title || "")
3031
const [seoDescription, setSeoDescription] = useState(config?.seo?.description || "")
3132
const [seoMedia, setSeoMedia] = useState(config?.seo?.media || "")
33+
const [destinationValue, setDestinationValue] = useState(config?.destinations?.[0]?.value || "")
3234

3335
const [error, setError] = useState("")
3436
const [isLoading, setIsLoading] = useState(false)
@@ -56,7 +58,10 @@ const EditModal = ({ open, onClose, config }: EditModalProps): JSX.Element => {
5658
description: seoDescription || "",
5759
media: seoMedia || "",
5860
},
59-
destinations: [],
61+
destinations: [{
62+
platform: "web",
63+
value: destinationValue,
64+
}],
6065
}),
6166
})
6267

@@ -124,6 +129,13 @@ const EditModal = ({ open, onClose, config }: EditModalProps): JSX.Element => {
124129
onChange={(e) => setName(e.target.value)}
125130
/>
126131

132+
<Box my={2}>
133+
<Typography variant="subtitle1">Destinations</Typography>
134+
{/* <Repeater onAdd, onRemove, items={config.destinations}, renderChild /> */}
135+
<Destination platform="web" value={destinationValue} onChange={val => setDestinationValue(val)} />
136+
137+
</Box>
138+
127139
<Box my={2}>
128140
<Typography variant="subtitle1">SEO Settings</Typography>
129141

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
export interface PlatformRedirectProps {
3+
destinations: Array<{ platform: string, value: string }>
4+
}
5+
6+
const PlatformRedirect = ({ destinations }: PlatformRedirectProps) => {
7+
8+
if (window && Array.isArray(destinations)) {
9+
const web = destinations.find(dest => dest.platform === "web")
10+
if (web && web.value) {
11+
window.location.href = web.value
12+
return null
13+
}
14+
}
15+
16+
return (
17+
<>
18+
</>
19+
)
20+
}
21+
22+
export default PlatformRedirect

lib/components/Repeater/index.tsx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Box, IconButton } from "@mui/material"
2+
import { Add, Delete } from "@mui/icons-material"
3+
4+
export interface RepeaterProps {
5+
renderChild: (data: unknown, idx: number) => JSX.Element
6+
items: { id?: string }[]
7+
onAdd: () => void
8+
onRemove: (id: string) => void
9+
}
10+
11+
const Repeater = ({ onAdd, onRemove, items, renderChild }: RepeaterProps): JSX.Element => {
12+
return (
13+
<>
14+
{items.map((item: { id?: string }, idx: number) => (
15+
<Box key={item.id} m={1} display="flex" alignItems="center">
16+
{renderChild(item, idx)}
17+
<Box display="flex" justifyContent="flex-end">
18+
<IconButton onClick={() => onRemove(item.id || "")}>
19+
<Delete color="warning" />
20+
</IconButton>
21+
</Box>
22+
</Box>
23+
))}
24+
<Box display="flex" justifyContent="flex-end">
25+
<IconButton onClick={onAdd}>
26+
<Add color="primary" />
27+
</IconButton>
28+
</Box>
29+
</>
30+
)
31+
}
32+
33+
export default Repeater

lib/services/linkConfig/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ export interface LinkConfig {
2525
destinations: Array<Destination>
2626
}
2727

28-
2928
const buildCollectionDocPath = (userId: string, docId?: string): string => {
3029
const path = `${config.datastore.basePath}/${userId}/configs`
3130
if (docId) {

pages/link/[userId]/[id].tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { GetServerSideProps } from "next"
22
import Head from "next/head"
3+
import dynamic from "next/dynamic"
34
import Error from "next/error"
45

6+
const PlatformRedirect = dynamic(() => import("@/lib/components/PlatformRedirect"), {
7+
ssr: false,
8+
})
9+
510
import { LinkConfig, fetchById } from "@/lib/services/linkConfig"
611

712

@@ -14,14 +19,15 @@ const LinkPage = ({ data }: LinkProps): JSX.Element => {
1419
return <Error statusCode={404} />
1520
}
1621

22+
1723
return (
1824
<>
1925
<Head>
2026
<title>{data?.seo?.title || "Link Config"}</title>
2127
<meta name="description" content={data?.seo?.description || "Link Config"} />
2228
</Head>
2329
<p>{data?.seo?.title || "Link Config"}</p>
24-
<p>Todo: redirect</p>
30+
<PlatformRedirect destinations={data?.destinations || []} />
2531
</>
2632
)
2733
}

0 commit comments

Comments
 (0)