-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Selective context works well for generalized DTO store with entity na…
…me key pattern.
- Loading branch information
1 parent
3eaaf26
commit 24aed34
Showing
7 changed files
with
137 additions
and
29 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
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,37 @@ | ||
'use client'; | ||
import { useSelectiveContextGlobalListener } from '../selective-context/components/global/selective-context-manager-global'; | ||
import { ObjectPlaceholder } from '../selective-context/components/typed/selective-context-manager-function'; | ||
import { NameIdStringTuple } from '../api/dtos/NameIdStringTupleSchema'; | ||
import { Badge, Card } from '@tremor/react'; | ||
import { | ||
useDtoStoreDispatch, | ||
useDtoStoreListener | ||
} from '../selective-context/hooks/dtoStores/use-dto-store'; | ||
import { HasNumberIdDto } from '../api/dtos/HasNumberIdDtoSchema'; | ||
import { HasNameDto } from '../api/dtos/HasNameDtoSchema'; | ||
import { isNotUndefined } from '../api/main'; | ||
import { PendingOverlay } from '../generic/components/overlays/pending-overlay'; | ||
import { Button } from '@nextui-org/button'; | ||
|
||
export default function SomeComponentInterestedInADto< | ||
T extends HasNumberIdDto & HasNameDto | ||
>({ entityName, id }: { entityName: string; id: string | number }) { | ||
const { currentState, dispatchWithoutControl } = useDtoStoreDispatch<T>( | ||
id, | ||
entityName, | ||
'someComponent' | ||
); | ||
console.log(currentState); | ||
|
||
const handleClick = () => { | ||
dispatchWithoutControl((dto) => ({ ...dto, name: `${dto.name}ol` })); | ||
}; | ||
|
||
return ( | ||
<Card> | ||
{<PendingOverlay pending={currentState === ObjectPlaceholder} />} | ||
<Button onPress={handleClick}>Current name: {currentState.name}</Button> | ||
<Badge>Current id: {currentState.id}</Badge> | ||
</Card> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
app/selective-context/components/controllers/dto-controller-array.tsx
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,22 @@ | ||
import { HasNumberIdDto } from '../../../api/dtos/HasNumberIdDtoSchema'; | ||
import { HasUuidDto } from '../../../api/dtos/HasUuidDtoSchema'; | ||
import DtoController from './dto-controller'; | ||
|
||
export interface DtoControllerArrayProps< | ||
T extends HasNumberIdDto | HasUuidDto | ||
> { | ||
dtoArray: T[]; | ||
entityName: string; | ||
} | ||
|
||
export default function DtoControllerArray< | ||
T extends HasNumberIdDto | HasUuidDto | ||
>({ dtoArray, entityName }: DtoControllerArrayProps<T>) { | ||
return dtoArray.map((dto) => ( | ||
<DtoController | ||
key={`${entityName}:${dto.id}`} | ||
dto={dto} | ||
entityName={entityName} | ||
/> | ||
)); | ||
} |
18 changes: 18 additions & 0 deletions
18
app/selective-context/components/controllers/dto-controller.tsx
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,18 @@ | ||
'use client'; | ||
import { HasNumberIdDto } from '../../../api/dtos/HasNumberIdDtoSchema'; | ||
import { HasUuidDto } from '../../../api/dtos/HasUuidDtoSchema'; | ||
import { useDtoStoreController } from '../../hooks/dtoStores/use-dto-store'; | ||
|
||
export interface DtoControllerProps<T extends HasNumberIdDto | HasUuidDto> { | ||
dto: T; | ||
entityName: string; | ||
} | ||
|
||
export default function DtoController<T extends HasNumberIdDto | HasUuidDto>({ | ||
dto, | ||
entityName | ||
}: DtoControllerProps<T>) { | ||
const { currentState } = useDtoStoreController(dto, entityName); | ||
console.log(currentState); | ||
return null; | ||
} |
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,41 @@ | ||
import { | ||
useSelectiveContextAnyController, | ||
useSelectiveContextAnyDispatch, | ||
useSelectiveContextGlobalListener | ||
} from '../../components/global/selective-context-manager-global'; | ||
import { HasUuidDto } from '../../../api/dtos/HasUuidDtoSchema'; | ||
import { HasNumberIdDto } from '../../../api/dtos/HasNumberIdDtoSchema'; | ||
import { ObjectPlaceholder } from '../../components/typed/selective-context-manager-function'; | ||
|
||
export function useDtoStoreController<T extends HasUuidDto | HasNumberIdDto>( | ||
dto: T, | ||
entityType: string | ||
) { | ||
return useSelectiveContextAnyController<T>({ | ||
contextKey: `${entityType}:${dto.id}`, | ||
initialValue: dto, | ||
listenerKey: 'controller' | ||
}); | ||
} | ||
export function useDtoStoreDispatch<T extends HasUuidDto | HasNumberIdDto>( | ||
id: number | string, | ||
entityType: string, | ||
listenerKey: string | ||
) { | ||
return useSelectiveContextAnyDispatch<T>({ | ||
contextKey: `${entityType}:${id}`, | ||
initialValue: ObjectPlaceholder as T, | ||
listenerKey: listenerKey | ||
}); | ||
} | ||
export function useDtoStoreListener<T extends HasUuidDto | HasNumberIdDto>( | ||
id: number | string, | ||
entityType: string, | ||
listenerKey: string | ||
) { | ||
return useSelectiveContextGlobalListener<T>({ | ||
contextKey: `${entityType}:${id}`, | ||
initialValue: ObjectPlaceholder as T, | ||
listenerKey: listenerKey | ||
}); | ||
} |