Skip to content

Commit

Permalink
Get rid of component hell for dream data context
Browse files Browse the repository at this point in the history
  • Loading branch information
bombies committed Nov 4, 2023
1 parent 609a142 commit cb01424
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ interface DreamsContextProps extends DataContextProps {
}

const [DreamsContext, useHook] = createDataContext<DreamsContextProps>("useDreamsData must be used in a DreamsProvider!")
const [UserDreamsContext, useUserDreamsHook] = createDataContext<DreamsState>("useUserDreams must be used in a UserDreamsProvider!")
const [UserDreamTagsContext, useUserDreamTagsHook] = createDataContext<DreamTagsState>("useUserDreamTags must be used in a UserDreamTagsProvider!")
const [UserDreamCharactersContext, useUserDreamCharactersHook] = createDataContext<DreamCharactersState>("useUserDreamCharacters must be used in a UserDreamCharactersProvider!")

const DreamsProvider: FC<PropsWithChildren> = ({children}) => {
const dreams = useDreams()
Expand All @@ -30,19 +27,10 @@ const DreamsProvider: FC<PropsWithChildren> = ({children}) => {

return (
<DreamsContext.Provider value={{dreams, characters, tags}}>
<UserDreamsContext.Provider value={dreams}>
<UserDreamTagsContext.Provider value={tags}>
<UserDreamCharactersContext.Provider value={characters}>
{children}
</UserDreamCharactersContext.Provider>
</UserDreamTagsContext.Provider>
</UserDreamsContext.Provider>
{children}
</DreamsContext.Provider>
)
}

export default DreamsProvider
export const useDreamsData = useHook
export const useUserDreams = useUserDreamsHook
export const useUserDreamTags = useUserDreamTagsHook
export const useUserDreamCharacters = useUserDreamCharactersHook
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import {Button} from "@nextui-org/button";
import {EditIcon} from "@nextui-org/shared-icons";
import toast from "react-hot-toast";
import EditableTextArea from "@/app/(site)/components/inputs/editable/EditableTextArea";
import DreamEditableChip from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/card/view/DreamEditableChip";
import DreamEditableChip
from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/card/view/DreamEditableChip";
import {DropdownItem, Spacer} from "@nextui-org/react";
import DreamEditableAddButton
from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/card/view/DreamEditableAddButton";
import {useDreamsData} from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/DreamsProvider";
import {
useDreamsData, useUserDreamCharacters,
useUserDreamTags
} from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/DreamsProvider";
import {FetchFullDream, UpdateDream} from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/hooks/dream-api-utils";
FetchFullDream,
UpdateDream
} from "@/app/(site)/(internal)/dashboard/(your-dreams)/components/dreams/hooks/dream-api-utils";

type Props = {
dream: Dream,
fetchDream?: boolean,
}

const DreamView: FC<Props> = ({dream, fetchDream}) => {
const {data: allTags} = useUserDreamTags()
const {data: allCharacters} = useUserDreamCharacters()
const {tags: {data: allTags}, characters: {data: allCharacters}} = useDreamsData()
const {data: fullDream, error: fullDreamError, mutate: mutateFullDream} = FetchFullDream(dream, fetchDream ?? true)
const {trigger: updateDream} = UpdateDream(dream.id)
const {dreams: {optimisticData: {editOptimisticData}}} = useDreamsData()
Expand Down

0 comments on commit cb01424

Please sign in to comment.