Skip to content

Commit

Permalink
Send file hook
Browse files Browse the repository at this point in the history
  • Loading branch information
bduhbya committed Aug 12, 2024
1 parent d5bcbc0 commit 8a2a793
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
31 changes: 20 additions & 11 deletions src/app/components/CharacterDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from "react";
import React, { useState } from "react";
// import strings from "@/strings";

export interface CharacterDisplayProps {
Expand All @@ -8,18 +8,27 @@ export interface CharacterDisplayProps {
export const CharacterDisplay: React.FC<CharacterDisplayProps> = ({
sourceFile,
}) => {
const [characterData, setCharacterData] = useState<String | null>(null);

if (sourceFile !== null) {
const reader = new FileReader();
reader.onload = () => {
try {
const jsonData = JSON.parse(reader.result as string);

setCharacterData(jsonData);
} catch (error) {
console.error("Error parsing JSON file:", error);
}
};
reader.readAsText(sourceFile);
}

return (
<div className="flex flex-col items-center justify-center">
{sourceFile === null && <p className="text-black dark:text-white">No file selected</p>}
{sourceFile !== null && (
<>
<img
src={URL.createObjectURL(sourceFile)}
alt={"Character Data"}
className="w-64 h-64 rounded-full"
/>
<p className="text-black">{sourceFile.name}</p>
</>
{characterData === null && <p className="text-black dark:text-white">No file selected</p>}
{characterData !== null && (
<div>{JSON.stringify(characterData)}</div>
)}
</div>
);
Expand Down
11 changes: 8 additions & 3 deletions src/app/components/CombatTracker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { BasicDialog, DialogData, DialogType } from "./BasicDialog";

export const activeCharacterTestId = "active-combat-character-row-";

const CombatTracker: React.FC = () => {
export interface CombatTrackerProps {
SetCharacterFile: (file: File) => void;
}

const CombatTracker: React.FC<CombatTrackerProps> = ({SetCharacterFile}) => {
const DIRECTION_UP = "up";
const DIRECTION_DOWN = "down";
type Direction = typeof DIRECTION_UP | typeof DIRECTION_DOWN;
Expand Down Expand Up @@ -81,7 +85,8 @@ const CombatTracker: React.FC = () => {
setPendingCharacter(null);
};

const handleCharacterClick = (character: Character) => {
const handleCharacterClick = (character: Character, SetCharacterFile: (file: File) => void) => {
SetCharacterFile(character.fileReference);
// TODO: activate a callback to set the character display in parent
const dynamicDataKeys = Object.keys(character.dynamicData);

Expand Down Expand Up @@ -182,7 +187,7 @@ const CombatTracker: React.FC = () => {
{combatCharacters.map((character, index) => (
<tr
key={index}
onClick={() => handleCharacterClick(character)}
onClick={() => handleCharacterClick(character, SetCharacterFile)}
className={character.active ? "bg-gray-200" : ""}
data-testid={
character.active ? `${activeCharacterTestId}${index}` : ""
Expand Down
2 changes: 1 addition & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function Home() {
</div> */}

<div className="mai flex justify-start items-center gap-x-4">
<CombatTracker />
<CombatTracker SetCharacterFile={setCharacterFile}/>
<div className="flex-grow">
<CharacterDisplay sourceFile={characterFile} />
</div>
Expand Down

0 comments on commit 8a2a793

Please sign in to comment.