Skip to content

Commit

Permalink
sale feature almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnauBlanch committed Jul 23, 2020
1 parent efcbb21 commit a92f29f
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 11 deletions.
28 changes: 21 additions & 7 deletions dragon-frontend/src/pages/BookScanned/BookData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ interface OwnProps {
}

const mapStateToProps = (state: RootState, props: OwnProps) =>
({ book: state.books[props.isbn]?.data, sales: state.sales[props.isbn] } || {});
({
book: state.books[props.isbn]?.data,
bookSold: state.books[props.isbn]?.sold,
sales: state.sales[props.isbn],
} || {});
const dispatchProps = { sellBook: sellBookAction.request, scanAgain: () => push('/scan') };

type Props = OwnProps & WithTranslation & ReturnType<typeof mapStateToProps> & typeof dispatchProps;

const BookData: React.FC<Props> = ({ isbn, book, sales, scanAgain, sellBook, t }: Props) => {
const BookData: React.FC<Props> = ({
isbn,
book,
bookSold,
sales,
scanAgain,
sellBook,
t,
}: Props) => {
if (book === undefined) return <Redirect to="/scan" />;

const enableSale = book?.availableCopies > 0 && !sales?.isSelling && !bookSold;
return (
<>
<div className="w-full sm:w-1/3">
Expand Down Expand Up @@ -58,13 +71,14 @@ const BookData: React.FC<Props> = ({ isbn, book, sales, scanAgain, sellBook, t }
</button>
<button
type="button"
disabled={book?.availableCopies === 0 || sales?.isSelling}
disabled={!enableSale}
onClick={() => sellBook({ isbn })}
className="bg-red-500 hover:bg-red-400 focus:outline-none focus:shadow-outline focus:border-red-500 text-white font-semibold p-2 rounded-lg align-middle inline-flex items-center justify-center"
className="bg-red-500 hover:bg-red-400 disabled:bg-red-500 focus:outline-none focus:shadow-outline focus:border-red-500 text-white font-semibold p-2 rounded-lg align-middle inline-flex items-center justify-center"
>
{book?.availableCopies === 0 && t('scan.sold-out')}
{sales?.isSelling && <Spinner className="w-3" />}
{!(book?.availableCopies === 0 || sales?.isSelling) && t('scan.record-sale')}
{!bookSold && book?.availableCopies === 0 && t('scan.sold-out')}
{sales?.isSelling && <Spinner className="w-10" />}
{enableSale && t('scan.record-sale')}
{bookSold && t('scan.sell-done')}
</button>
</div>
</div>
Expand Down
3 changes: 2 additions & 1 deletion dragon-frontend/src/pages/BookScanned/SalesHistory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,14 @@ class SalesHistory extends React.Component<Props> {
<div className="flex flex-wrap">
{(isFetching || error !== undefined || (data !== undefined && data.length === 0)) && (
<div className="flex items-center justify-center h-24 w-full font-normal text-gray-600 text-sm">
{isFetching && data === undefined && <Spinner className="w-12 text-red-300" />}
{isFetching && <Spinner className="w-12 text-red-300" />}
{data && data.length === 0 && t('scan.no-sales')}
{error !== undefined && t('scan.there-was-a-problem')}
</div>
)}

{!error &&
!isFetching &&
data?.map((x) => (
<div
key={x.date.toISOString()}
Expand Down
1 change: 0 additions & 1 deletion dragon-frontend/src/pages/ScannerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class ScannerPage extends React.Component<Props, State> {

this.onCodeScanned = this.onCodeScanned.bind(this);
this.scanAgain = this.scanAgain.bind(this);
console.log('CONSTRUCTOR');
}

onCodeScanned(isbn: number) {
Expand Down
35 changes: 33 additions & 2 deletions dragon-frontend/src/reducers/books.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { createReducer } from 'typesafe-actions';
import { ErrorType } from '../models/enums';
import { BookActions, getBook, clearBookError } from '../actions/books';
import { Book } from '../models/book';
import { SalesActions, sellBook, unsellBook } from '../actions/sales';

export type BooksState = {
[isbn: number]: {
isFetching: boolean;
data?: Book;
sold?: boolean;
error?: ErrorType;
};
};

const initialState: BooksState = {};

const reducer = createReducer<BooksState, BookActions>(initialState)
const reducer = createReducer<BooksState, BookActions | SalesActions>(initialState)
.handleAction(getBook.request, (state, action) => ({
...state,
[action.payload.isbn]: { isFetching: true, data: undefined, error: undefined },
[action.payload.isbn]: {
...state[action.payload.isbn],
isFetching: true,
error: undefined,
sold: undefined,
},
}))
.handleAction(getBook.success, (state, action) => ({
...state,
Expand All @@ -29,6 +37,29 @@ const reducer = createReducer<BooksState, BookActions>(initialState)
.handleAction(clearBookError, (state, action) => {
if (state[action.payload.isbn]) return state;
return { ...state, [action.payload.isbn]: { ...state[action.payload.isbn], error: undefined } };
})
.handleAction(sellBook.success, (state, action) => {
if (!state[action.payload.isbn] || !state[action.payload.isbn].data) return state;
const { availableCopies } = state[action.payload.isbn].data!;
return {
...state,
[action.payload.isbn]: {
...state[action.payload.isbn],
data: { ...state[action.payload.isbn].data!, availableCopies: availableCopies - 1 },
sold: true,
},
};
})
.handleAction(unsellBook.success, (state, action) => {
if (!state[action.payload.isbn] || !state[action.payload.isbn].data) return state;
const { availableCopies } = state[action.payload.isbn].data!;
return {
...state,
[action.payload.isbn]: {
...state[action.payload.isbn],
data: { ...state[action.payload.isbn].data!, availableCopies: availableCopies + 1 },
},
};
});

export default reducer;
1 change: 1 addition & 0 deletions dragon-frontend/src/reducers/sales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type SalesState = {
data?: Sale[];
error?: ErrorType;
isSelling?: boolean;
sold?: boolean;
sellError?: ErrorType;
};
};
Expand Down
1 change: 1 addition & 0 deletions dragon-frontend/src/translations/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ export default {
'scan.no-sales': "Encara no s'ha venut cap exemplar d'aquest llibre",
'scan.undo-sale': 'Desfés',
'scan.sold-out': 'Esgotat',
'scan.sell-done': 'Venut!',
},
};
1 change: 1 addition & 0 deletions dragon-frontend/src/translations/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,6 @@ export default {
'scan.no-sales': 'No copies of this book have been sold yet',
'scan.undo-sale': 'Undo',
'scan.sold-out': 'Sold out',
'scan.sell-done': 'Sold!',
},
};

0 comments on commit a92f29f

Please sign in to comment.