if (!parentDocId) {
return;
}
// Родительский документ по отношению к текущему
const parentDoc = documentSelector(parentDocId)(useDocumentStore.getState());
// проверяем является ли родительский продукт ГС РКЛ по отношению
// к сохраняемому документу и обновляем его, а также его субпродукты
if (getProductDocCreditModeFromUrn(parentDoc.documentView.templateURN) === 'RKL') {
const { body: parentProductDoc } = await dispatch(
getDocumentEndpoints.getDocument.initiate(
{ documentId: parentDocId, documentType: 'PRODUCT' },
{ forceRefetch: true },
),
).unwrap();
const productLinks = getProductLinksFromDoc(parentProductDoc).map((el) => el.productId);
if (productLinks.length > 0) {
dispatch(getProductDocs({ productLinks })).unwrap();
}
return;
}
// проверяем является ли сохраненный продукт ГС РКЛ и обновляем документ решения (DECISION), а также его субпродукты
if (getProductDocCreditModeFromUrn(newDocumentTemplate.documentView.templateURN) === 'RKL') {
await dispatch(
getDocumentEndpoints.getDocument.initiate(
{ documentId: parentDocId, documentType: 'DECISION' },
{ forceRefetch: true },
),
).unwrap();
const productLinks = getProductLinksFromDoc(newDocumentTemplate).map((el) => el.productId);
if (productLinks.length > 0) {
dispatch(getProductDocs({ productLinks })).unwrap();
}
return;
}
// если верхнии условия не выполнились, то обновляем документ решения (DECISION)
await dispatch(
getDocumentEndpoints.getDocument.initiate(
{ documentId: parentDocId, documentType: 'DECISION' },
{ forceRefetch: true },
),
).unwrap();
},
);
To write Jest tests for your updateDocsAfterEdit thunk, we need to set up a few things, such as mocking the Redux store, action dispatching, and any external dependency functions. The goal will be to cover various scenarios outlined in your code, including the conditions for the different branches of logic.
Here’s an example of how you might write Jest tests for your thunk:
Mocks: The code mocks dependencies such as getDocumentEndpoints, useDocumentStore, and helper functions to isolate the tests.
Tests: Given the conditions in your thunk:
When documentType is DECISION, the function does nothing.
When parentDocId is undefined, it also does nothing.
If the parent document's credit mode is identified as 'RKL', it fetches the product document and dispatches getProductDocs.
If the new document's credit mode is 'RKL', it fetches the decision document and also dispatches getProductDocs.
If none of the earlier conditions are satisfied, it fetches the decision document.
Running Tests: You would run these tests using Jest, and they would assert that the appropriate actions are dispatched based on your thunk's logic.
Keep any details of the functions and structures appropriate to your actual implementation as you adjust this example for your use case.
Creating a "sticky" position in CSS allows an element to stick to a defined position in the viewport while scrolling. Here's how you can create a sticky positioning effect using the position: sticky; property.
Basic Usage
HTML Structure:
You need a container (e.g., a div) and the element you want to make sticky (e.g., a header or sidebar).
html
<div class="container">
<header class="sticky-header">I stick to the top when you scroll!</header>
<main>
<p>Scroll down to see the effect...</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Quisque sit amet accumsan arcu.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Quisque sit amet accumsan arcu.</p>
<p>Scroll down for more...</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
</main>
</div>
CSS Styles:
You need to set the position property to sticky and define a top offset. This means that the element will start as a static element and become "sticky" when it reaches the specified offset from the top of its container.
css
.container {
height: 1500px; /* Just to allow for scrolling */
padding: 20px;
background: #f0f0f0;
}
.sticky-header {
position: sticky;
top: 0; /* Sticks the header to the top when you scroll past it */
background: #4CAF50; /* A distinct background color */
color: white;
padding: 10px 0;
text-align: center;
z-index: 1000; /* Optional: makes sure it stays on top */
}
main {
padding: 10px;
}
How It Works:
position: sticky;: This sets the element to use sticky positioning.
top: 0;: This specifies that the element will "stick" to the top of its container when you scroll down. You can adjust this value to stick the element at different positions (e.g., top: 50px;).
z-index: This ensures that the sticky element appears above other elements as you scroll.
Example:
Here is a complete example combining the HTML and CSS provided above:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sticky Position Example</title>
<style>
.container {
height: 1500px; /* Just to allow for scrolling */
padding: 20px;
background: #f0f0f0;
}
.sticky-header {
position: sticky;
top: 0; /* Sticks the header to the top when you scroll past it */
background: #4CAF50; /* A distinct background color */
color: white;
padding: 10px 0;
text-align: center;
z-index: 1000; /* Optional */
}
main {
padding: 10px;
}
</style>
</head>
<body>
<div class="container">
<header class="sticky-header">I stick to the top when you scroll!</header>
<main>
<p>Scroll down to see the effect...</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Quisque sit amet accumsan arcu.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Quisque sit amet accumsan arcu.</p>
<p>Scroll down for more...</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit...</p>
</main>
</div>
</body>
</html>
Important Considerations:
Browser Compatibility: Sticky positioning is supported in most modern browsers, but it's good to check compatibility if you're targeting older browsers.
Overflow and Ancestry: The sticky element must be within a scrollable ancestor (the closest parent element with a scrolling mechanism) to function properly.
Height of Parent: Ensure the parent element has a height sufficient to allow scrolling; otherwise, the sticky effect may not be noticeable.
Other Positions: You can also use position: sticky; along with right, left, or bottom for sidebars or footer elements. Adjust them similarly like top.
By following these steps, you can create a sticky element that enhances the user interface by keeping important information visible while scrolling.