Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, { useEffect, useState } from "react"
import Component from "./section/component"
import Button from "./core/Button"
import { GRAPHQL_URL } from "../constant"
interface ISection {
id: string
}
const getGraphqlQuery = (id: string) => {
return {
query: `
query getSectionById ($id: ID!) {
sections_by_id(id: $id) {
id
title
subtitle
content
actions {
actions_id {
name
url
style
id
}
}
components {
components_id {
id
title
subtitle
content
url_dataviz
url_map
type
mapdid
layout
length
size
actions {
actions_id {
name
status
style
id
}
}
services {
services_id {
name
url
description
id
image {
id
}
}
}
images {
directus_files_id {
id
}
}
}
}
}
}
`,
variables: {
id,
},
}
}
const Section = (props: ISection) => {
const { id } = props
const [data, setData] = useState<any>()
useEffect(() => {
fetch(GRAPHQL_URL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(getGraphqlQuery(id)),
})
.then((response) => {
if (!response.ok) {
throw Error(response.statusText)
}
return response.json()
})
.then((result) => {
setData(result.data.sections_by_id)
})
.catch(function (error) {
console.log(error)
})
}, [])
if (!data) return null
return (
<div>
{data.title && (
<h1 className="text-3xl font-extrabold">{data.title}</h1>
)}
{data.subtitle && (
<h4 className="text-2xl font-bold mt-2">{data.subtitle}</h4>
)}
{data.content && (
<div
className="text-lg leading-8 py-4 text-justify"
dangerouslySetInnerHTML={{ __html: data.content }}
></div>
)}
{data.components.length > 0 && (
<div
className={`grid grid-cols-1 lg:grid-cols-${data.components.length} gap-4`}
>
{data.components.map((component) => {
const dtComponent = component.components_id
return (
<React.Fragment key={dtComponent.id}>
<Component {...dtComponent} />
</React.Fragment>
)
})}
</div>
)}
{data.actions.length > 0 && (
<div className="flex space-x-4 mt-4">
{data.actions.map((actionId) => {
const action = actionId.actions_id
return (
<Button
key={action.id}
{...action}
className={action.style}
/>
)
})}
</div>
)}
</div>
)
}
export default Section