-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (89 loc) · 2.11 KB
/
index.js
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
import Feedback from "../views/feedback";
import Welcome from "../views/welcome";
import dayjs from "dayjs";
import axios from "axios";
export default function Index(props) {
if (props.subdomain === "www") {
return <Welcome />;
} else {
return (
<Feedback
subdomain={props.subdomain}
subdomainExists={props.subdomainExists}
feedback={props.feedback}
createdAt={props.createdAt}
/>
);
}
}
export let API_ENDPOINT;
if (process.env.NODE_ENV === "production") {
API_ENDPOINT = "https://api.tempfeedback.com";
} else {
API_ENDPOINT = "http://localhost:3000";
}
export async function getServerSideProps(context) {
const subdomain = context.req.headers.host.split(".")[0];
if (subdomain === "www") {
return {
props: {
subdomain,
subdomainExists: true,
},
};
} else {
try {
const res = await axios.get(API_ENDPOINT + "/GetFeedbackFromSubdomain", {
params: { subdomain: subdomain },
});
let feedback;
if ("feedback" in res.data) {
feedback = res.data.feedback;
feedback.sort((a, b) => {
const x = dayjs(a);
const y = dayjs(b);
if (x.isBefore(y)) {
return 1;
} else if (x.isSame(y)) {
return 0;
} else {
return -1;
}
});
} else {
feedback = [];
}
return {
props: {
subdomain,
feedback,
createdAt: res.data.createdAt,
subdomainExists: true,
},
};
} catch (err) {
return {
props: {
subdomain,
feedback: [],
createdAt: 0,
subdomainExists: false,
},
};
}
//feedback = [
//{
//dateTime: "2021-02-19T15:43:00Z",
//text: "The quick brown fox jumps over the lazy dog",
//},
//{
//dateTime: "2021-02-22T15:43:00Z",
//text: "The quick brown fox jumps over the lazy dog 2",
//},
//{
//dateTime: "2021-02-23T15:43:00Z",
//text: "The quick brown fox jumps over the lazy dog 3",
//},
//];
}
}