Developed an interactive map feature to display agents based on user localities.
Implemented Hashing and Encryption for login and registration.
Resolved time slot conflicts to prevent double booking of agents.
" and this is the code which you have to modify "import React, { useState, useEffect, useRef } from 'react';
import { useStaticQuery, graphql } from 'gatsby';
import { CSSTransition } from 'react-transition-group';
import styled from 'styled-components';
import { srConfig } from '@config';
import { KEY_CODES } from '@utils';
import sr from '@utils/sr';
import { usePrefersReducedMotion } from '@hooks';
/* Pseudo-elements for horizontal lines */
&::before,
&::after {
content: '';
flex: 1;
border-bottom: 1px solid SlateGray; /* Adjust color and thickness as needed */
margin: 0 15px; /* Space between lines and text */
}
useEffect(() => {
if (prefersReducedMotion) {
return;
}
sr.reveal(revealContainer.current, srConfig());
}, []);
const focusTab = () => {
if (tabs.current[tabFocus]) {
tabs.current[tabFocus].focus();
return;
}
// If we're at the end, go to the start
if (tabFocus >= tabs.current.length) {
setTabFocus(0);
}
// If we're at the start, move to the end
if (tabFocus < 0) {
setTabFocus(tabs.current.length - 1);
}
};
// Only re-run the effect if tabFocus changes
useEffect(() => focusTab(), [tabFocus]);
// Focus on tabs when using up & down arrow keys
const onKeyDown = e => {
switch (e.key) {
case KEY_CODES.ARROW_UP: {
e.preventDefault();
setTabFocus(tabFocus - 1);
break;
}
To display the jobs in descending order of date (with the latest job at the top), you can ensure this sorting both in your GraphQL query and within your React component. While your current GraphQL query already sorts the jobs by frontmatter.date in descending order, it's a good practice to also handle sorting within the component to account for any unforeseen discrepancies.
Here's how you can modify your code to achieve the desired sorting:
Ensure the GraphQL Query Sorts Jobs by Date in Descending Order
Your existing GraphQL query correctly sorts the jobs by the date field in descending order. However, ensure that the date field in your Markdown files is consistently formatted (preferably in YYYY-MM-DD format) to allow accurate sorting.
const data = useStaticQuery(graphql`
query {
jobs: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/jobs/" } }
sort: { fields: [frontmatter___date], order: DESC }
) {
edges {
node {
frontmatter {
title
company
location
range
url
date // Ensure the date field is included
}
html
}
}
}
}
`);
Sort Jobs Data Within the Component (Optional but Recommended)
To add an extra layer of assurance that the jobs are sorted correctly, you can sort the jobsData array within the component based on the date field. This is especially useful if there's any inconsistency in the data or if additional transformations are needed.
import React, { useState, useEffect, useRef } from 'react';
import { useStaticQuery, graphql } from 'gatsby';
import { CSSTransition } from 'react-transition-group';
import styled from 'styled-components';
import { srConfig } from '@config';
import { KEY_CODES } from '@utils';
import sr from '@utils/sr';
import { usePrefersReducedMotion } from '@hooks';
// ... [Styled Components Definitions] ...
const Jobs = () => {
const data = useStaticQuery(graphql`
query {
jobs: allMarkdownRemark(
filter: { fileAbsolutePath: { regex: "/content/jobs/" } }
sort: { fields: [frontmatter___date], order: DESC }
) {
edges {
node {
frontmatter {
title
company
location
range
url
date // Ensure the date field is included
}
html
}
}
}
}
`);
const jobsData = data.jobs.edges;
// Sort jobsData in descending order based on the date field
const sortedJobs = [...jobsData].sort((a, b) => {
const dateA = new Date(a.node.frontmatter.date);
const dateB = new Date(b.node.frontmatter.date);
return dateB - dateA; // Descending order
});
const [activeTabId, setActiveTabId] = useState(0);
const [tabFocus, setTabFocus] = useState(null);
const tabs = useRef([]);
const revealContainer = useRef(null);
const prefersReducedMotion = usePrefersReducedMotion();
useEffect(() => {
if (prefersReducedMotion) {
return;
}
sr.reveal(revealContainer.current, srConfig());
}, []);
const focusTab = () => {
if (tabs.current[tabFocus]) {
tabs.current[tabFocus].focus();
return;
}
// If we're at the end, go to the start
if (tabFocus >= tabs.current.length) {
setTabFocus(0);
}
// If we're at the start, move to the end
if (tabFocus < 0) {
setTabFocus(tabs.current.length - 1);
}
};
// Only re-run the effect if tabFocus changes
useEffect(() => focusTab(), [tabFocus]);
// Focus on tabs when using up & down arrow keys
const onKeyDown = e => {
switch (e.key) {
case KEY_CODES.ARROW_UP: {
e.preventDefault();
setTabFocus(tabFocus - 1);
break;
}
case KEY_CODES.ARROW_DOWN: {
e.preventDefault();
setTabFocus(tabFocus + 1);
break;
}
default: {
break;
}
}
};
return (
<StyledJobsSection id="jobs" ref={revealContainer}>
<h2 className="numbered-heading">Where I’ve Worked</h2>
<div className="inner">
<StyledTabList role="tablist" aria-label="Job tabs" onKeyDown={e => onKeyDown(e)}>
{sortedJobs &&
sortedJobs.map(({ node }, i) => {
const { company } = node.frontmatter;
return (
<StyledTabButton
key={i}
isActive={activeTabId === i}
onClick={() => setActiveTabId(i)}
ref={el => (tabs.current[i] = el)}
id={`tab-${i}`}
role="tab"
tabIndex={activeTabId === i ? '0' : '-1'}
aria-selected={activeTabId === i ? true : false}
aria-controls={`panel-${i}`}>
<span>{company}</span>
</StyledTabButton>
);
})
}
<StyledHighlight activeTabId={activeTabId} />
</StyledTabList>
<StyledTabPanels>
{sortedJobs &&
sortedJobs.map(({ node }, i) => {
const { frontmatter, html } = node;
const { title, url, company, range } = frontmatter;
return (
<CSSTransition key={i} in={activeTabId === i} timeout={250} classNames="fade">
<StyledTabPanel
id={`panel-${i}`}
role="tabpanel"
tabIndex={activeTabId === i ? '0' : '-1'}
aria-labelledby={`tab-${i}`}
aria-hidden={activeTabId !== i}
hidden={activeTabId !== i}>
<h3>
<span>{title}</span>
<span className="company">
@
<a href={url} className="inline-link">
{company}
</a>
</span>
</h3>
<p className="range">{range}</p>
<div dangerouslySetInnerHTML={{ __html: html }} />
</StyledTabPanel>
</CSSTransition>
);
})}
</StyledTabPanels>
</div>
</StyledJobsSection>
);
};
export default Jobs;
Explanation of Changes
GraphQL Query Adjustment: Ensure that the date field is included in the frontmatter selection within your GraphQL query. This is crucial for both sorting in the query and any potential use within the component.
Sorting Within the Component:
Cloning the Array: Instead of directly sorting jobsData (which could mutate the original data), create a new array using the spread operator [...jobsData].
Sorting Logic: Use the sort method to compare the date fields of each job. By converting the date strings to Date objects, you ensure accurate comparisons.
Descending Order: dateB - dateA ensures that newer dates come first.
Using sortedJobs for Rendering: Replace jobsData with sortedJobs in both the StyledTabList and StyledTabPanels sections to ensure the rendered order reflects the sorted data.
Additional Recommendations
Date Format Consistency: Ensure that all job entries have the date field in a consistent and sortable format (e.g., YYYY-MM-DD). Inconsistent date formats can lead to incorrect sorting or runtime errors.
Error Handling: Consider adding error handling to account for missing or improperly formatted date fields to prevent potential crashes.
Performance Optimization: If your list of jobs is extensive, you might want to memoize the sorted list using useMemo to avoid unnecessary re-sorting on every render.
import React, { useState, useEffect, useRef, useMemo } from 'react';
// ... other imports
const Jobs = () => {
// ... existing code
const sortedJobs = useMemo(() => {
return [...jobsData].sort((a, b) => new Date(b.node.frontmatter.date) - new Date(a.node.frontmatter.date));
}, [jobsData]);
// ... rest of the component
};
By implementing these changes, your jobs section will reliably display the most recent job at the top, ensuring that visitors see your latest experience first.