forked from Memmy-App/memmy
-
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
Showing
30 changed files
with
462 additions
and
343 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
File renamed without changes.
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,92 @@ | ||
import { useEffect, useRef } from "react"; | ||
import { NativeStackNavigationProp } from "@react-navigation/native-stack"; | ||
import { useNavigation } from "@react-navigation/native"; | ||
import { trigger } from "react-native-haptic-feedback"; | ||
import { useToast } from "native-base"; | ||
import { UseFeed, useFeed } from "./useFeed"; | ||
import { useAppDispatch, useAppSelector } from "../../../store"; | ||
import { selectPost } from "../../../slices/post/postSlice"; | ||
import { subscribeToCommunity } from "../../../slices/communities/communitiesActions"; | ||
|
||
interface UseCommunityFeed { | ||
feed: UseFeed; | ||
|
||
onSubscribePress: () => void; | ||
onAboutPress: () => void; | ||
onPostPress: () => void; | ||
} | ||
|
||
const useCommunityFeed = (communityFullName: string): UseCommunityFeed => { | ||
// Global state | ||
const { post } = useAppSelector(selectPost); | ||
|
||
// Refs | ||
const creatingPost = useRef<boolean>(false); | ||
const lastPost = useRef<number>(0); | ||
|
||
// Hooks | ||
const feed = useFeed(communityFullName); | ||
|
||
// Other hooks | ||
const dispatch = useAppDispatch(); | ||
const navigation = useNavigation<NativeStackNavigationProp<any>>(); | ||
const toast = useToast(); | ||
|
||
useEffect(() => { | ||
if (creatingPost.current && post && lastPost.current !== post.post.id) { | ||
creatingPost.current = false; | ||
|
||
setTimeout(() => { | ||
navigation.push("Post"); | ||
}, 500); | ||
} | ||
}, [post]); | ||
|
||
// Events | ||
const onSubscribePress = () => { | ||
trigger("impactMedium"); | ||
|
||
toast.show({ | ||
title: `${!feed.subscribed ? "Subscribed to" : "Unsubscribed from"} ${ | ||
feed.community.community.name | ||
}`, | ||
duration: 3000, | ||
}); | ||
|
||
dispatch( | ||
subscribeToCommunity({ | ||
communityId: feed.community.community.id, | ||
subscribe: !feed.subscribed, | ||
}) | ||
); | ||
}; | ||
|
||
const onAboutPress = () => { | ||
navigation.push("CommunityAbout", { | ||
name: feed.community.community.name, | ||
banner: feed.community.community.banner, | ||
description: feed.community.community.description, | ||
title: feed.community.community.title, | ||
}); | ||
}; | ||
|
||
const onPostPress = () => { | ||
creatingPost.current = true; | ||
lastPost.current = post ? post.post.id : 0; | ||
|
||
navigation.push("NewPost", { | ||
communityId: feed.community.community.id, | ||
communityName: feed.community.community.name, | ||
}); | ||
}; | ||
|
||
return { | ||
feed, | ||
|
||
onSubscribePress, | ||
onAboutPress, | ||
onPostPress, | ||
}; | ||
}; | ||
|
||
export default useCommunityFeed; |
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,142 @@ | ||
import { useRef, useState } from "react"; | ||
import { Alert, TextInput } from "react-native"; | ||
import { NativeStackNavigationProp } from "@react-navigation/native-stack"; | ||
import { useNavigation } from "@react-navigation/native"; | ||
import { CreatePost } from "lemmy-js-client"; | ||
import { useAppDispatch } from "../../../store"; | ||
import { selectImage } from "../../../helpers/ImageHelper"; | ||
import uploadToImgur from "../../../helpers/ImgurHelper"; | ||
import { writeToLog } from "../../../helpers/LogHelper"; | ||
import { lemmyAuthToken, lemmyInstance } from "../../../lemmy/LemmyInstance"; | ||
import { setPost } from "../../../slices/post/postSlice"; | ||
|
||
interface UseNewPost { | ||
loading; | ||
error; | ||
|
||
form: { | ||
body: string; | ||
name: string; | ||
url: string; | ||
nsfw: boolean; | ||
}; | ||
|
||
inputRef: React.MutableRefObject<TextInput>; | ||
|
||
onFormChange: (name: string, value: string | boolean) => void; | ||
|
||
doUpload: () => Promise<void>; | ||
doSubmit: () => Promise<void>; | ||
} | ||
|
||
const useNewPost = (communityId: number): UseNewPost => { | ||
// State | ||
const [loading, setLoading] = useState<boolean>(false); | ||
const [error, setError] = useState<string | undefined>(undefined); | ||
|
||
const [form, setForm] = useState({ | ||
body: "", | ||
name: "", | ||
url: "", | ||
nsfw: false, | ||
}); | ||
|
||
// Refs | ||
const inputRef = useRef<TextInput>(); | ||
|
||
// Other hooks | ||
const navigation = useNavigation<NativeStackNavigationProp<any>>(); | ||
const dispatch = useAppDispatch(); | ||
|
||
const onFormChange = (name: string, value: string | boolean) => { | ||
setForm({ | ||
...form, | ||
[name]: value, | ||
}); | ||
}; | ||
|
||
const doUpload = async () => { | ||
setError(undefined); | ||
let path; | ||
|
||
try { | ||
path = await selectImage(); | ||
} catch (e) { | ||
if (e === "permissions") { | ||
Alert.alert( | ||
"Permissions Error", | ||
"Please allow Memmy to access your camera roll." | ||
); | ||
} | ||
|
||
return; | ||
} | ||
|
||
setLoading(true); | ||
|
||
let imgurLink; | ||
|
||
try { | ||
imgurLink = await uploadToImgur(path); | ||
} catch (e) { | ||
writeToLog("Error uploading image."); | ||
writeToLog(e.toString()); | ||
|
||
setLoading(false); | ||
setError(e.toString()); | ||
Alert.alert("Error", "Error uploading image to Imgur."); | ||
} | ||
|
||
onFormChange("url", imgurLink); | ||
setLoading(false); | ||
}; | ||
|
||
const doSubmit = async () => { | ||
try { | ||
setLoading(true); | ||
setError(undefined); | ||
|
||
const prepared: CreatePost = { | ||
...form, | ||
name: form.name !== "" ? form.name : undefined, | ||
body: form.body !== "" ? form.body : undefined, | ||
url: form.url !== "" ? form.url : undefined, | ||
auth: lemmyAuthToken, | ||
language_id: 37, | ||
community_id: communityId, | ||
nsfw: form.nsfw, | ||
}; | ||
|
||
const res = await lemmyInstance.createPost(prepared); | ||
|
||
setLoading(false); | ||
|
||
dispatch(setPost(res.post_view)); | ||
|
||
navigation.pop(); | ||
} catch (e) { | ||
writeToLog("Error submitting post."); | ||
writeToLog(e.toString()); | ||
|
||
setLoading(false); | ||
setError(e.toString()); | ||
Alert.alert(e.toString()); | ||
} | ||
}; | ||
|
||
return { | ||
loading, | ||
error, | ||
|
||
form, | ||
|
||
inputRef, | ||
|
||
onFormChange, | ||
|
||
doUpload, | ||
doSubmit, | ||
}; | ||
}; | ||
|
||
export default useNewPost; |
Oops, something went wrong.