-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08cd215
commit 4d99437
Showing
20 changed files
with
741 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { QueryFunction } from "react-query" | ||
import { AVATAR_BUCKET } from "../constants" | ||
import { supabaseClient } from "./supabaseClient" | ||
|
||
export const fetchAvatar: QueryFunction<string | undefined> = async ({ queryKey }) => { | ||
const [_key, path] = queryKey | ||
const { data, error } = await supabaseClient.storage | ||
.from(AVATAR_BUCKET) | ||
.download(path as string) | ||
|
||
if(error) { | ||
throw new Error(error.message) | ||
} | ||
|
||
if(!data) { | ||
return undefined | ||
} | ||
|
||
return URL.createObjectURL(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { QueryFunction } from "react-query/types/core/types" | ||
import { FAVORITES_TABLE } from "../constants" | ||
import { supabaseClient } from "./supabaseClient" | ||
|
||
export const toggleFavorite: QueryFunction<boolean> = () => { | ||
|
||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import dayjs from 'dayjs'; | ||
import { QueryFunction } from 'react-query'; | ||
import { supabaseClient } from '../api/supabaseClient'; | ||
import { TWEETS_TABLE } from '../constants'; | ||
import { definitions } from './types'; | ||
|
||
type TweetResponse = { | ||
id: number | ||
content: string | ||
createdAt: string, | ||
favorited_users: { id: string, username: string }[] | ||
tweet_author: definitions["profiles"] | ||
} | ||
|
||
export type Tweet = { | ||
id: number | ||
isFavorited: boolean | ||
favorites: number | ||
favoritedBy: definitions["profiles"][] | ||
author: definitions["profiles"] | ||
createdAt: string | ||
content: string | ||
} | ||
|
||
type AddTweetRequestBody = { | ||
userId: string | ||
content: string | ||
} | ||
|
||
export const fetchTweets: QueryFunction<Tweet[], [string, string | undefined, string | undefined]> = async ({ queryKey }) => { | ||
const [_key, loggedInUserId, userIdToFilterTweetsBy] = queryKey | ||
|
||
const query = userIdToFilterTweetsBy ? | ||
supabaseClient.rpc<TweetResponse>('get_tweets', { u_id: userIdToFilterTweetsBy }) : | ||
supabaseClient.rpc<TweetResponse>('get_tweets') | ||
|
||
let { data, error } = await query | ||
|
||
if(error) { | ||
throw new Error(error.message); | ||
} | ||
if(!data) { | ||
return [] | ||
} | ||
|
||
return data.map(fromResponseToTweet(loggedInUserId as string | undefined)) | ||
} | ||
|
||
const fromResponseToTweet = (loggedInUserId?: string, )=>(response: TweetResponse): Tweet => { | ||
const { id, content, createdAt, favorited_users, tweet_author } = response; | ||
return { | ||
id, | ||
content, | ||
createdAt: dayjs(createdAt).format(`DD MMM`), | ||
favoritedBy: favorited_users, | ||
author: tweet_author, | ||
isFavorited: favorited_users?.findIndex(u => u.id === loggedInUserId) > -1, | ||
favorites: favorited_users ? favorited_users.length : 0 | ||
} | ||
} | ||
|
||
export const createTweet = async (tweet: AddTweetRequestBody) => { | ||
const { data, error } = await supabaseClient | ||
.from<Tweet>(TWEETS_TABLE) | ||
.insert(tweet) | ||
|
||
if(error) { | ||
throw new Error(error.message) | ||
} | ||
|
||
return data || [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.