Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle image uploads #4

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion app/(app)/post/[id]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {useAppLogic} from '../../../../src/providers/AppLogicProvider';
import {fetchDeletePost, fetchPostById} from '../../../../src/apis/postQueries';
import {supabase} from '../../../../src/supabase';
import {toggleLike} from '../../../../src/apis/likeQueries';
import ParsedText from 'react-native-parsed-text';
import ImageCarousel from '../../../../src/components/uis/ImageCarousel';

const Container = styled.View`
background-color: ${({theme}) => theme.bg.basic};
Expand Down Expand Up @@ -220,7 +222,30 @@ export default function PostDetails(): JSX.Element {
</View>
</Pressable>
) : null}
<Typography.Body1>{post.content}</Typography.Body1>
<ParsedText
parse={[
{
type: 'url',
onPress: (url) => openURL(url),
style: css`
color: ${theme.role.link};
`,
},
]}
selectable
style={css`
color: ${theme.text.basic};
font-size: 16px;
line-height: 22.8px;
`}
>
{post.content}
</ParsedText>

{post.images && post.images.length > 0 ? (
<ImageCarousel borderRadius={8} images={post.images} />
) : null}

<ControlItem
hasLiked={hasLiked}
likeCnt={postLikes}
Expand Down
38 changes: 33 additions & 5 deletions app/(app)/post/[id]/replies.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import {t} from '../../../../src/STRINGS';
import ReplyItem from '../../../../src/components/uis/ReplyItem';
import ReplyInput from '../../../../src/components/uis/ReplyInput';
import {ImagePickerAsset} from 'expo-image-picker';
import {supabase} from '../../../../src/supabase';
import {
getPublicUrlFromPath,
supabase,
uploadFileToSupabase,
} from '../../../../src/supabase';
import {
fetchCreateReply,
fetchReplyById,
Expand Down Expand Up @@ -40,6 +44,7 @@ export default function Replies({
const [page, setPage] = useState(0);
const [replies, setReplies] = useState<ReplyWithJoins[]>([]);
const [loadingMore, setLoadingMore] = useState(false);
const [isCreateReplyInFlight, setIsCreateReplyInFlight] = useState(false);

const {error, isValidating, mutate} = useSWR<ReplyWithJoins[]>(
['replies', postId, page],
Expand Down Expand Up @@ -115,16 +120,37 @@ export default function Replies({
const handleCreateReply = async () => {
if (!authId || !postId) return;

setIsCreateReplyInFlight(true);

try {
const imageUploadPromises = assets.map(async (asset) => {
const destPath = `${asset.type === 'video' ? 'videos' : 'images'}/${Date.now()}_${asset.fileName}`;
const file = asset.uri;

return await uploadFileToSupabase({
uri: file,
fileType: asset.type === 'video' ? 'Video' : 'Image',
bucket: 'images',
destPath,
});
});

const images = await Promise.all(imageUploadPromises);

const newReply = await fetchCreateReply({
reply: {
message: reply,
user_id: authId,
post_id: postId,
},
images: assets.map((asset) => ({
url: asset.uri,
})),
images: images
.filter((el) => !!el)
.map((el) => ({
...el,
image_url: el?.image_url
? getPublicUrlFromPath(el.image_url)
: undefined,
})),
});

mutate(newReply ? [newReply, ...replies] : replies, false);
Expand All @@ -133,6 +159,7 @@ export default function Replies({
} finally {
setReply('');
setAssets([]);
setIsCreateReplyInFlight(false);
}
};

Expand All @@ -144,7 +171,7 @@ export default function Replies({

const handleRefresh = () => {
setReplies([]);
setPage(1);
setPage(0);
mutate();
};

Expand Down Expand Up @@ -264,6 +291,7 @@ export default function Replies({
style={css`
padding: 0;
`}
loading={isCreateReplyInFlight}
styles={{
container: css`
border-radius: 0;
Expand Down
Loading
Loading