import { useUser } from '@clerk/clerk-react' import React from 'react' import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native' import { Post, StatusEnum } from '../models/postModel' type PostComponentProps = { post: Post fetchData: () => void } export const PostComponent: React.FC = ({ post, fetchData }) => { const { user } = useUser() 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(() => { fetchData() }) } 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(() => { fetchData() }) } return ( {post._id} {post.notes} {user?.publicMetadata.role !== 'admin' && ( {post.status === StatusEnum.Created && Created} {post.status === StatusEnum.Pending && Pending} {post.status === StatusEnum.Denied && Denied} {post.status === StatusEnum.Accepted && Accepted} )} {user?.publicMetadata.role === 'admin' && ( denyPost(post._id)}> Deny approvePost(post._id)}> Approve )} ) } const styles = StyleSheet.create({ text: { color: '#fff', justifyContent: 'center' }, posts: { marginTop: 10, backgroundColor: '#373d44ff', borderColor: '#626e7aff', borderStyle: 'solid', borderWidth: 1, borderRadius: 12, padding: 10, width: '100%' }, created: { color: 'white' }, pending: { color: 'yellow' }, denied: { color: 'red' }, accepted: { color: 'green' } })