Skip to content

Commit

Permalink
Merge pull request #7 from InterwebAlchemy/feature/delete-profile
Browse files Browse the repository at this point in the history
feat: delete profiles; stop eslint wanting return await
  • Loading branch information
ericrallen authored Feb 7, 2022
2 parents 0d162cc + c29830d commit dde210e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
"standard-with-typescript",
"prettier"
],
"plugins": ["@typescript-eslint"]
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/return-await": "off"
}
}
22 changes: 21 additions & 1 deletion src/context/UserProfileContext.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useState, useEffect, createContext } from 'react'
import { Auth } from '@supabase/ui'

import { getUserProfile, updateUserProfile, getUserAvatar, logIn, logOut } from '../services/user'
import {
getUserProfile,
updateUserProfile,
deleteUserProfile,
getUserAvatar,
logIn,
logOut,
} from '../services/user'
import {
getPersonalityTypes,
getPersonalityColors,
Expand All @@ -21,6 +28,7 @@ export interface UserProfileContextProvider extends KeysToCamelCase<definitions[
setPersonalityTypeId: React.Dispatch<React.SetStateAction<number | undefined>>
setPersonalityColorId: React.Dispatch<React.SetStateAction<number | undefined>>
updateProfile: () => Promise<void>
deleteProfile: () => Promise<void>
logIn: () => Promise<void>
logOut: () => Promise<void>
}
Expand All @@ -44,6 +52,7 @@ export const UserProfileContext = createContext<UserProfileContextProvider>({
setPersonalityTypeId: () => {},
setPersonalityColorId: () => {},
updateProfile: async () => {},
deleteProfile: async () => {},
logIn: async () => {},
logOut: async () => {},
/* eslint-enable @typescript-eslint/no-empty-function */
Expand Down Expand Up @@ -89,6 +98,16 @@ export const UserProfileProvider = ({
})
}

const deleteProfile = async (): Promise<void> => {
await deleteUserProfile(id).catch((e) => {
if (process.env.NEXT_PUBLIC_FEATURE__DEBUG_LOGS === 'ENABLED') {
console.error(e)
}
})

await logOut()
}

// get user profile for editing when user authenticates
useEffect(() => {
// get psychometric options on mount
Expand Down Expand Up @@ -168,6 +187,7 @@ export const UserProfileProvider = ({
personalityColorId,
personalityTypeId,
updateProfile,
deleteProfile,
personalityTypes,
personalityColors,
enneagramTypes,
Expand Down
6 changes: 1 addition & 5 deletions src/pages/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ import Website from '../components/EditProfile/Website'
import useProfile from '../hooks/useProfile'

const Profile: NextPage = (): React.ReactElement => {
const { updateProfile } = useProfile()

const deleteProfile = (): void => {
alert('This functionality is coming soon!')
}
const { updateProfile, deleteProfile } = useProfile()

return (
<Page authenticated>
Expand Down
26 changes: 17 additions & 9 deletions src/services/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export const logIn = async (): Promise<void> => {
export const getUserProfile = async (
userId: definitions['profiles']['id']
): Promise<PostgrestSingleResponse<definitions['profiles']>> => {
return await supabase
return supabase
.from<definitions['profiles']>('profiles')
.select(
`username, website, communication_style, personality_type_id, personality_color_id, enneagram_type_id`
Expand All @@ -111,7 +111,7 @@ export const getFullUserProfile = async (
username?: string
): Promise<PostgrestSingleResponse<FullUserProfile> | null> => {
if (typeof username !== 'undefined') {
return await supabase
return supabase
.from<FullUserProfile>('profiles')
.select(
`username, website, communication_style, personality:personality_type_id(type, name, description, url), color:personality_color_id(name, description, url), enneagram:enneagram_type_id(name, number, description, url)`
Expand All @@ -131,18 +131,26 @@ export const updateUserProfile = async (
throw new Error('Must provide a valid userId String.')
}

return await supabase
.from<definitions['profiles']>('profiles')
.update(profile)
.match({ id: userId })
return supabase.from<definitions['profiles']>('profiles').update(profile).match({ id: userId })
}

export const deleteUserProfile = async (): Promise<unknown> => {
return await supabase.from<definitions['profiles']>('profiles').delete()
export const deleteUserProfile = async (userId: definitions['profiles']['id']): Promise<void> => {
try {
await supabase
.from<definitions['profiles']>('profiles')
.delete({ returning: 'minimal' })
.match({ id: userId })

await supabase.storage.from('avatars').remove([`public/${userId}.png`])
} catch (e) {
if (process.env.NEXT_PUBLIC_FEATURE__DEBUG_LOGS === 'ENABLED') {
console.error(e)
}
}
}

export const getFeaturedProfiles = async (): Promise<
PostgrestResponse<Pick<definitions['profiles'], 'username'>>
> => {
return await supabase.rpc('featured_users', { user_count: 5 })
return supabase.rpc('featured_users', { user_count: 5 })
}

1 comment on commit dde210e

@ericrallen
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for work-with-me ready!

✅ Preview
https://work-with-me-mqsqunzi5-interweb-alchemy.vercel.app
https://work-with-me.vercel.app

Built with commit dde210e.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.