forked from thewca/worldcubeassociation.org
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Edit Profile Form * Review changes * Fixed unit test issue * Removed old section of edit profile form * Review changes
- Loading branch information
1 parent
c629fc7
commit 4c00e1c
Showing
13 changed files
with
390 additions
and
163 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
class ContactEditProfile < ContactForm | ||
attribute :wca_id | ||
attribute :changes_requested | ||
attribute :edit_profile_reason | ||
attribute :document, attachment: true | ||
|
||
EditProfileChange = Struct.new( | ||
:field, | ||
:from, | ||
:to, | ||
) | ||
|
||
def to_email | ||
UserGroup.teams_committees_group_wrt.metadata.email | ||
end | ||
|
||
def subject | ||
Time.now.strftime("Edit Profile request by #{wca_id} on %d %b %Y at %R") | ||
end | ||
|
||
def headers | ||
super.merge(template_name: "contact_edit_profile") | ||
end | ||
end |
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,5 @@ | ||
<% provide(:title, t('page.contact_edit_profile.title')) %> | ||
<%= react_component("ContactEditProfilePage", { | ||
loggedInUserId: current_user&.id, | ||
recaptchaPublicKey: AppSecrets.RECAPTCHA_PUBLIC_KEY, | ||
}) %> |
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,21 @@ | ||
<p>Hey WRT,</p> | ||
<p> | ||
<%= @resource.wca_id %> requested following change in their profile: | ||
</p> | ||
<table> | ||
<body> | ||
<% @resource.changes_requested.each do |change| %> | ||
<tr> | ||
<td><%= change[:field] %></td> | ||
<td><%= change[:from] %> -> <%= change[:to] %></td> | ||
</tr> | ||
<% end %> | ||
<tr style="height: 1em"> | ||
<td colspan="2"></td> | ||
</tr> | ||
</body> | ||
</table> | ||
<p>You can edit this person <%= link_to "here", panel_index_url(panel_id: 'wrt', wcaId: @resource.wca_id, anchor: User.panel_pages[:editPerson]) %>.</p> | ||
<% if @resource.document.present? %> | ||
<p>Note: There is a proof attachment to this email.</p> | ||
<% end %> |
163 changes: 163 additions & 0 deletions
163
app/webpacker/components/ContactEditProfilePage/EditProfileForm.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,163 @@ | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { Form, Message } from 'semantic-ui-react'; | ||
import ReCAPTCHA from 'react-google-recaptcha'; | ||
import { QueryClient, useQuery } from '@tanstack/react-query'; | ||
import i18n from '../../lib/i18n'; | ||
import { apiV0Urls, contactEditProfileActionUrl } from '../../lib/requests/routes.js.erb'; | ||
import { genders, countries } from '../../lib/wca-data.js.erb'; | ||
import Loading from '../Requests/Loading'; | ||
import Errored from '../Requests/Errored'; | ||
import useSaveAction from '../../lib/hooks/useSaveAction'; | ||
import { fetchJsonOrError } from '../../lib/requests/fetchWithAuthenticityToken'; | ||
import UtcDatePicker from '../wca/UtcDatePicker'; | ||
|
||
const CONTACT_EDIT_PROFILE_FORM_QUERY_CLIENT = new QueryClient(); | ||
|
||
const genderOptions = _.map(genders.byId, (gender) => ({ | ||
key: gender.id, | ||
text: gender.name, | ||
value: gender.id, | ||
})); | ||
|
||
const countryOptions = _.map(countries.byIso2, (country) => ({ | ||
key: country.iso2, | ||
text: country.name, | ||
value: country.iso2, | ||
})); | ||
|
||
export default function EditProfileForm({ | ||
wcaId, | ||
onContactSuccess, | ||
recaptchaPublicKey, | ||
}) { | ||
const [editProfileReason, setEditProfileReason] = useState(); | ||
const [editedProfileDetails, setEditedProfileDetails] = useState(); | ||
const [proofAttachment, setProofAttachment] = useState(); | ||
const [captchaValue, setCaptchaValue] = useState(); | ||
const [captchaError, setCaptchaError] = useState(false); | ||
const [saveError, setSaveError] = useState(); | ||
const { save, saving } = useSaveAction(); | ||
|
||
const { data, isLoading, isError } = useQuery({ | ||
queryKey: ['profileData'], | ||
queryFn: () => fetchJsonOrError(apiV0Urls.persons.show(wcaId)), | ||
}, CONTACT_EDIT_PROFILE_FORM_QUERY_CLIENT); | ||
|
||
const profileDetails = data?.data?.person; | ||
|
||
const isSubmitDisabled = useMemo( | ||
() => !editedProfileDetails || _.isEqual(editedProfileDetails, profileDetails) || !captchaValue, | ||
[captchaValue, editedProfileDetails, profileDetails], | ||
); | ||
|
||
useEffect(() => { | ||
setEditedProfileDetails(profileDetails); | ||
}, [profileDetails]); | ||
|
||
const formSubmitHandler = () => { | ||
const formData = new FormData(); | ||
|
||
formData.append('formValues', JSON.stringify({ | ||
editedProfileDetails, editProfileReason, wcaId, | ||
})); | ||
formData.append('attachment', proofAttachment); | ||
|
||
save( | ||
contactEditProfileActionUrl, | ||
formData, | ||
onContactSuccess, | ||
{ method: 'POST', headers: {}, body: formData }, | ||
setSaveError, | ||
); | ||
}; | ||
|
||
const handleEditProfileReasonChange = (e, { value }) => { | ||
setEditProfileReason(value); | ||
}; | ||
|
||
const handleProofUpload = (event) => { | ||
setProofAttachment(event.target.files[0]); | ||
}; | ||
|
||
const handleFormChange = (e, { name: formName, value }) => { | ||
setEditedProfileDetails((prev) => ({ ...prev, [formName]: value })); | ||
}; | ||
|
||
const handleDobChange = (date) => handleFormChange(null, { | ||
name: 'dob', | ||
value: date, | ||
}); | ||
|
||
if (saving || isLoading) return <Loading />; | ||
if (saveError || isError) return <Errored />; | ||
|
||
return ( | ||
<Form onSubmit={formSubmitHandler}> | ||
<Form.Input | ||
label={i18n.t('activerecord.attributes.user.name')} | ||
name="name" | ||
value={editedProfileDetails?.name} | ||
onChange={handleFormChange} | ||
/> | ||
<Form.Select | ||
options={countryOptions} | ||
label={i18n.t('activerecord.attributes.user.country_iso2')} | ||
name="country_iso2" | ||
search | ||
value={editedProfileDetails?.country_iso2} | ||
onChange={handleFormChange} | ||
/> | ||
<Form.Select | ||
options={genderOptions} | ||
label={i18n.t('activerecord.attributes.user.gender')} | ||
name="gender" | ||
value={editedProfileDetails?.gender} | ||
onChange={handleFormChange} | ||
/> | ||
<Form.Field | ||
label={i18n.t('activerecord.attributes.user.dob')} | ||
name="dob" | ||
control={UtcDatePicker} | ||
showYearDropdown | ||
dateFormatOverride="YYYY-MM-dd" | ||
dropdownMode="select" | ||
isoDate={editedProfileDetails?.dob} | ||
onChange={handleDobChange} | ||
/> | ||
<Form.TextArea | ||
label={i18n.t('page.contact_edit_profile.form.edit_reason.label')} | ||
name="editProfileReason" | ||
required | ||
value={editProfileReason} | ||
onChange={handleEditProfileReasonChange} | ||
/> | ||
<Form.Input | ||
label={i18n.t('page.contact_edit_profile.form.proof_attach.label')} | ||
type="file" | ||
onChange={handleProofUpload} | ||
/> | ||
<Form.Field> | ||
<ReCAPTCHA | ||
sitekey={recaptchaPublicKey} | ||
// onChange is a mandatory parameter for ReCAPTCHA. According to the documentation, this | ||
// is called when user successfully completes the captcha, hence we are assuming that any | ||
// existing errors will be cleared when onChange is called. | ||
onChange={setCaptchaValue} | ||
onErrored={setCaptchaError} | ||
/> | ||
{captchaError && ( | ||
<Message | ||
error | ||
content={i18n.t('page.contact_edit_profile.form.captcha.validation_error')} | ||
/> | ||
)} | ||
</Form.Field> | ||
<Form.Button | ||
type="submit" | ||
disabled={isSubmitDisabled} | ||
> | ||
{i18n.t('page.contact_edit_profile.form.submit_edit_request_button.label')} | ||
</Form.Button> | ||
</Form> | ||
); | ||
} |
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,58 @@ | ||
import React, { useState } from 'react'; | ||
import { Container, Header, Message } from 'semantic-ui-react'; | ||
import { QueryClient, useQuery } from '@tanstack/react-query'; | ||
import i18n from '../../lib/i18n'; | ||
import I18nHTMLTranslate from '../I18nHTMLTranslate'; | ||
import { apiV0Urls } from '../../lib/requests/routes.js.erb'; | ||
import Loading from '../Requests/Loading'; | ||
import { fetchJsonOrError } from '../../lib/requests/fetchWithAuthenticityToken'; | ||
import Errored from '../Requests/Errored'; | ||
import EditProfileForm from './EditProfileForm'; | ||
|
||
const CONTACT_EDIT_PROFILE_QUERY_CLIENT = new QueryClient(); | ||
|
||
export default function ContactEditProfilePage({ loggedInUserId, recaptchaPublicKey }) { | ||
const { data: loggedInUserData, isLoading, isError } = useQuery({ | ||
queryKey: ['userData'], | ||
queryFn: () => fetchJsonOrError(apiV0Urls.users.me.userDetails), | ||
enabled: !!loggedInUserId, | ||
}, CONTACT_EDIT_PROFILE_QUERY_CLIENT); | ||
const wcaId = loggedInUserData?.data?.user?.wca_id; | ||
const [contactSuccess, setContactSuccess] = useState(false); | ||
|
||
if (isLoading) return <Loading />; | ||
if (isError) return <Errored />; | ||
if (!loggedInUserData) { | ||
return ( | ||
<Message error> | ||
<I18nHTMLTranslate i18nKey="page.contact_edit_profile.not_logged_in_error" /> | ||
</Message> | ||
); | ||
} | ||
if (loggedInUserData && !wcaId) { | ||
return ( | ||
<Message error> | ||
<I18nHTMLTranslate i18nKey="page.contact_edit_profile.no_profile_error" /> | ||
</Message> | ||
); | ||
} | ||
if (contactSuccess) { | ||
return ( | ||
<Message | ||
success | ||
content={i18n.t('page.contact_edit_profile.success_message')} | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<Container text> | ||
<Header as="h2">{i18n.t('page.contact_edit_profile.title')}</Header> | ||
<EditProfileForm | ||
wcaId={wcaId} | ||
onContactSuccess={() => setContactSuccess(true)} | ||
recaptchaPublicKey={recaptchaPublicKey} | ||
/> | ||
</Container> | ||
); | ||
} |
Oops, something went wrong.