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

Feature/launches filters on home page #20

Merged
merged 5 commits into from
May 17, 2022
Merged
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
7 changes: 7 additions & 0 deletions public/CREWED.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions public/PREVIOUS.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,441 changes: 1,441 additions & 0 deletions public/california.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions public/crewed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions public/florida.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions public/previous.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/components/Home/Home.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.home-container {
margin: 0 auto;
padding-top: 6rem;
}

.home-inner-container {
margin: 0 auto;
max-width: 96rem;
}
13 changes: 12 additions & 1 deletion src/components/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import React from 'react';
import { Outlet } from 'react-router-dom';
import { LaunchFilter } from '../LaunchFilter/LaunchFilter';
import { Slider } from '../Slider';
import './Home.scss';

export const Home = () => <Slider />;
export const Home = () => (
<div className="home-container">
<LaunchFilter />
<div className="home-inner-container">
<Slider />
<Outlet />
</div>
</div>
);
8 changes: 8 additions & 0 deletions src/components/LaunchFilter/LaunchFilter.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

.launch-filter-list {
position: fixed;
z-index: 2;
top: 6rem;
margin: 0;
list-style: none;
}
12 changes: 12 additions & 0 deletions src/components/LaunchFilter/LaunchFilter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import React from 'react';
import { FilterLink } from './components/FilterLink/FilterLink';
import { LaunchFilters } from './constants/filerConstants';
import './LaunchFilter.scss';

export const LaunchFilter = () => (
<ul className="launch-filter-list">
{Object.values(LaunchFilters).map((filterValue, index) => (
<FilterLink key={index} filterValue={filterValue} />
))}
</ul>
);
31 changes: 31 additions & 0 deletions src/components/LaunchFilter/actions/actionCreators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { LaunchActions } from '../types/launchActionTypes';
import { LaunchFilters } from '../constants/filerConstants';
import type {
FilterActionError,
FilterActionRequest,
FilterActionSuccess,
} from '../types/LaunchActions';
import type { Launch } from '../../../shared/api/types/Launch';

export const filterRequestAction = (
filterValue: LaunchFilters,
): FilterActionRequest => ({
type: `${filterValue}${LaunchActions.LAUNCH_REQUEST}`,
payload: { filterValue },
});

export const filterSuccessAction = (
filterValue: LaunchFilters,
launches: Launch[],
): FilterActionSuccess => ({
type: `${filterValue}${LaunchActions.LAUNCH_SUCCESS}`,
payload: { filterValue, [filterValue]: launches },
});

export const filterErrorAction = (
filterValue: LaunchFilters,
error: string,
): FilterActionError => ({
type: `${filterValue}${LaunchActions.LAUNCH_ERROR}`,
payload: { filterValue, error },
});
22 changes: 22 additions & 0 deletions src/components/LaunchFilter/actions/getFilteredLaunches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Dispatch } from '@reduxjs/toolkit';
import {
filterErrorAction,
filterRequestAction,
filterSuccessAction,
} from './actionCreators';
import { LaunchFilters } from '../constants/filerConstants';
import { fetchLaunches } from '../services/fetchLaunches';
import type { FilterAction } from '../types/LaunchActions';
import type { LaunchResponse } from '../types/LaunchResponse';

export const getFilteredLaunches =
(filterValue: LaunchFilters) =>
async (dispatch: Dispatch<FilterAction>): Promise<void> => {
dispatch(filterRequestAction(filterValue));
try {
const response: LaunchResponse = await fetchLaunches(filterValue);
dispatch(filterSuccessAction(filterValue, response.data.results));
} catch (error) {
dispatch(filterErrorAction(filterValue, (error as Error).message));
}
};
74 changes: 74 additions & 0 deletions src/components/LaunchFilter/components/FilterLink/FilterLink.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
@import '../../../../shared/styles/variables';

.launches-filter-item {
display: flex;
width: 5rem;
height: 5rem;

background-color: $primary-main;
border-radius: 50%;

&:not(:last-child) {
margin-bottom: 1rem;
}

&:hover,
&:focus {
width: 16rem;
border-radius: 50% / 7.5rem;
background-color: $secondary-main;
box-shadow: 0px 3px 3px -2px rgba(0, 0, 0, 0.2),
0px 8px 10px 1px rgba(0, 0, 0, 0.14), 0px 3px 14px 2px rgba(0, 0, 0, 0.12);

transition-property: background-color, background-position, width;
transition-duration: $default-transition-delay;
transition-timing-function: ease;
}
}

.launches-filter-link {
display: flex;
justify-content: flex-start;
align-items: center;
width: 100%;

background-size: 3rem;
background-repeat: no-repeat;
background-position: center;

font-family: $default-font;
text-decoration: none;
font-size: 0.75rem;
color: $contrast-text-color;

& .launches-filter-caption {
visibility: hidden;
}

&:hover {
background-position-x: 1rem;

& .launches-filter-caption {
visibility: inherit;
margin-left: 5rem;
text-transform: uppercase;
transition: visibility 0ms ease $default-transition-delay;
}
}

&.previous {
background-image: url('../../../../../public/previous.svg');
}

&.crewed {
background-image: url('../../../../../public/crewed.svg');
}

&.california {
background-image: url('../../../../../public/california.svg');
}

&.florida {
background-image: url('../../../../../public/florida.svg');
}
}
32 changes: 32 additions & 0 deletions src/components/LaunchFilter/components/FilterLink/FilterLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { Link } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { Dispatch } from '@reduxjs/toolkit';
import { RoutesPath } from '../../../Router/routesPath';
import { getFilteredLaunches } from '../../actions/getFilteredLaunches';
import { FilterCaptions, LaunchFilters } from '../../constants/filerConstants';
import type { FilterAction } from '../../types/LaunchActions';
import './FilterLink.scss';

interface FilterLinkProps {
filterValue: LaunchFilters;
}

export const FilterLink = ({ filterValue }: FilterLinkProps) => {
const dispatch: Dispatch<FilterAction> = useDispatch();
const handleClick = () => getFilteredLaunches(filterValue)(dispatch);

return (
<li className="launches-filter-item">
<Link
className={`launches-filter-link ${filterValue.toLowerCase()}`}
to={RoutesPath[filterValue]}
onClick={handleClick}
>
<span className="launches-filter-caption">
{FilterCaptions[filterValue]}
</span>
</Link>
</li>
);
};
1 change: 1 addition & 0 deletions src/components/LaunchFilter/components/FilterLink/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { FilterLink } from './FilterLink';
18 changes: 18 additions & 0 deletions src/components/LaunchFilter/constants/filerConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export enum LaunchFilters {
PREVIOUS = 'PREVIOUS',
CREWED = 'CREWED',
CALIFORNIA = 'CALIFORNIA',
FLORIDA = 'FLORIDA',
}

export enum FilterCaptions {
PREVIOUS = 'Previous launches',
CREWED = 'Crewed launches',
CALIFORNIA = 'Location: California',
FLORIDA = 'Location: Florida',
}

export enum LocationIds {
CALIFORNIA = '11',
FLORIDA = '12,27',
}
1 change: 1 addition & 0 deletions src/components/LaunchFilter/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { LaunchFilter } from './LaunchFilter';
Loading