Browse Source

Friends display on homepage

master
Archie 1 week ago
parent
commit
8e65e8248f
4 changed files with 77 additions and 3 deletions
  1. +5
    -0
      .env.example
  2. +36
    -0
      src/pages/api/friends/index.js
  3. +1
    -1
      src/pages/api/pulsar/clients.js
  4. +35
    -2
      src/pages/index.js

+ 5
- 0
.env.example View File

@ -12,6 +12,11 @@ AVATAR_SECRET=
PULSAR_API=
PULSAR_SECRET=
FRIENDS_API=
FRIENDS_SECRET=
HORIZON_API=
NEXT_PUBLIC_ORIGIN=
NEXT_PUBLIC_COOKIE_DOMAIN=

+ 36
- 0
src/pages/api/friends/index.js View File

@ -0,0 +1,36 @@
import auth from "../../../utils/auth";
import axios from "axios";
const { FRIENDS_API, FRIENDS_SECRET, HORIZON_API } = process.env;
export default async (req, res) => {
const user = await auth(req);
if (!user) return res.status(401).send({ err: "badAuthorization" });
try {
res.json({
friends: (
await Promise.all(
(
await axios.get(`${FRIENDS_API}/${user.id}`, {
headers: {
Authorization: FRIENDS_SECRET,
},
})
).data.friends.sort((a, b) => a.acceptedAt > b.acceptedAt).map(async ({ user }) => {
try {
return (
await axios.get(
`${HORIZON_API}/users/${encodeURIComponent(user)}`
)
).data;
} catch (err) {}
})
)
).filter((u) => !!u),
});
} catch (err) {
console.log(err);
res.status(500).json({ err: "internalError" });
}
};

+ 1
- 1
src/pages/api/pulsar/clients.js View File

@ -10,7 +10,7 @@ export default async (req, res) => {
try {
res.json(
(
await axios.get(`${PULSAR_API}/user/${encodeURIComponent(user.id)}`, {
await axios.get(`${PULSAR_API}/user/${user.id}`, {
headers: {
Authorization: PULSAR_SECRET,
},

+ 35
- 2
src/pages/index.js View File

@ -1,4 +1,4 @@
import { Box } from "@alleshq/reactants";
import { Avatar, Box } from "@alleshq/reactants";
import {
User as UserIcon,
Shield,
@ -10,6 +10,8 @@ import {
import Link from "next/link";
import { Page } from "../components/Page";
import { useUser } from "../utils/userContext";
import axios from "axios";
import cookies from "next-cookies";
const categories = [
{
@ -98,7 +100,7 @@ const products = [
},
];
const page = () => {
const page = ({ friends }) => {
const user = useUser();
return (
@ -108,6 +110,23 @@ const page = () => {
{user.plus && <sup className="select-none text-primary">+</sup>}
</h4>
{friends.length > 0 && (
<div className="grid grid-cols-6 gap-3">
{friends.map((u) => (
<div className="flex justify-center" key={u.id}>
<Link
href="/[user]"
as={`/${encodeURIComponent(u.username || u.id)}`}
>
<a className="flex">
<Avatar src={`https://avatar.alles.cc/${u.id}`} size={50} />
</a>
</Link>
</div>
))}
</div>
)}
<div className="grid grid-cols-2 md:grid-cols-6 gap-3">
{products.map((product, i) =>
product.external ? (
@ -169,4 +188,18 @@ const page = () => {
);
};
page.getInitialProps = async (ctx) => {
try {
return (
await axios.get(`${process.env.NEXT_PUBLIC_ORIGIN}/api/friends`, {
headers: {
Authorization: cookies(ctx).sessionToken,
},
})
).data;
} catch (err) {
return {};
}
};
export default page;

Loading…
Cancel
Save