Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app finished #4

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions components/UI/Card.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.card {
background-color: white;
border-radius: 6px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}
19 changes: 19 additions & 0 deletions components/UI/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ReactNode } from "react";
import classes from "./Card.module.css";

type Props = {
children: ReactNode;
className: string;
};

const Card: React.FC<Props> = (props) => {
return (
<div
className={`${classes.card} ${props.className ? props.className : ""}`}
>
{props.children}
</div>
);
};

export default Card;
24 changes: 24 additions & 0 deletions components/UI/vikings/SingleViking.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
.card-margin {
margin: 1rem 1rem;
}

.card-margin ul {
margin: 1rem 0;
}

.name {
padding: 0 0.5rem;
font-size: 1rem;
text-align: center;
}

.name p {
font-weight: 700;
font-size: 1.3rem;
padding: 0.5rem 0;
margin: 0;
}

.border--small {
border-bottom: 1px solid black;
}
48 changes: 48 additions & 0 deletions components/UI/vikings/SingleViking.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useState, useEffect, Fragment } from "react";

import Card from "../Card";
import { viking } from "../../../types";
import styles from "./SingleViking.module.css";

const SingleViking: React.FC<{ viking: viking }> = (props) => {
const [gotAnyFightingSkills, setGotAnyFightingSkills] = useState(false);
const {
viking: {
fullName,
presenceOfChildren,
age,
hometown,
canFightWithSword,
canFightWithAxe,
canFightWithSpear,
},
} = props;

useEffect(() => {
if (canFightWithSword || canFightWithAxe || canFightWithSpear) {
setGotAnyFightingSkills(true);
}
}, [canFightWithSword, canFightWithAxe, canFightWithSpear]);

return (
<Card className={styles["card-margin"]}>
<div className={styles.name}>
<p className={styles["border--small"]}>{fullName}</p>
</div>
<ul>
<li>Children: {presenceOfChildren ? "Yes" : "No"}</li>
<li>Age: {age}</li>
<li>Hometown: {hometown} </li>
{gotAnyFightingSkills && (
<Fragment>
{canFightWithSword && <li>Can fight with sword. </li>}
{canFightWithAxe && <li>Can fight with axe. </li>}
{canFightWithSpear && <li>Can fight with spear. </li>}
</Fragment>
)}
</ul>
</Card>
);
};

export default SingleViking;
90 changes: 90 additions & 0 deletions lib/vikings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { viking, rawJsonViking, rawYamlViking } from "../types";

// Filters provided .json data from the same objects
const getUniqueJsonArray = (rawJson: rawJsonViking[]) => {
return rawJson.filter(
(v: any, i: number, a: any) =>
a.findIndex((v2: any) =>
["fullName", "age", "village", "valkyrie", "hasSon"].every(
(k: any) => v2[k] === v[k]
)
) === i
);
};

// Filters provided .yaml data from the same objects
const getUniqueYamlArray = (rawJson: rawYamlViking[]) => {
return rawJson.filter(
(v: any, i: number, a: any) =>
a.findIndex((v2: any) =>
[
"name",
"years_old",
"has_home_in",
"name_of_father",
"number_of_children",
"weapon",
].every((k: any) => v2[k] === v[k])
) === i
);
};

// Maps raw .json object to viking type
const mapJsonToViking = (rawJson: rawJsonViking[]) => {
const mappedVikings: viking[] = getUniqueJsonArray(rawJson)
.filter((viking: rawJsonViking) => viking.age >= 25 && viking.age <= 65)
.map((viking: rawJsonViking) => {
return {
fullName: viking.fullName,
presenceOfChildren: viking.hasSon,
age: viking.age,
hometown: viking.village,
canFightWithSword: false,
canFightWithAxe: false,
canFightWithSpear: false,
};
});
return mappedVikings;
};

// Maps raw .yaml object to viking type
const mapYamlToViking = (rawYaml: rawYamlViking[]) => {
const mappedVikings: viking[] = getUniqueYamlArray(rawYaml)
.filter(
(viking: rawYamlViking) =>
viking.years_old >= 25 && viking.years_old <= 65
)
.map((viking: rawYamlViking) => {
return {
fullName: `${viking.name} ${viking.name_of_father}sson`,
presenceOfChildren: viking.number_of_children === 0 ? false : true,
age: viking.years_old,
hometown: viking.has_home_in,
canFightWithSword: viking.weapon === "sword" ? true : false,
canFightWithAxe: viking.weapon === "axe" ? true : false,
canFightWithSpear: viking.weapon === "spear" ? true : false,
};
});

return mappedVikings;
};

// Filters out the same objects from the given arrays of viking type
export const validateAndMergeArrays = (
rawJson: rawJsonViking[],
rawYaml: any
) => {
const mappedJsonVikings = mapJsonToViking(rawJson);
const mappedYamlVikings = mapYamlToViking(rawYaml);

const names = new Set(mappedJsonVikings.map((viking) => viking.fullName));
const villages = new Set(mappedJsonVikings.map((viking) => viking.hometown));
const mergedVikings = [
...mappedJsonVikings,
...mappedYamlVikings.filter((viking) => {
return !names.has(viking.fullName) && !villages.has(viking.hometown);
}),
];

return mergedVikings;
};
4 changes: 4 additions & 0 deletions module.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "*.yaml" {
const data: { [key: string]: any };
export default data;
}
7 changes: 5 additions & 2 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
}
};

module.exports = nextConfig
const withYaml = require("next-yaml");

module.exports = nextConfig;
module.exports = withYaml();
Loading