Skip to content

Commit

Permalink
Merge pull request #4205 from omgitsmiles/WV-890-politician-page-pre-…
Browse files Browse the repository at this point in the history
…load-option-display-name-and-state-information-prior-to-api-responses

WV-490 parse politician name and state, render on before load
  • Loading branch information
DaleMcGrew authored Dec 15, 2024
2 parents 67d8474 + fb97c9d commit 8e741f1
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,4 @@ tests/browserstack*/config/browserstack.config.js
tests/browserstack*/capabilities/mobile.json
tests/browserstack*/screenshots/
storybook-static/
compileDate.js.backup
45 changes: 43 additions & 2 deletions src/js/common/pages/Politician/PoliticianDetailsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,38 @@ function marginTopOffset (scrolledDown) {
return 0;
}

function extractPoliticianDetailsFromUrl(url) {
// Split the URL into parts using '/'
const parts = url.split('/');

// assume the second part of the path is the SEO-friendly string ("nancy-a-montgomery-politician-from-new-york")
const seoFriendlyPart = parts[1];
console.log('SEO Friendly Part:', seoFriendlyPart);

if (!seoFriendlyPart) {
return { state: null, name: null }; // If there's no seoFriendlyPart, return null for state and name
}

// Split the SEO-friendly part by dashes to get the name and state words
const words = seoFriendlyPart.split('-');
console.log('Words:', words);

const fromIndex = words.lastIndexOf('from'); // Look for the last occurrence of "from", as it typically separates the name and state, reduce chances of pulling "from" in the name
console.log('From last Index:', fromIndex);

if (fromIndex === -1) {
return { state: null, name: null }; // If 'from' is not found, return null for both
}

// Extract state and name based on the position of 'from'
const state = words.slice(fromIndex + 1).join(' '); // Combine words after 'from' for the state
const name = words.slice(0, fromIndex).join(' '); // Combine words before 'from' for the name
const nameWithoutPolitician = name ? name.replace(/\bpolitician\b/i, '').trim() : null;


return { state, name: nameWithoutPolitician };
}


class PoliticianDetailsPage extends Component {
constructor (props) {
Expand Down Expand Up @@ -124,6 +156,8 @@ class PoliticianDetailsPage extends Component {
supporterEndorsementsWithText: [],
voterCanEditThisPolitician: false,
wikipediaUrl: '',
politicianStateParsedFromURLBeforeLoad: '',
politicianNameParsedFromURLBeforeLoad: '',
// youtubeUrl: '',
};
// this.onScroll = this.onScroll.bind(this);
Expand All @@ -133,6 +167,11 @@ class PoliticianDetailsPage extends Component {
// console.log('PoliticianDetailsPage componentDidMount');
const { match: { params } } = this.props;
const { politicianSEOFriendlyPath: politicianSEOFriendlyPathFromUrl, politicianWeVoteId } = params;
const { state, name } = extractPoliticianDetailsFromUrl(this.props.match.url); // Using the match URL for parsing
this.setState({
politicianStateParsedFromURLBeforeLoad: state,
politicianNameParsedFromURLBeforeLoad: name,
});
// console.log('componentDidMount politicianSEOFriendlyPathFromUrl: ', politicianSEOFriendlyPathFromUrl, ', politicianWeVoteId: ', politicianWeVoteId);
this.onAppObservableStoreChange();
this.appStateSubscription = messageService.getMessage().subscribe(() => this.onAppObservableStoreChange());
Expand Down Expand Up @@ -592,6 +631,8 @@ class PoliticianDetailsPage extends Component {
render () {
renderLog('PoliticianDetailsPage'); // Set LOG_RENDER_EVENTS to log all renders

const { politicianStateParsedFromURLBeforeLoad } = this.state; // reaname variable state calculated from URL to be used when page is still loading.
const { politicianNameParsedFromURLBeforeLoad } = this.state; // reaname variable name calculated from URL to be used when page is still loading.
const { classes } = this.props;
const { match: { params } } = this.props;
const { politicianSEOFriendlyPath: politicianSEOFriendlyPathFromUrl } = params;
Expand Down Expand Up @@ -779,7 +820,7 @@ class PoliticianDetailsPage extends Component {
officeName={contestOfficeName}
politicalParty={candidateCampaign.party}
showOfficeName={showOfficeName}
stateName={stateName}
stateName={stateName || politicianStateParsedFromURLBeforeLoad}
year={`${year}`}
/>
</CandidateCampaignWrapper>
Expand Down Expand Up @@ -923,7 +964,7 @@ class PoliticianDetailsPage extends Component {
{/* Candidate Name */}
<CandidateNameAndPartyWrapper>
<CandidateNameH4>
{politicianName}
{politicianName || politicianNameParsedFromURLBeforeLoad}
</CandidateNameH4>
<CandidateParty>
{politicalParty}
Expand Down
9 changes: 8 additions & 1 deletion src/js/components/Settings/DeleteYourAccountButton.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ class DeleteYourAccountButton extends React.Component {
if (this.updateMessage) clearInterval(this.updateMessage);
}

onVoterStoreChange () {
onVoterStoreChange = () => {
const voterSignedIn = VoterStore.getVoterIsSignedIn();
if (!voterSignedIn) {
const { history } = this.props;
console.log(history);
if (!history || typeof history.replace !== 'function') {
console.error('History object is undefined or invalid:', history);
return;
} else {
console.log('History object is valid, replacing with:', history.location);
}
const location = {
pathname: '/ready',
state: {
Expand Down

0 comments on commit 8e741f1

Please sign in to comment.