From 4f627e2a5c4f31e155f7081e3353f9e0350339fa Mon Sep 17 00:00:00 2001 From: keyan Date: Fri, 21 May 2021 17:32:21 -0500 Subject: [PATCH] check for usernames on typing --- api/resolvers/user.js | 11 +++++- api/typeDefs/user.js | 3 +- components/form.js | 5 ++- components/upvote.js | 2 - components/user-header.js | 65 ++++++++++++++++++++++++++++++++- pages/api/auth/[...nextauth].js | 2 - styles/globals.scss | 1 + 7 files changed, 80 insertions(+), 9 deletions(-) diff --git a/api/resolvers/user.js b/api/resolvers/user.js index ed590ca8..4cab97a4 100644 --- a/api/resolvers/user.js +++ b/api/resolvers/user.js @@ -1,3 +1,5 @@ +import { AuthenticationError } from 'apollo-server-errors' + export default { Query: { me: async (parent, args, { models, me }) => @@ -6,7 +8,14 @@ export default { return await models.user.findUnique({ where: { name } }) }, users: async (parent, args, { models }) => - await models.user.findMany() + await models.user.findMany(), + nameAvailable: async (parent, { name }, { models, me }) => { + if (!me) { + throw new AuthenticationError('you must be logged in') + } + + return me.name === name || !(await models.user.findUnique({ where: { name } })) + } }, User: { diff --git a/api/typeDefs/user.js b/api/typeDefs/user.js index 5788e0ae..6ec10b5d 100644 --- a/api/typeDefs/user.js +++ b/api/typeDefs/user.js @@ -3,8 +3,9 @@ import { gql } from 'apollo-server-micro' export default gql` extend type Query { me: User - user(name: String): User + user(name: String!): User users: [User!] + nameAvailable(name: String!): Boolean! } type User { diff --git a/components/form.js b/components/form.js index 8f8a7584..67a120ce 100644 --- a/components/form.js +++ b/components/form.js @@ -48,11 +48,11 @@ export function InputSkeleton ({ label }) { ) } -export function Input ({ label, prepend, append, hint, ...props }) { +export function Input ({ label, prepend, append, hint, showValid, noBottomMargin, ...props }) { const [field, meta] = props.readOnly ? [{}, {}] : useField(props) return ( - + {label && {label}} {prepend && ( @@ -63,6 +63,7 @@ export function Input ({ label, prepend, append, hint, ...props }) { {append && ( diff --git a/components/upvote.js b/components/upvote.js index 3f98fe25..0e9abf6c 100644 --- a/components/upvote.js +++ b/components/upvote.js @@ -50,9 +50,7 @@ export default function UpVote ({ itemId, meSats, className }) { try { await vote({ variables: { id: itemId, sats: 1 } }) } catch (error) { - console.log(error.toString()) if (error.toString().includes('insufficient funds')) { - console.log('hjo') setError(true) return } diff --git a/components/user-header.js b/components/user-header.js index e147f277..083adb1d 100644 --- a/components/user-header.js +++ b/components/user-header.js @@ -1,12 +1,75 @@ +import { Button } from 'react-bootstrap' +import { useSession } from 'next-auth/client' import Link from 'next/link' import { useRouter } from 'next/router' import Nav from 'react-bootstrap/Nav' +import { useState } from 'react' +import { Form, Input, SubmitButton } from './form' +import InputGroup from 'react-bootstrap/InputGroup' +import * as Yup from 'yup' +import { gql, useApolloClient, useQuery } from '@apollo/client' + +const NAME_QUERY = +gql` + query nameAvailable($name: String!) { + nameAvailable(name: $name) + } +` export default function UserHeader ({ user }) { + const [editting, setEditting] = useState(false) + const [session] = useSession() const router = useRouter() + const client = useApolloClient() + + const Satistics = () =>

[{user.stacked} stacked, {user.sats} sats]

+ + const UserSchema = Yup.object({ + name: Yup.string() + .required('required') + .matches(/^[\w_]+$/, 'only letters, numbers, and _') + .max(32, 'too long') + .test({ + name: 'name', + test: async name => { + if (!name || !name.length) return false + const { data } = await client.query({ query: NAME_QUERY, variables: { name } }) + return data.nameAvailable + }, + message: 'taken' + }) + }) + return ( <> -

@{user.name} [{user.stacked} stacked, {user.sats} sats]

+ {editting + ? ( +
+ @ + name='name' + autoFocus + noBottomMargin + showValid + /> + + setEditting(true)}>save + + ) + : ( +
+

@{user.name}

+ + {session && session.user && session.user.name === user.name && + } +
+ )}