Source: pages/dreams/[dreamId].jsx

  1. /** @module pages/dreams/:dreamId */
  2. import Head from "next/head";
  3. import Dream from "../../containers/dream";
  4. import { getAuthProps } from "../../lib/auth";
  5. import { truncate } from "../../lib/strings";
  6. import {
  7. getPostById,
  8. getUserByEmail,
  9. getUserById,
  10. getComments,
  11. } from "../../lib/db/reads";
  12. import { getUserAgentProps } from "../../lib/user-agent";
  13. import { serverSideTranslations } from "next-i18next/serverSideTranslations";
  14. /**
  15. * Dream page. This page shows a user's dream.
  16. *
  17. * @param {{ data, comments, serverSession }} props - The props this component gets from getServerSideProps
  18. */
  19. export default function DreamPage(props) {
  20. const { data: rawData, comments: rawComments, ...authProps } = props;
  21. const data = JSON.parse(rawData);
  22. const comments = JSON.parse(rawComments);
  23. return (
  24. <>
  25. <Head>
  26. <title>{truncate(data.dream.text, 50, true)}</title>
  27. </Head>
  28. <Dream data={data} comments={comments} {...authProps} />
  29. </>
  30. );
  31. }
  32. export async function getServerSideProps(context) {
  33. const authProps = await getAuthProps(context);
  34. const { res } = context;
  35. try {
  36. const { dreamId } = context.params;
  37. if (!authProps.props.serverSession) {
  38. const data = await getPostById(dreamId);
  39. if (data.visibility === "private") {
  40. res.setHeader("location", `/${context.locale}`);
  41. res.statusCode = 302;
  42. res.end();
  43. return { props: {} };
  44. }
  45. const user = await getUserById(data.userId);
  46. data.user = user;
  47. const comments = await getComments(dreamId);
  48. return {
  49. props: {
  50. ...authProps.props,
  51. data: JSON.stringify(data),
  52. comments: JSON.stringify(comments),
  53. ...getUserAgentProps(context),
  54. ...(await serverSideTranslations(context.locale, [
  55. "layout",
  56. "footer",
  57. "common",
  58. ])),
  59. },
  60. };
  61. }
  62. let [data, user] = await Promise.all([
  63. getPostById(dreamId),
  64. getUserByEmail(authProps.props.serverSession.user.email),
  65. ]);
  66. const isDreamOwner = user._id.toString() === data.userId.toString();
  67. if (data.visibility === "private" && !isDreamOwner) {
  68. res.setHeader("location", `/${context.locale}/dreams`);
  69. res.statusCode = 302;
  70. res.end();
  71. return {
  72. props: {
  73. ...getUserAgentProps(context),
  74. ...(await serverSideTranslations(context.locale, [
  75. "dashboard",
  76. "common",
  77. ])),
  78. },
  79. };
  80. }
  81. if (data.visibility === "anonymous") {
  82. delete data.userId;
  83. } else {
  84. if (isDreamOwner) {
  85. data.user = user;
  86. } else {
  87. const user = await getUserById(data.userId);
  88. data.user = user;
  89. }
  90. }
  91. const comments = await getComments(dreamId);
  92. return {
  93. props: {
  94. ...authProps.props,
  95. data: JSON.stringify(data),
  96. comments: JSON.stringify(comments),
  97. ...getUserAgentProps(context),
  98. ...(await serverSideTranslations(context.locale, [
  99. "dashboard",
  100. "common",
  101. ])),
  102. },
  103. };
  104. } catch (error) {
  105. logError(error, {
  106. service: "web",
  107. pathname: "/dreams/[dreamId]",
  108. component: "DreamPage",
  109. });
  110. }
  111. }