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

Fabi0o solution #3

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}
26 changes: 26 additions & 0 deletions components/VikingCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Viking } from "../types/viking";
import styles from "../styles/VikingCard.module.css";
const VikingCard = ({ viking }: { viking: Viking }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lack of whitespace in all places. The code is hard to read as there is too few "enters" used. It applies to all your code, not only this file :)

return (
<div className={styles.card}>
<div className={styles.cardFullName}>{viking.fullName}</div>
<div className={styles.cardAge}>Age: {viking.age}</div>
<div className={styles.cardHomeTown}>Town: {viking.hometown}</div>
<div className={styles.cardChildren}>
{viking.presenceOfChildren ? "Has children" : "Childless"}
</div>
<div>
Fight with
{viking.canFightWithSword

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I am not a huge fan of chaining ternary operators that way.

? " Sword"
: viking.canFightWithAxe
? " Axe"
: viking.canFightWithSpear
? " Spear"
: " Hammer"}
</div>
</div>
);
};

export default VikingCard;
51 changes: 51 additions & 0 deletions lib/vikings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Viking } from "../types/viking";
type vikingDataJSON = {
fullName: string;
village: string;
age: number;
valkyrie: string;
hasSon: boolean;
};
type vikingDataYaml = {
name: string;
weapon: "sword" | "spear" | "axe" | "hammer";
name_of_father: string;
has_home_in: string;
years_old: number;
number_of_children: number;
};

export const handleVikingList = (
vikingsJSON: Array<vikingDataJSON>,
vikingsYaml: Array<vikingDataYaml>
) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could write return type here (: Viking[]) and then you could remove all those Array from inside the function and it would work. You don't have to use type assertion everywhere for every variable, Typescript is clever enough to infer that :)

const jsonVikingList: Array<Viking> = vikingsJSON.map((vikingJSON) => {
const fullViking = {
fullName: vikingJSON.fullName,
presenceOfChildren: vikingJSON.hasSon ? true : false,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to return true or false from ternary operator, as the condition is already a boolean.

age: vikingJSON.age,
hometown: vikingJSON.village,
canFightWithSword: vikingsJSON.indexOf(vikingJSON) <= 2 ? true : false,
canFightWithAxe: vikingsJSON.indexOf(vikingJSON) < 6 ? true : false,
canFightWithSpear: vikingsJSON.indexOf(vikingJSON) >= 6 ? true : false,
};
return fullViking;
});
const yamlVikingList: Array<Viking> = vikingsYaml.map((vikingYaml) => {
const fullViking = {
fullName: `${vikingYaml.name} ${vikingYaml.name_of_father}sson`,
presenceOfChildren: vikingYaml.number_of_children > 0 ? true : false,
age: vikingYaml.years_old,
hometown: vikingYaml.has_home_in,
canFightWithSword: vikingYaml.weapon == "sword" ? true : false,
canFightWithAxe: vikingYaml.weapon == "axe" ? true : false,
canFightWithSpear: vikingYaml.weapon == "spear" ? true : false,
};
return fullViking;
});
const fullVikingList: Array<Viking> = [...jsonVikingList, ...yamlVikingList];
const vikingList: Array<Viking> = fullVikingList.filter(
(viking) => viking.age > 25 && viking.age < 65
);
return vikingList;
};
7 changes: 4 additions & 3 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/** @type {import('next').NextConfig} */
const withYAML = require("next-yaml");
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
}

module.exports = nextConfig
};
module.exports = nextConfig;
module.exports = withYAML();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You replaced our custom next config with that function, so we lost it.

Loading