-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00210fa
commit 8772903
Showing
10 changed files
with
386 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,6 @@ | ||
--- | ||
'demo-store': patch | ||
'@shopify/hydrogen': patch | ||
--- | ||
|
||
Cart Optimistic UI helpers |
117 changes: 115 additions & 2 deletions
117
packages/hydrogen/docs/generated/generated_docs_data.json
Large diffs are not rendered by default.
Oops, something went wrong.
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
38 changes: 38 additions & 0 deletions
38
packages/hydrogen/src/optimistic-ui/OptimisticInput.doc.ts
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,38 @@ | ||
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs'; | ||
|
||
const data: ReferenceEntityTemplateSchema = { | ||
name: 'OptimisticInput', | ||
category: 'components', | ||
isVisualComponent: false, | ||
related: [], | ||
description: | ||
'Creates a form input for optimistic UI updates. Use `useOptimisticData` to update the UI with the latest optimistic data.', | ||
type: 'component', | ||
defaultExample: { | ||
description: 'This is the default example', | ||
codeblock: { | ||
tabs: [ | ||
{ | ||
title: 'JavaScript', | ||
code: './OptimisticInput.example.jsx', | ||
language: 'js', | ||
}, | ||
{ | ||
title: 'TypeScript', | ||
code: './OptimisticInput.example.tsx', | ||
language: 'ts', | ||
}, | ||
], | ||
title: 'example', | ||
}, | ||
}, | ||
definitions: [ | ||
{ | ||
title: 'Props', | ||
type: 'OptimisticInputProps', | ||
description: '', | ||
}, | ||
], | ||
}; | ||
|
||
export default data; |
27 changes: 27 additions & 0 deletions
27
packages/hydrogen/src/optimistic-ui/OptimisticInput.example.jsx
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,27 @@ | ||
import {CartForm, OptimisticInput, useOptimisticData} from '@shopify/hydrogen'; | ||
|
||
export default function Cart({line}) { | ||
const optimisticId = line.id; | ||
const optimisticData = useOptimisticData(optimisticId); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
// Hide the line item if the optimistic data action is remove | ||
// Do not remove the form from the DOM | ||
display: optimisticData?.action === 'remove' ? 'none' : 'block', | ||
}} | ||
> | ||
<CartForm | ||
route="/cart" | ||
action={CartForm.ACTIONS.LinesRemove} | ||
inputs={{ | ||
lineIds: [line.id], | ||
}} | ||
> | ||
<button type="submit">Remove</button> | ||
<OptimisticInput id={optimisticId} data={{action: 'remove'}} /> | ||
</CartForm> | ||
</div> | ||
); | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/hydrogen/src/optimistic-ui/OptimisticInput.example.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,32 @@ | ||
import {CartForm, OptimisticInput, useOptimisticData} from '@shopify/hydrogen'; | ||
import {CartLine} from '@shopify/hydrogen-react/storefront-api-types'; | ||
|
||
type OptimisticData = { | ||
action: string; | ||
}; | ||
|
||
export default function Cart({line}: {line: CartLine}) { | ||
const optimisticId = line.id; | ||
const optimisticData = useOptimisticData<OptimisticData>(optimisticId); | ||
|
||
return ( | ||
<div | ||
style={{ | ||
// Hide the line item if the optimistic data action is remove | ||
// Do not remove the form from the DOM | ||
display: optimisticData?.action === 'remove' ? 'none' : 'block', | ||
}} | ||
> | ||
<CartForm | ||
route="/cart" | ||
action={CartForm.ACTIONS.LinesRemove} | ||
inputs={{ | ||
lineIds: [line.id], | ||
}} | ||
> | ||
<button type="submit">Remove</button> | ||
<OptimisticInput id={optimisticId} data={{action: 'remove'}} /> | ||
</CartForm> | ||
</div> | ||
); | ||
} |
28 changes: 28 additions & 0 deletions
28
packages/hydrogen/src/optimistic-ui/optimistic-ui.test.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,28 @@ | ||
import {describe, expect, it} from 'vitest'; | ||
import {render} from '@testing-library/react'; | ||
import {OptimisticInput} from './optimistic-ui'; | ||
|
||
function getOptimisticIdentifier(container: HTMLElement) { | ||
return container | ||
.querySelector('input[name="optimistic-identifier"]') | ||
?.getAttribute('value'); | ||
} | ||
|
||
function getOptimisticData(container: HTMLElement) { | ||
return container | ||
.querySelector('input[name="optimistic-data"]') | ||
?.getAttribute('value'); | ||
} | ||
|
||
describe('<OptimisticInput />', () => { | ||
it('renders a form with children', () => { | ||
const {container} = render( | ||
<form> | ||
<OptimisticInput id="test" data={{action: 'remove'}} /> | ||
</form>, | ||
); | ||
|
||
expect(getOptimisticIdentifier(container)).toBe('test'); | ||
expect(getOptimisticData(container)).toBe('{"action":"remove"}'); | ||
}); | ||
}); |
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,49 @@ | ||
import {useFetchers} from '@remix-run/react'; | ||
|
||
export function useOptimisticData<T>(identifier: string) { | ||
const fetchers = useFetchers(); | ||
const data: Record<string, unknown> = {}; | ||
|
||
for (const fetcher of fetchers) { | ||
const formData = fetcher.submission?.formData; | ||
if (formData && formData.get('optimistic-identifier') === identifier) { | ||
try { | ||
if (formData.has('optimistic-data')) { | ||
const dataInForm: unknown = JSON.parse( | ||
String(formData.get('optimistic-data')), | ||
); | ||
Object.assign(data, dataInForm); | ||
} | ||
} catch { | ||
// do nothing | ||
} | ||
} | ||
} | ||
return data as T; | ||
} | ||
|
||
export type OptimisticInputProps = { | ||
/** | ||
* A unique identifier for the optimistic input. Use the same identifier in `useOptimisticData` | ||
* to retrieve the optimistic data from actions. | ||
*/ | ||
id: string; | ||
/** | ||
* The data to be stored in the optimistic input. Use for creating an optimistic successful state | ||
* of this form action. | ||
*/ | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export function OptimisticInput({id, data}: OptimisticInputProps) { | ||
return ( | ||
<> | ||
<input type="hidden" name="optimistic-identifier" value={id} /> | ||
<input | ||
type="hidden" | ||
name="optimistic-data" | ||
value={JSON.stringify(data)} | ||
/> | ||
</> | ||
); | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/hydrogen/src/optimistic-ui/useOptimisticData.doc.ts
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,38 @@ | ||
import {ReferenceEntityTemplateSchema} from '@shopify/generate-docs'; | ||
|
||
const data: ReferenceEntityTemplateSchema = { | ||
name: 'useOptimisticData', | ||
category: 'hooks', | ||
isVisualComponent: false, | ||
related: [], | ||
description: | ||
'Gets the latest optimistic data with matching optimistic id from actions. Use `OptimisticInput` to accept optimistic data in forms.', | ||
type: 'component', | ||
defaultExample: { | ||
description: 'This is the default example', | ||
codeblock: { | ||
tabs: [ | ||
{ | ||
title: 'JavaScript', | ||
code: './OptimisticInput.example.jsx', | ||
language: 'js', | ||
}, | ||
{ | ||
title: 'TypeScript', | ||
code: './OptimisticInput.example.tsx', | ||
language: 'ts', | ||
}, | ||
], | ||
title: 'example', | ||
}, | ||
}, | ||
definitions: [ | ||
{ | ||
title: 'Props', | ||
type: 'UseOptimisticDataGeneratedType', | ||
description: '', | ||
}, | ||
], | ||
}; | ||
|
||
export default data; |
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