Skip to content

Instantly share code, notes, and snippets.

View AlejandroYanes's full-sized avatar

Alejandro Yanes AlejandroYanes

View GitHub Profile
@AlejandroYanes
AlejandroYanes / app-folder-tree.txt
Created October 10, 2022 17:30
App folder tree
src/
├── api/
│ ├── productsApi.ts
│ └── cartApi.ts
├── components/
│ ├── base/
│ │ ├── Button/
│ │ ├── Input/
│ │ ├── Modal/
│ │ ├── Text/
@AlejandroYanes
AlejandroYanes / using-use-reducer-hook.ts
Created October 10, 2022 17:28
Using userReducer hook
const [state, dispatch] = useReducer(productDetailsReducer, {
showModal: false,
quantity: 1,
});
@AlejandroYanes
AlejandroYanes / product-details-tree.txt
Created October 10, 2022 17:27
ProductDetails file tree
ProductDetails/
├── components/
│ └── ...
├── state/
│ ├── actions/
│ │ ├── add-to-cart.ts
│ │ ├── get-current-cart.ts
│ │ └── resolve-error-message.ts
│ ├── index.ts
│ ├── reducer.ts
@AlejandroYanes
AlejandroYanes / index.tsx
Created October 10, 2022 10:46
This would be how to use the context provider
<ProductDetailsProvider
product={product}
quantity={quantity}
increaseQuantity={increaseQuantity}
decreaseQuantity={decreaseQuantity}
>
{/* ...your components */}
</ProductDetailsProvider>
@AlejandroYanes
AlejandroYanes / contex.tsx
Created October 10, 2022 10:45
This is the context implementation
import { createContext, PropsWithChildren, useContext } from 'react';
import { ProductDetailContext } from './types';
const context = createContext<ProductDetailContext>(undefined);
const { Provider } = context;
const ProductDetailsProvider = (props: PropsWithChildren<ProductDetailContext>) => {
const { children, ...rest } = props;
return (
@AlejandroYanes
AlejandroYanes / context-type.ts
Last active October 10, 2022 10:44
This is the type used for the context
// this should go on the types.ts file discussed previously
export interface ProductDetailContext {
product: ProductModel;
quantity: number;
increaseQuantity: () => void;
decreaseQuantity: () => void;
}
@AlejandroYanes
AlejandroYanes / action-generator.ts
Created October 10, 2022 10:40
This is the patter form the action generators
export default function actionGenerator(
dispatch: CustomDispatch,
<...extraParamsNeeded>,
) {
// the actual action to be called by the UI
return (<...paramsToReceivedFromUI>) => {}
}
@AlejandroYanes
AlejandroYanes / handle-form-change.ts
Created October 10, 2022 10:32
This is a simpler action that takes some inputs from the UI
export default handleFormChange(dispatch: CustomDispatch) {
return (newValues: FormState) => {
dispatch({ type: 'update_form_state', values: newValues });
};
}
@AlejandroYanes
AlejandroYanes / index.tsx
Created October 10, 2022 10:31
This is how to use the state hook
const ProductDetailsPage = () => {
const {
isLoading,
product,
error,
quantity,
showModal,
goBack,
openModal,
closeModal,
@AlejandroYanes
AlejandroYanes / index.ts
Created October 10, 2022 10:29
the connection point where all comes together
import { useReducer } from 'react';
import { useHistory } from 'react-router-dom';
import { useParams } from 'react-router';
import { useQuery } from 'react-query';
import { AppwriteException } from 'appwrite';
import productsApi from 'api/products';
import { QueryKey } from 'components/providers/Query';
import productDetailsReducer from './reducer';
import addToCart from './actions/add-to-cart';