Skip to content

Commit

Permalink
Uploading works
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Sep 24, 2024
1 parent 60aadfe commit 342d22d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 47 deletions.
49 changes: 32 additions & 17 deletions website/src/components/modal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {ChangeEvent, useEffect, useRef, useState} from "react";
import styles from "@/styles/components/modal.module.css"
import {fetchPlant, macronCodeToChar, numberDictionary} from "@/lib/plant_data";
import {makeCachedRequest, makeRequestWithToken} from "@/lib/api_tools";
import {makeCachedRequest, makeRequestWithToken, removeCachedRequest} from "@/lib/api_tools";
import {getFilePath, getPostImage} from "@/lib/data";
import {UserCard} from "@/pages/media/components/cards";
import {RongoaUser} from "@/lib/users";
Expand Down Expand Up @@ -150,19 +150,24 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {
// Reverse the posts
posts.reverse();

setThisImages(posts.filter((post: any) => post.in_use))
setPostImages(posts.filter((post: any) => !post.in_use))
setThisImages(posts.filter((post: any) => post.post_in_use))
setPostImages(posts.filter((post: any) => !post.post_in_use))
setCurrentDisplayImages(posts.filter((post: any) => post.post_in_use))

}


let cSelectedImages = [[], Array(posts.length).fill(false), Array(plantUserPosts.length).fill(false)]
setSelectedImages(cSelectedImages)


}


const updateSelectedImages = (imageIndex: number) => {
const updateSelectedImages = (imageIndex: number, tab = activeTab) => {

let newSelectedImages = selectedImages
newSelectedImages[activeTab][imageIndex] = !newSelectedImages[activeTab][imageIndex]
newSelectedImages[tab][imageIndex] = !newSelectedImages[tab][imageIndex]

// Set the current image
let currentImage : any = currentDisplayImages[imageIndex]
Expand All @@ -172,33 +177,33 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {
setCurrentImagePlant((plantNames[plantIDs.indexOf(currentImage.post_plant_id)]))
setCurrentImageDate(new Date(currentImage.post_date).toLocaleString())

// If the current tab is the post gallery, update the my posts tab if its the same image
if(activeTab === 1){
// If the current tab is the post gallery
if(tab === 1){
let myPostIndex = myImages.findIndex((post: any) => post.id === currentImage.id)
if(myPostIndex !== -1){
newSelectedImages[2][myPostIndex] = newSelectedImages[activeTab][imageIndex]
newSelectedImages[2][myPostIndex] = newSelectedImages[tab][imageIndex]
}

// My Images
}else if(activeTab === 2){
}else if(tab === 2){

// Update the post gallery tab if its the same image
let postIndex = postImages.findIndex((post: any) => post.id === currentImage.id)
if(postIndex !== -1){
newSelectedImages[1][postIndex] = newSelectedImages[activeTab][imageIndex]
newSelectedImages[1][postIndex] = newSelectedImages[tab][imageIndex]
}

// Update the this plant tab if its the same image
let thisIndex = thisImages.findIndex((post: any) => post.id === currentImage.id)
if(thisIndex !== -1){
newSelectedImages[0][thisIndex] = newSelectedImages[activeTab][imageIndex]
newSelectedImages[0][thisIndex] = newSelectedImages[tab][imageIndex]
}
}else{

// Update the my posts tab if its the same image
let myPostIndex = myImages.findIndex((post: any) => post.id === currentImage.id)
if(myPostIndex !== -1){
newSelectedImages[2][myPostIndex] = newSelectedImages[activeTab][imageIndex]
newSelectedImages[2][myPostIndex] = newSelectedImages[tab][imageIndex]
}
}

Expand Down Expand Up @@ -305,7 +310,6 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {
const file = localFiles[localImages.indexOf(image)]
console.log(file)


const titleInput = document.getElementById("title") as HTMLElement;
const title = titleInput.innerText;
if(title === "Please Type a Title Here" || title === "No Image Selected") {
Expand All @@ -324,8 +328,12 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {
const userName = user.database.user_name

// Upload the image
await postImage(file, title, id, userID, userName, setLoadingMessage, true)
setLoadingMessage("")
const newPost = await postImage(file, title, id, userID, userName, setLoadingMessage, true)

if(!newPost)
return

console.log("New Post", newPost)

// Remove the image from the local images
const index = localImages.indexOf(image)
Expand All @@ -334,11 +342,18 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {

// Reset the current image
resetCurrentImage()
sessionStorage.removeItem("editor_posts_"+id)
sessionStorage.removeItem("editor_posts_mine_"+id)

setMyImages((prev) => [...prev, newPost])
setThisImages((prev) => [...prev, newPost])
setSelectedImages((prev) => { console.log(prev); return prev})
setLoadingMessage("")
}

const moveImages = async () => {

// Find the ids
// Find th

}

Expand Down Expand Up @@ -381,7 +396,7 @@ export function ImagePopup({show, hideCallback, id = 0}: ImagePopupProps) {
<div>
<h1
id={"title"}
contentEditable={activeTab == 3}
contentEditable={activeTab == 3 && currentImageName !== "No Image Selected"}
>{currentImageName}</h1>
<h2>Description</h2>
</div>
Expand Down
4 changes: 4 additions & 0 deletions website/src/lib/api_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ export async function makeCachedRequest(key: string, url: string){
saveToCache(key, cache.data.data)
}
return cache.data.data
}

export function removeCachedRequest(key: string){
return saveToCache(key, null)
}
4 changes: 3 additions & 1 deletion website/src/lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ export function getFilePath(userId: number, postId: number, fileName: string): s

}

export function getPostImage(post): string {
export function getPostImage(post: any): string {

if(!post) return "";

// Base
let path = process.env.NEXT_PUBLIC_FTP_PUBLIC_URL + "/users/"
Expand Down
48 changes: 23 additions & 25 deletions website/src/pages/api/posts/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,32 @@ export default async function handler(
const timeFunction = USE_POSTGRES ? "to_timestamp" : "FROM_UNIXTIME";

// Run the query
const query = `INSERT INTO posts (${tables.post_title}, ${tables.post_plant_id}, ${tables.post_user_id}, ${tables.post_image}, ${tables.post_date}, ${tables.post_approved}, ${tables.post_in_use}) VALUES ('${title}', ${plant}, ${user_id}, '${image}', ${timeFunction}(${Date.now()} / 1000.0), ${!user_is_member}, ${post_in_use} ) RETURNING id;`;
const data = await makeQuery(query, client, true)

console.log("DATA")
console.log(data)
if(!data)
return response.status(500).json({ error: "No data returned" });

// Get the id of the new plant
// @ts-ignore (has to be like this data[0] is an object)
let id = undefined

if(USE_POSTGRES){
id = data[0].rows[0].id;
}else{

// Loop through the data
data.forEach((item: any) => {

// If there is an id, set it
if (item && item.id) {
id = item.id;
}
});
const query = `INSERT INTO posts (${tables.post_title}, ${tables.post_plant_id}, ${tables.post_user_id}, ${tables.post_image}, ${tables.post_date}, ${tables.post_approved}, ${tables.post_in_use}) VALUES ('${title}', ${plant}, ${user_id}, '${image}', ${timeFunction}(${Date.now()} / 1000.0), ${!user_is_member}, ${post_in_use} ) ${process.env.USING_MYSQL == "true" ? '' : 'RETURNING id;'}`;
const data = await makeQuery(query, client, true);

let id;
if (process.env.USING_MYSQL == "true") {
// Workaround for MySQL to get the last inserted ID
const lastInsertIdQuery = 'SELECT LAST_INSERT_ID() as id;';
const lastInsertIdData = await makeQuery(lastInsertIdQuery, client, true);
id = lastInsertIdData[0].id;
} else {
if(USE_POSTGRES){
id = data[0].rows[0].id;
}else{

// Loop through the data
data.forEach((item: any) => {

// If there is an id, set it
if (item && item.id) {
id = item.id;
}
});
}
}



// If there is no id, return an error
if(!id) {
return response.status(500).json({ error: "Error creating plant (id not returned)" });
Expand Down
15 changes: 11 additions & 4 deletions website/src/pages/media/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,6 @@ export const postImage = async (image: File, postTitle: string, currentPlant: nu
// Send the post data to the server
const response = await makeRequestWithToken('post', '/api/posts/new?title=' + post_title + '&plant=' + post_plant_id + '&image=' + plant_image + '&inUse=' + inUse);

console.log(response)
return
console.log(userID)

// Get the post id
const newId = response.data.id

Expand Down Expand Up @@ -253,4 +249,15 @@ export const postImage = async (image: File, postTitle: string, currentPlant: nu
console.log(uids)

const response2 = await makeRequestWithToken('post', '/api/user/notifications?operation=send_notification' + uids + '&title=New Post&body=' + userName + ' has made a new post&image=' + getFilePath(userID, newId, plant_image));
const newpost = {
post_title: post_title,
post_plant_id: post_plant_id,
post_user_id: userID,
post_image: plant_image,
post_date: Date.now(),
post_approved: true,
post_in_use: inUse,
id: newId
}
return newpost
}

0 comments on commit 342d22d

Please sign in to comment.