From 804e3ef37dfec6d98ddc9eeb08b91b7d7c023e00 Mon Sep 17 00:00:00 2001 From: Martin Sosic Date: Fri, 27 Nov 2020 16:10:02 +0100 Subject: [PATCH] Added following other users. --- examples/realworld/ext/UserProfilePage.js | 56 +++++++++-- examples/realworld/ext/actions.js | 17 +++- examples/realworld/ext/queries.js | 14 ++- examples/realworld/main.wasp | 7 ++ .../README.md | 48 +++++++++ .../schema.prisma | 59 +++++++++++ .../steps.json | 97 +++++++++++++++++++ examples/realworld/migrations/migrate.lock | 3 +- 8 files changed, 288 insertions(+), 13 deletions(-) create mode 100644 examples/realworld/migrations/20201127145135-added-following-of-other-users/README.md create mode 100644 examples/realworld/migrations/20201127145135-added-following-of-other-users/schema.prisma create mode 100644 examples/realworld/migrations/20201127145135-added-following-of-other-users/steps.json diff --git a/examples/realworld/ext/UserProfilePage.js b/examples/realworld/ext/UserProfilePage.js index eaf2bcb4b..9470603f6 100644 --- a/examples/realworld/ext/UserProfilePage.js +++ b/examples/realworld/ext/UserProfilePage.js @@ -1,5 +1,6 @@ import React, { useState } from 'react' import { Link, useHistory } from 'react-router-dom' +import moment from 'moment' import useAuth from '@wasp/auth/useAuth.js' import logout from '@wasp/auth/logout.js' @@ -8,6 +9,7 @@ import getUser from '@wasp/queries/getUser' import getArticlesByUser from '@wasp/queries/getArticlesByUser' import getFavoritedArticles from '@wasp/queries/getFavoritedArticles' import setArticleFavorited from '@wasp/actions/setArticleFavorited' +import followUser from '@wasp/actions/followUser' import { useQuery } from '@wasp/queries' import Navbar from './Navbar' @@ -16,6 +18,8 @@ import smileyImageUrl from './smiley.jpg' const UserProfilePage = (props) => { const history = useHistory() + const { data: me } = useAuth() + const username = props.match.params.username const { data: user, error: userError } = useQuery(getUser, { username }) @@ -28,9 +32,6 @@ const UserProfilePage = (props) => { history.push("/") } - // TODO: List My Articles - // TODO: List Favorited Articles - return user ? (
@@ -38,26 +39,52 @@ const UserProfilePage = (props) => {

{ user.username }

{ user.bio }

-
- { /* TODO: Show this link only if user is logged in. */ } - Edit Profile Settings -
+ { me && me.username === username && ( +
+ Edit Profile Settings +
+ )} + { me && me.username !== username && ( +
+ +
+ )}
) : null } +const FollowUserButton = (props) => { + const user = props.user + const { data: me } = useAuth() + + const toggleFollow = async () => { + try { + followUser({ username: user.username, follow: !user.following }) + } catch (err) { + console.error(err) + window.alert(err) + } + } + + return me && me.username !== user.username ? ( + + ) : null +} + const Articles = (props) => { const user = props.user - const { data: myArticles } = useQuery(getArticlesByUser, { username: props.user.username }) + const { data: authoredArticles } = useQuery(getArticlesByUser, { username: props.user.username }) const { data: favoritedArticles } = useQuery(getFavoritedArticles, { username: props.user.username }) return (

My Articles

- +

Favorited Articles

@@ -82,11 +109,20 @@ const Article = (props) => { } return ( -
+

{ article.title }

{ article.description }

+

+ Tags: + { article.tags.map(t => t.name).join('; ') } +

+

+ +

{ article.user.username }
+
{ moment(article.createdAt).format('MMMM DD, YYYY') }
+