|
| 1 | +import React from "react"; |
| 2 | +import { NextPage } from "next"; |
| 3 | +import { useRouter } from "next/router"; |
| 4 | +import { Auth, Loading } from "@supabase/ui"; |
| 5 | +import { useSupabaseClient } from "../lib/supabase"; |
| 6 | +import { DocumentType, gql } from "../gql"; |
| 7 | +import { CombinedError, useMutation, useQuery } from "urql"; |
| 8 | + |
| 9 | +const UserProfileQuery = gql(/* GraphQL */ ` |
| 10 | + query UserProfileQuery($profileId: UUID!) { |
| 11 | + profileCollection(filter: { id: { eq: $profileId } }) { |
| 12 | + edges { |
| 13 | + node { |
| 14 | + id |
| 15 | + username |
| 16 | + website |
| 17 | + } |
| 18 | + } |
| 19 | + } |
| 20 | + } |
| 21 | +`); |
| 22 | + |
| 23 | +const Account: NextPage = () => { |
| 24 | + const session = Auth.useUser(); |
| 25 | + const [profileQuery] = useQuery({ |
| 26 | + query: UserProfileQuery, |
| 27 | + variables: { profileId: session.user?.id }, |
| 28 | + pause: session.user === null, |
| 29 | + }); |
| 30 | + const router = useRouter(); |
| 31 | + |
| 32 | + const profile = profileQuery.data?.profileCollection?.edges?.[0].node; |
| 33 | + |
| 34 | + React.useEffect(() => { |
| 35 | + if (session.user === null || (profileQuery.data && profile === null)) { |
| 36 | + router.replace("/login"); |
| 37 | + } |
| 38 | + }, [session.user, profile, profileQuery.data]); |
| 39 | + |
| 40 | + if (profile) { |
| 41 | + return <AccountForm profile={profile} />; |
| 42 | + } |
| 43 | + |
| 44 | + return null; |
| 45 | +}; |
| 46 | + |
| 47 | +const ProfileFragment = gql(/* GraphQL */ ` |
| 48 | + fragment AccountProfileFragment on Profile { |
| 49 | + id |
| 50 | + username |
| 51 | + website |
| 52 | + } |
| 53 | +`); |
| 54 | + |
| 55 | +const UpdateProfileMutation = gql(/* GraphQL */ ` |
| 56 | + mutation updateProfile( |
| 57 | + $userId: UUID! |
| 58 | + $newUsername: String! |
| 59 | + $newWebsite: String! |
| 60 | + ) { |
| 61 | + updateProfileCollection( |
| 62 | + filter: { id: { eq: $userId } } |
| 63 | + set: { username: $newUsername, website: $newWebsite } |
| 64 | + ) { |
| 65 | + affectedCount |
| 66 | + records { |
| 67 | + id |
| 68 | + username |
| 69 | + website |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +`); |
| 74 | + |
| 75 | +function extractExpectedGraphQLErrors( |
| 76 | + error: CombinedError | undefined |
| 77 | +): null | string { |
| 78 | + if (error === undefined) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + for (const graphQLError of error.graphQLErrors) { |
| 83 | + if (graphQLError.message.includes("usernamelength")) { |
| 84 | + return "Username must have a minimum length of 3 characters."; |
| 85 | + } |
| 86 | + if (graphQLError.message.includes("Profile_username_key")) { |
| 87 | + return "The name is already taken."; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return null; |
| 92 | +} |
| 93 | + |
| 94 | +function AccountForm(props: { profile: DocumentType<typeof ProfileFragment> }) { |
| 95 | + const supabaseClient = useSupabaseClient(); |
| 96 | + const [username, setUsername] = React.useState(props.profile.username ?? ""); |
| 97 | + const [website, setWebsite] = React.useState(props.profile.website ?? ""); |
| 98 | + const [updateProfileMutation, updateProfile] = useMutation( |
| 99 | + UpdateProfileMutation |
| 100 | + ); |
| 101 | + |
| 102 | + const errorState = extractExpectedGraphQLErrors(updateProfileMutation.error); |
| 103 | + |
| 104 | + return ( |
| 105 | + <> |
| 106 | + <div className="form-widget"> |
| 107 | + <div> |
| 108 | + <label htmlFor="username">Name</label> |
| 109 | + <input |
| 110 | + id="username" |
| 111 | + type="text" |
| 112 | + value={username} |
| 113 | + onChange={(e) => setUsername(e.target.value)} |
| 114 | + /> |
| 115 | + </div> |
| 116 | + <div> |
| 117 | + <label htmlFor="website">Website</label> |
| 118 | + <input |
| 119 | + id="website" |
| 120 | + type="website" |
| 121 | + value={website || ""} |
| 122 | + onChange={(e) => setWebsite(e.target.value)} |
| 123 | + /> |
| 124 | + </div> |
| 125 | + |
| 126 | + <div> |
| 127 | + <button |
| 128 | + className="button block primary" |
| 129 | + onClick={() => |
| 130 | + updateProfile({ |
| 131 | + userId: props.profile.id, |
| 132 | + newUsername: username, |
| 133 | + newWebsite: website, |
| 134 | + }) |
| 135 | + } |
| 136 | + disabled={updateProfileMutation.fetching} |
| 137 | + > |
| 138 | + {updateProfileMutation.fetching ? "Loading ..." : "Update"} |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + |
| 142 | + <div>{errorState}</div> |
| 143 | + |
| 144 | + <div> |
| 145 | + <button |
| 146 | + className="button block" |
| 147 | + onClick={() => supabaseClient.auth.signOut()} |
| 148 | + > |
| 149 | + Sign Out |
| 150 | + </button> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + </> |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export default Account; |
0 commit comments