-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP][FEATURE] Collection and Cards Form (#140)
* feat(CollectionForm): create basic collection form template * feat(CollectionForm): modularize collection form components * feat(CollectionForm): refactor Select TextField Types and Cards model * feat(CollectionForm): create card form * feat(CollectionForm): rename collectionFormView, refactor CollectionForm and create.tsx * feat(CollectionForm): add categories array example * feat(CollectionForm): add delete button with styles * feat(CollectionForm): add fetch onSubmit * fix: restore tags to avoid conflicts * fix> add validation to collections without tags * fix prettier Co-authored-by: Agustin Vazquez <[email protected]>
- Loading branch information
Showing
10 changed files
with
283 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Button, Container, Divider, Stack, Text } from "@chakra-ui/react"; | ||
import React from "react"; | ||
import { CardData } from "../models"; | ||
import { useForm, Controller } from "react-hook-form"; | ||
import TextField from "@/modules/shared/components/TextField"; | ||
import Trash from "@/modules/shared/components/Icons/Trash"; | ||
|
||
const CardForm = ({ update, index, value, remove, control }: any) => { | ||
const { handleSubmit } = useForm<CardData>({ | ||
defaultValues: value, | ||
}); | ||
// const { handleSubmit } = useFormWithYup<CardData>(cardDataSchema); | ||
|
||
const onSubmit = async (data: any) => { | ||
console.log(data); | ||
}; | ||
|
||
console.log(value); | ||
return ( | ||
<Container maxW="container.xl"> | ||
<Divider /> | ||
|
||
<Stack py={8} spacing={12} onSubmit={handleSubmit(onSubmit)}> | ||
<Stack spacing={6}> | ||
<Text variant="label" as="label"> | ||
Tarjeta número {index + 1} | ||
</Text> | ||
|
||
<Controller | ||
control={control} | ||
name="sideA" | ||
render={({ field: { onChange, value, ref } }) => ( | ||
<TextField | ||
label="Lado A" | ||
placeholder="Ingrese una pregunta" | ||
onChange={() => console.log(value)} | ||
value={value} | ||
/> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="sideB" | ||
render={({ field: { onChange, value, ref } }) => ( | ||
<TextField | ||
label="Lado B" | ||
placeholder="Ingrese una pregunta" | ||
onChange={() => console.log(value)} | ||
value={value} | ||
/> | ||
)} | ||
/> | ||
|
||
<button | ||
type="button" | ||
onClick={handleSubmit((data) => { | ||
update(index, data); | ||
})} | ||
> | ||
guardar tarjeta | ||
</button> | ||
<Button | ||
variant="outline" | ||
colorScheme="red" | ||
leftIcon={<Trash color="red" height={20} width={20} />} | ||
size="sm" | ||
type="button" | ||
onClick={() => remove(index)} | ||
> | ||
Borrar | ||
</Button> | ||
</Stack> | ||
</Stack> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default CardForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
import CardForm from "@/modules/Cards/components/CardForm"; | ||
import TextField from "@/modules/shared/components/TextField"; | ||
import Select from "@/modules/shared/components/Select"; | ||
import { Button, Center, Container, Stack, Text } from "@chakra-ui/react"; | ||
import React from "react"; | ||
import { useForm, useFieldArray, Controller } from "react-hook-form"; | ||
import { CollectionFirebaseData } from "../models"; | ||
import { createCollectionSchema } from "../schema"; | ||
import { categories } from "../utils/categories"; | ||
import { CardData } from "@/modules/Cards/models"; | ||
|
||
interface CollectionForm { | ||
title: string; | ||
description: string; | ||
category: string; | ||
cards: CardData[]; | ||
} | ||
|
||
const fieldArrayName = "cards"; | ||
|
||
//{ sideA: { type: "text", value: "" }, sideB: { type: "text", value: "" } } | ||
const CollectionForm = () => { | ||
const { control, handleSubmit } = useForm<CollectionForm>({ | ||
defaultValues: { | ||
title: "", | ||
description: "", | ||
cards: [{ sideA: { type: "text", value: "" }, sideB: { type: "text", value: "" } }], | ||
category: "", | ||
}, | ||
}); | ||
|
||
const { fields, append, update, remove } = useFieldArray({ | ||
control, | ||
name: fieldArrayName, | ||
}); | ||
|
||
const onSubmit = async (data: CollectionForm) => { | ||
try { | ||
let result = await fetch("/api/collections", { | ||
method: "POST", | ||
headers: { "Content-Type": "application/json" }, | ||
body: JSON.stringify(data), | ||
}); | ||
result = await result.json(); | ||
window.alert("Collection created"); | ||
return result; | ||
} catch (error) { | ||
window.alert("ERROR :("); | ||
return null; | ||
} | ||
}; | ||
|
||
// const watchFieldArray = watch(fieldArrayName); | ||
// const controlledFields = fields.map((field, index) => { | ||
// return { | ||
// ...field, | ||
// ...watchFieldArray[index], | ||
// }; | ||
// }); | ||
|
||
return ( | ||
<Container maxW="container.xl"> | ||
<Center> | ||
<Stack as="form" py={8} spacing={12} onSubmit={handleSubmit(onSubmit)}> | ||
<Stack spacing={6}> | ||
<Controller | ||
control={control} | ||
name="title" | ||
render={({ field: { onChange, value, ref } }) => ( | ||
<TextField | ||
label="Título" | ||
placeholder="Ingresa un título" | ||
onChange={onChange} | ||
value={value} | ||
/> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="category" | ||
render={({ field: { onChange, value, ref } }) => ( | ||
<Select label="Categoria" option={categories} onChange={onChange} value={value} /> | ||
)} | ||
/> | ||
|
||
<Controller | ||
control={control} | ||
name="description" | ||
render={({ field: { onChange, value, ref } }) => ( | ||
<TextField | ||
label="Descripción" | ||
placeholder="Ingrese una descripción" | ||
onChange={onChange} | ||
value={value} | ||
/> | ||
)} | ||
/> | ||
|
||
{fields.length ? <Text variant="label">Crear Tarjetas</Text> : null} | ||
|
||
{fields.map((field, index) => ( | ||
<fieldset key={field.id}> | ||
<CardForm | ||
control={control} | ||
update={update} | ||
index={index} | ||
value={field} | ||
remove={remove} | ||
/> | ||
</fieldset> | ||
))} | ||
|
||
<Button | ||
type="button" | ||
variant="outline" | ||
onClick={() => { | ||
append({ | ||
sideA: { value: "", type: "text" }, | ||
sideB: { value: "", type: "text" }, | ||
}); | ||
}} | ||
> | ||
Añadir tarjeta | ||
</Button> | ||
</Stack> | ||
|
||
<Button type="submit">Crear coleccion</Button> | ||
</Stack> | ||
</Center> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default CollectionForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React, { useState } from "react"; | ||
import CollectionForm from "./CollectionForm"; | ||
import CardForm from "@/modules/Cards/components/CardForm"; | ||
|
||
const CollectionFormsView = () => { | ||
// si no tiene estado crea una nueva coleccion | ||
// si recibe estado deberia actualizar una coleccion | ||
|
||
return ( | ||
<div> | ||
<CollectionForm /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CollectionFormsView; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const categories = ["Medicina", "Biologia", "Matematicas", "Fisica", "Computacion"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import React from "react"; | ||
|
||
import CollectionFormsView from "@/modules/Collections/components/CollectionFormsView"; | ||
|
||
const CreateCollection: React.FC = () => { | ||
return <CollectionFormsView />; | ||
}; | ||
|
||
export default CreateCollection; |