Skip to content

Commit

Permalink
Display Now Working
Browse files Browse the repository at this point in the history
  • Loading branch information
maxtyson123 committed Sep 26, 2024
1 parent 1e052a1 commit 6ba066b
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 157 deletions.
2 changes: 1 addition & 1 deletion server/my_sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ CREATE TABLE IF NOT EXISTS plants (
long_description TEXT,
author TEXT,
last_modified DATE,
display_image TEXT,
display_images TEXT,
plant_type TEXT,
published BOOLEAN,
PRIMARY KEY (id)
Expand Down
2 changes: 1 addition & 1 deletion website/src/components/credits.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function CreditedImage({url, alt, credits, colour = "gray"}: CreditedImag
src={url}
alt={alt}
fill
style={{objectFit: "contain"}}
style={{objectFit: "cover"}}
/>
<p className={styles.credits + " " + backgroundColour}> <FontAwesomeIcon icon={faCopyright}/> {credits} </p>
</>
Expand Down
66 changes: 66 additions & 0 deletions website/src/components/image.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React, { useState } from "react"
import styles from "@/styles/components/image.module.css"
import {ModalImage} from "@/components/modal";
import {CreditedImage} from "@/components/credits";
import {images} from "next/dist/build/webpack/config/blocks/images";


export interface ImageArrayProps {
images: {url: string, name: string, credits: string, description: string}[]
colour?: string
enableModal?: boolean
}

export function ImageArray(props: ImageArrayProps) {
const [currentImage, setCurrentImage] = useState(0)

// Image slide show
const nextImage = () => {
if(currentImage === props.images.length - 1) {
setCurrentImage(0)
} else {
setCurrentImage(currentImage + 1)
}
}

const previousImage = () => {
if(currentImage === 0) {
setCurrentImage(props.images.length - 1)
} else {
setCurrentImage(currentImage - 1)
}
}

return(
<>
<div className={styles.imageContainer}>
{props.enableModal ?
<ModalImage
url={props.images[currentImage].url}
description={props.images[currentImage].description}
>
<CreditedImage
url={props.images[currentImage].url}
alt={props.images[currentImage].name}
credits={props.images[currentImage].credits}
/>
</ModalImage>
:
<CreditedImage url={props.images[currentImage].url} alt={props.images[currentImage].name} credits={props.images[currentImage].credits}/>
}
</div>
{props.images.length > 1 &&
<div className={styles.imageControls}>
<button onClick={previousImage}>&#60;</button>
<div className={styles.dotsContainer}>
{props.images.map((image, index) => {
return <button key={index} onClick={() => setCurrentImage(index)} className={styles.dot + " " + (currentImage === index ? styles.active : "")}/>
})}
</div>
<button onClick={nextImage}>&#62;</button>
</div>
}
</>
)

}
68 changes: 49 additions & 19 deletions website/src/components/plant_sections.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {IconDefinition} from "@fortawesome/fontawesome-svg-core";
import {CreditedImage} from "@/components/credits";
import {ModalImage} from "@/components/modal";
import {getPostImage} from "@/lib/data";
import {ImageArray} from "@/components/image";

interface AutoSectionProps{
section: any
Expand Down Expand Up @@ -82,7 +83,6 @@ interface EdibleSectionProps{
* @param {EdibleSectionData} props.section - The section data.
* @param {boolean} props.isLeft - Whether the image should be on the left or right side of the page.
*
* @see {@link AdvancedTextArea} - The advanced text area component.
*
* @returns {JSX.Element} The rendered section component.
*/
Expand All @@ -93,16 +93,26 @@ export function EdibleSection({section, isLeft} : EdibleSectionProps){
const preparationRef = useRef<HTMLParagraphElement>(null)

const hasImage = section.images.length > 0
const imageURI = hasImage ? getPostImage(section.images[0]) : "/media/images/default_noImage.png"
const imageAlt = hasImage ? section.images[0].post_title : "No Image"
const imageCredits = hasImage ? section.images[0].post_user_id.toString() : ""
const images = hasImage ?
section.images.map((image: any) => {
return {
url: getPostImage(image),
name: image.post_title,
credits: image.post_user_id.toString(),
description: image.post_title
}
})
: [{
url: "/media/images/default_noImage.png",
name: "No Image",
credits: "",
description: ""
}]

const imageDiv = (
<>
<div className={styles.imageContainer}>
<ModalImage url={imageURI} description={imageAlt}>
<CreditedImage url={imageURI} alt={imageAlt} credits={imageCredits}/>
</ModalImage>
<ImageArray images={images} enableModal/>
</div>
</>
)
Expand Down Expand Up @@ -165,16 +175,26 @@ export function MedicalSection({section, isLeft} : MedicalSectionProps){

// TODO COMPONETIZE THIS
const hasImage = section.images.length > 0
const imageURI = hasImage ? getPostImage(section.images[0]) : "/media/images/default_noImage.png"
const imageAlt = hasImage ? section.images[0].post_title : "No Image"
const imageCredits = hasImage ? section.images[0].post_user_id.toString() : ""
const images = hasImage ?
section.images.map((image: any) => {
return {
url: getPostImage(image),
name: image.post_title,
credits: image.post_user_id.toString(),
description: image.post_title
}
})
: [{
url: "/media/images/default_noImage.png",
name: "No Image",
credits: "",
description: ""
}]

const imageDiv = (
<>
<div className={styles.imageContainer}>
<ModalImage url={imageURI} description={imageAlt}>
<CreditedImage url={imageURI} alt={imageAlt} credits={imageCredits}/>
</ModalImage>
<ImageArray images={images} enableModal/>
</div>
</>
)
Expand Down Expand Up @@ -238,16 +258,26 @@ export function CraftSection({section, isLeft} : CraftSectionProps){
const additional_infoRef = useRef<HTMLDivElement>(null)

const hasImage = section.images.length > 0
const imageURI = hasImage ? getPostImage(section.images[0]) : "/media/images/default_noImage.png"
const imageAlt = hasImage ? section.images[0].post_title : "No Image"
const imageCredits = hasImage ? section.images[0].post_user_id.toString() : ""
const images = hasImage ?
section.images.map((image: any) => {
return {
url: getPostImage(image),
name: image.post_title,
credits: image.post_user_id.toString(),
description: image.post_title
}
})
: [{
url: "/media/images/default_noImage.png",
name: "No Image",
credits: "",
description: ""
}]

const imageDiv = (
<>
<div className={styles.imageContainer}>
<ModalImage url={imageURI} description={imageAlt}>
<CreditedImage url={imageURI} alt={imageAlt} credits={imageCredits}/>
</ModalImage>
<ImageArray images={images} enableModal/>
</div>
</>
)
Expand Down
4 changes: 2 additions & 2 deletions website/src/lib/databse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class SQLDatabase {
long_description: string;
author: string;
last_modified: string;
display_image: string;
display_images: string;
plant_type: string;
published: string;

Expand Down Expand Up @@ -133,7 +133,7 @@ export class SQLDatabase {
this.long_description = "long_description";
this.author = "author";
this.last_modified = "last_modified";
this.display_image = "display_image";
this.display_images = "display_images";
this.plant_type = "plant_type";
this.published = "published"

Expand Down
16 changes: 8 additions & 8 deletions website/src/lib/plant_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface PlantData {
authorIDs: number[];
authors: UserDatabaseDetails[];
last_modified: string;
display_image: string;
display_images: PostData[];
plant_type: string;
attachments: AttachmentData[];
sections: any[];
Expand All @@ -40,7 +40,7 @@ export interface PlantDataApi {
long_description: string;
author: string;
last_modified: string;
display_image: string;
display_images: PostData[];
plant_type: string;
published: boolean;
months_ready_events: string[];
Expand Down Expand Up @@ -351,7 +351,7 @@ export function ValidPlantDataApi(apiData : PlantDataApi) : boolean {
|| apiData.long_description == null
|| apiData.author == null
|| apiData.last_modified == null
|| apiData.display_image == null
|| apiData.display_images == null
|| apiData.plant_type == null
|| apiData.months_ready_events == null
|| apiData.months_ready_start_months == null
Expand Down Expand Up @@ -403,7 +403,7 @@ export function ValidPlantData(plantData : PlantData) : boolean {
|| plantData.long_description == null
|| plantData.authorIDs == null
|| plantData.last_modified == null
|| plantData.display_image == null
|| plantData.display_images == null
|| plantData.plant_type == null
|| plantData.months_ready_for_use == null
|| plantData.use == null
Expand Down Expand Up @@ -439,7 +439,7 @@ export function ConvertApiIntoPlantData(apiData : PlantDataApi){
plantData.small_description = apiData.small_description;
plantData.long_description = apiData.long_description;
plantData.last_modified = apiData.last_modified;
plantData.display_image = apiData.display_image;
plantData.display_images = apiData.display_images;
plantData.plant_type = apiData.plant_type;
plantData.published = apiData.published;

Expand Down Expand Up @@ -598,7 +598,7 @@ export function ConvertPlantDataIntoApi(plantData : PlantData){
apiData.small_description = plantData.small_description;
apiData.long_description = plantData.long_description;
apiData.last_modified = plantData.last_modified;
apiData.display_image = plantData.display_image;
apiData.display_images = plantData.display_images.map((image: any) => image.id).join(",") as any;
apiData.plant_type = plantData.plant_type;
apiData.author = plantData.authorIDs.toString()
apiData.published = plantData.published;
Expand Down Expand Up @@ -686,7 +686,7 @@ export function emptyPlantData(){
authorIDs: [],
authors: [],
last_modified: "",
display_image: "",
display_images: [],
plant_type: "",
attachments: [],
sections: [],
Expand All @@ -711,7 +711,7 @@ export function emptyPlantApiData(){
long_description: "",
author: "",
last_modified: new Date().toISOString(),
display_image: "",
display_images: [],
plant_type: "",
months_ready_events: [],
months_ready_start_months: [],
Expand Down
10 changes: 5 additions & 5 deletions website/src/pages/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ function createBackUpQuery(json: any) {
let old = query

query = createUpSertQuery("plants",
[tables.id, tables.preferred_name, tables.english_name, tables.maori_name, tables.latin_name, tables.location_found, tables.small_description, tables.long_description, tables.author, tables.last_modified, tables.display_image, tables.plant_type, tables.published],
[plantTable[j].id, plantTable[j].preferred_name, plantTable[j].english_name, plantTable[j].maori_name, plantTable[j].latin_name, plantTable[j].location_found, plantTable[j].small_description, plantTable[j].long_description, plantTable[j].author, plantTable[j].last_modified, plantTable[j].display_image, plantTable[j].plant_type, plantTable[j].published]
[tables.id, tables.preferred_name, tables.english_name, tables.maori_name, tables.latin_name, tables.location_found, tables.small_description, tables.long_description, tables.author, tables.last_modified, tables.display_images, tables.plant_type, tables.published],
[plantTable[j].id, plantTable[j].preferred_name, plantTable[j].english_name, plantTable[j].maori_name, plantTable[j].latin_name, plantTable[j].location_found, plantTable[j].small_description, plantTable[j].long_description, plantTable[j].author, plantTable[j].last_modified, plantTable[j].display_images, plantTable[j].plant_type, plantTable[j].published]
)

// Add the old query
Expand All @@ -106,7 +106,7 @@ function createBackUpQuery(json: any) {
let craftTable = json.data[i];
for (let j = 0; j < craftTable.length; j++)
query += createUpSertQuery("craft",
[tables.id, tables.plant_id, tables.craft_part_of_plant, tables.craft_use_identifier, tables.craft_use, tables.craft_image, tables.craft_additional_info],
[tables.id, tables.plant_id, tables.craft_part_of_plant, tables.craft_use_identifier, tables.craft_use, tables.craft_images, tables.craft_additional_info],
[craftTable[j].id, craftTable[j].plant_id, craftTable[j].craft_part_of_plant, craftTable[j].craft_use_identifier, craftTable[j].craft_use, craftTable[j].craft_image, craftTable[j].craft_additional_info]
)

Expand All @@ -130,7 +130,7 @@ function createBackUpQuery(json: any) {
let edibleTable = json.data[i];
for (let j = 0; j < edibleTable.length; j++)
query += createUpSertQuery("edible",
[tables.id, tables.plant_id, tables.edible_part_of_plant, tables.edible_use_identifier, tables.edible_image_of_part, tables.edible_nutrition, tables.edible_preparation, tables.edible_preparation_type],
[tables.id, tables.plant_id, tables.edible_part_of_plant, tables.edible_use_identifier, tables.edible_images, tables.edible_nutrition, tables.edible_preparation, tables.edible_preparation_type],
[edibleTable[j].id, edibleTable[j].plant_id, edibleTable[j].edible_part_of_plant, edibleTable[j].edible_use_identifier, edibleTable[j].edible_image, edibleTable[j].edible_nutrition, edibleTable[j].edible_preparation, edibleTable[j].edible_preparation_type]
)
continue;
Expand All @@ -142,7 +142,7 @@ function createBackUpQuery(json: any) {
let medicalTable = json.data[i];
for (let j = 0; j < medicalTable.length; j++)
query += createUpSertQuery("medical",
[tables.id, tables.plant_id, tables.medical_type, tables.medical_use_identifier, tables.medical_use, tables.medical_image, tables.medical_preparation, tables.medical_restricted],
[tables.id, tables.plant_id, tables.medical_type, tables.medical_use_identifier, tables.medical_use, tables.medical_images, tables.medical_preparation, tables.medical_restricted],
[medicalTable[j].id, medicalTable[j].plant_id, medicalTable[j].medical_type, medicalTable[j].medical_use_identifier, medicalTable[j].medical_use, medicalTable[j].medical_image, medicalTable[j].medical_preparation, medicalTable[j].medical_restricted]
)
continue;
Expand Down
12 changes: 11 additions & 1 deletion website/src/pages/api/plants/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export async function downloadPlantData(table: any, id: any, client: any, restri
plants.${tables.long_description},
plants.${tables.author},
plants.${tables.last_modified},
plants.${tables.display_image},
plants.${tables.display_images},
plants.${tables.plant_type},
plants.${tables.published},`;
break;
Expand Down Expand Up @@ -272,12 +272,14 @@ export async function downloadPlantData(table: any, id: any, client: any, restri
data[0].medical_images = parseImageIds(data[0].medical_images);
data[0].edible_images = parseImageIds(data[0].edible_images);
data[0].craft_images = parseImageIds(data[0].craft_images);
data[0].display_images = parseImageId(data[0].display_images);

// Remove duplicates
let allPostIds: any = [];
for (let i = 0; i < data[0].medical_images.length; i++) { allPostIds = allPostIds.concat(data[0].medical_images[i]); }
for (let i = 0; i < data[0].edible_images.length; i++) { allPostIds = allPostIds.concat(data[0].edible_images[i]); }
for (let i = 0; i < data[0].craft_images.length; i++) { allPostIds = allPostIds.concat(data[0].craft_images[i]); }
allPostIds.concat(data[0].display_images);
allPostIds = allPostIds.filter((id: any, index: any) => allPostIds.indexOf(id) === index);

// Make sure that allPostIds is not empty
Expand All @@ -291,6 +293,7 @@ export async function downloadPlantData(table: any, id: any, client: any, restri
for (let i = 0; i < data[0].medical_images.length; i++) { data[0].medical_images[i] = data[0].medical_images[i].map((id: any) => postData.find((post: any) => post.id === id)); }
for (let i = 0; i < data[0].edible_images.length; i++) { data[0].edible_images[i] = data[0].edible_images[i].map((id: any) => postData.find((post: any) => post.id === id)); }
for (let i = 0; i < data[0].craft_images.length; i++) { data[0].craft_images[i] = data[0].craft_images[i].map((id: any) => postData.find((post: any) => post.id === id)); }
data[0].display_images = data[0].display_images.map((id: any) => postData.find((post: any) => post.id === id));

// If the data is not empty, return the data
return ["success", data];
Expand All @@ -311,4 +314,11 @@ const parseImageIds = (imageData: string) => {
})
}
)
}

const parseImageId = (imageData: string) => {
if(imageData === null || imageData == "") return [];
return imageData.split(",").map((id: string) => {
return parseInt(id.trim().split("\n")[0])
})
}
Loading

0 comments on commit 6ba066b

Please sign in to comment.