import { initialState, Post, StatusEnum } from '@/app/models/postModel' import Ionicons from '@expo/vector-icons/Ionicons' import { router, useFocusEffect, useLocalSearchParams } from 'expo-router' import React, { useState } from 'react' import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native' export default function DetailsScreen() { const isProfileView = window.location.href.includes('/profile') const [post, setPost] = useState(initialState) const { id } = useLocalSearchParams() console.log(id) useFocusEffect( React.useCallback(() => { // Do something when the screen is focused // TODO: add endpoint to get only non approved or denied status posts fetch(`http://localhost:3000/api/v1/posts/${id}`) .then((res) => res.json()) .then((json) => { console.log(json) setPost(json.data) }) return () => { // Do something when the screen is unfocused // Useful for cleanup functions } }, []) ) async function approvePost(postID: string) { console.log('Approving post ' + postID) await fetch(`http://localhost:3000/api/v1/posts/${postID}`, { method: 'PATCH', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({ status: 'approved' }) }).then(() => { router.back() }) } async function denyPost(postID: string) { console.log('Denying post ' + postID) await fetch(`http://localhost:3000/api/v1/posts/${postID}`, { method: 'PATCH', headers: { Accept: 'application/json, text/plain, */*', 'Content-Type': 'application/json' }, body: JSON.stringify({ status: 'denied' }) }).then(() => { router.back() }) } return ( router.back()}> {post._id} {post.notes} {isProfileView ? ( {post.status === StatusEnum.Created && ( Created )} {post.status === StatusEnum.Pending && ( Pending )} {post.status === StatusEnum.Denied && Denied} {post.status === StatusEnum.Approved && ( Approved )} ) : null} {!isProfileView ? ( denyPost(post._id)}> Deny approvePost(post._id)}> Approve ) : null} ) } const styles = StyleSheet.create({ text: { color: '#fff', justifyContent: 'center' }, container: { backgroundColor: '#373d44ff', padding: 10, flex: 1 }, header: { height: 30 }, statusTag: { paddingVertical: 3, paddingHorizontal: 10, borderRadius: 6 }, created: { backgroundColor: '#0d6efd', color: '#ffffff' }, pending: { backgroundColor: '#ffc107', color: '#000000' }, denied: { backgroundColor: '#dc3545', color: '#ffffff' }, approved: { backgroundColor: '#198754', color: '#ffffff' } })