Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form new create #5

Open
wants to merge 12 commits into
base: external_resource_submission
Choose a base branch
from
26 changes: 26 additions & 0 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class ResourcesController < ApplicationController
skip_before_action :if_not_signed_in

def new
@external_resource = ExternalResource.new
end

def create
@external_resource = ExternalResource.new external_resources_params
if @external_resource.save
redirect_to new_resource_path,
notice: 'Your suggestion has been submitted!'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a translation key

else
redirect_to new_resource_path,
alert: @external_resource.errors.full_messages.join(',')
end
end

private

def external_resources_params
params.require(:external_resource).permit(:name, :link, languages: [])
end
end
96 changes: 96 additions & 0 deletions app/helpers/external_resources_form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# frozen_string_literal: true
module ExternalResourcesFormHelper
include FormHelper

def new_external_resource_props
new_form_props(external_resources_form_inputs, resources_path)
end

private

def external_resource_input_props(field, type, label, group = false)
{ id: "external_resource_#{field}",
type: type,
name: "external_resource[#{field}]#{group ? '[]' : ''}",
label: label }
end

def external_resources_form_inputs
[external_resource_name, external_resource_link, external_resource_languages]
end

def external_resource_text_input_props(field, type, label, required = false)
external_resource_input_props(field, type, label)
.merge(value: @external_resource[field] || nil, required: required, dark: false)
end

def external_resource_name
external_resource_text_input_props('name', 'text', 'name', true)
end

def external_resource_link
external_resource_text_input_props('link', 'text', 'link', true)
end

def external_resource_languages
{
id: 999,
name: 'external_resource[languages][]',
label: 'Choose a language',

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might need a tranlsation key

type: 'select',
value: 'languages',
options: [
{
id: 1,
value: 'en',
label: 'English'
},
{
id: 2,
value: 'pt-BR',
label: 'Portuguese'
},
{
id: 3,
value: 'es',
label: 'Spanish'
},
{
id: 4,
value: 'fr',
label: 'French'
},
{
id: 5,
value: 'it',
label: 'Italian'
},
{
id: 6,
value: 'hi',
label: 'Hindi'
},
{
id: 7,
value: 'nb',
label: 'Norwegian'
},
{
id: 8,
value: 'nl',
label: 'Dutch'
},
{
id: 9,
value: 'sv',
label: 'Swedish'
},
{
id: 10,
value: 'vi',
label: 'Vietnamese'
}
]
}
end
end
1 change: 1 addition & 0 deletions app/views/pages/resources.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<% title t('navigation.resources') %>
<%= react_component('Resources', props: { resources: @resources, keywords: @keywords }) %>

2 changes: 2 additions & 0 deletions app/views/resources/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<% title "#{t('pages.resources.new')}" %>
<%= react_component('Form', props: new_external_resource_props) %>
46 changes: 28 additions & 18 deletions client/app/widgets/Resources/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ const sortAlpha = (checkboxes: Checkbox[]): Checkbox[] =>

const infoDescription = (
<center className={css.marginBottom}>
<p>
<a href={`/resources/new`}>
{I18n.t('pages.resources.external_resource')}
</a>
</p>
{I18n.t('pages.resources.description')}
<p>
<a
Expand All @@ -59,8 +64,10 @@ const createCheckboxes = (resources: ResourceProp[], keywords: string[]) => {
const tagsList = [
...new Set(
resources
.map((resource: ResourceProp) => resource.tags.concat(resource.languages))
.reduce((acc, val) => acc.concat(val), []),
.map((resource: ResourceProp) =>
resource.tags.concat(resource.languages)
)
.reduce((acc, val) => acc.concat(val), [])
),
];
return sortAlpha(
Expand All @@ -70,23 +77,24 @@ const createCheckboxes = (resources: ResourceProp[], keywords: string[]) => {
value: tag,
label: tag,
checked: keywords.some(
(keyword) => keyword.toLowerCase() === tag.toLowerCase(),
(keyword) => keyword.toLowerCase() === tag.toLowerCase()
),
})),
}))
);
};

const filterList = (
checkboxes: Checkbox[],
resources: ResourceProp[],
resources: ResourceProp[]
): ResourceProp[] => {
const selectedCheckboxes: Checkbox[] = checkboxes.filter(
(checkbox: Checkbox) => !!checkbox.checked,
(checkbox: Checkbox) => !!checkbox.checked
);
return resources.filter((resource: ResourceProp) => {
const tagCheck = selectedCheckboxes.map((checkbox: Checkbox) =>
// eslint-disable-next-line implicit-arrow-linebreak
resource.tags.concat(resource.languages).includes(checkbox.id));
resource.tags.concat(resource.languages).includes(checkbox.id)
);
return selectedCheckboxes.length > 0 ? tagCheck.includes(true) : true;
});
};
Expand All @@ -97,16 +105,16 @@ export const Resources = ({
history = HistoryLib,
}: Props) => {
const [checkboxes, setCheckboxes] = useState<Checkbox[]>(
createCheckboxes(resources, keywords),
createCheckboxes(resources, keywords)
);
const [filteredResources, setFilteredResources] = useState<ResourceProp[]>(
filterList(checkboxes, resources),
filterList(checkboxes, resources)
);
const [resourcesDisplayed, setResourcesDisplayed] = useState<number>(
Math.min(RESOURCES_PER_PAGE, filteredResources.length),
Math.min(RESOURCES_PER_PAGE, filteredResources.length)
);
const [lastPage, setLastPage] = useState<boolean>(
filteredResources.length <= RESOURCES_PER_PAGE,
filteredResources.length <= RESOURCES_PER_PAGE
);

useEffect(() => {
Expand All @@ -128,28 +136,30 @@ export const Resources = ({
useEffect(() => {
setLastPage(filteredResources.length <= RESOURCES_PER_PAGE);
setResourcesDisplayed(
Math.min(RESOURCES_PER_PAGE, filteredResources.length),
Math.min(RESOURCES_PER_PAGE, filteredResources.length)
);
}, [filteredResources]);

const checkboxChange = (box: Checkbox) => {
setCheckboxes(
checkboxes.filter((checkbox) => checkbox.id !== box.id).concat(box),
checkboxes.filter((checkbox) => checkbox.id !== box.id).concat(box)
);
};

const updateTagFilter = (tagLabel: String) => {
const updatedBoxes = checkboxes.map((box) =>
// eslint-disable-next-line implicit-arrow-linebreak
(box.label === tagLabel ? { ...box, checked: true } : box));
box.label === tagLabel ? { ...box, checked: true } : box
);
setCheckboxes(updatedBoxes);
};

const onClick = () => {
const updatedResourcesDisplayed = Math.min(
filteredResources.length - resourcesDisplayed,
RESOURCES_PER_PAGE,
) + resourcesDisplayed;
const updatedResourcesDisplayed =
Math.min(
filteredResources.length - resourcesDisplayed,
RESOURCES_PER_PAGE
) + resourcesDisplayed;

setResourcesDisplayed(updatedResourcesDisplayed);
setLastPage(filteredResources.length === updatedResourcesDisplayed);
Expand Down
2 changes: 2 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,8 @@ de:
hast.</p><p class="no_margin_bottom"> <em>Datenschutzrichtlinie
angepasst von %{jamie_king_media}.</em></p>
resources:
new: Eine neue Ressource vorschlagen
external_resource: Eine Ressource vorschlagen
description: >-
Dies ist unsere kuratierte Liste von Ressourcen für psychische
Gesundheit, die Gemeinschaften, Bildungsinstrumente, Hotlines und
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,8 @@ en:
class="noMarginBottom"><em>Privacy Policy adapted from
%{jamie_king_media}.</em></p>
resources:
new: Suggest a new resource
external_resource: Suggest a resource
description: >-
This is our curated list of mental health resources, which includes
communities, educational tools, hotlines, and services.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,8 @@ es:
preocupaciones.</p><p class="noMarginBottom"><em>Política de Privacidad
adoptada de %{jamie_king_media}.</em></p>
resources:
new: Sugerir un nuevo recurso
external_resource: Sugerir un recurso
description: >-
Esta es nuestra lista curada de recursos de salud mental, que incluye
comunidades, herramientas educativas, líneas directas y servicios.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ fr:
class="noMarginBottom"><em> Politique de confidentialité adaptée de
%{jamie_king_media}.</em></p>
resources:
new: Suggérer une nouvelle ressource
external_resource: Suggérer une ressource
description: >-
Voici notre liste de ressources en santé mentale, comprenant des
communautés, des outils éducatifs, des lignes directes et des services.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/hi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,8 @@ hi:
class="noMarginBottom"><em> गोपनीयता नीति %{jamie_king_media}</em></p>
से अनुकूलित है।
resources:
new: एक नया संसाधन सुझाएं
external_resource: एक संसाधन सुझाएं
description: >-
यह मानसिक स्वास्थ्य संसाधनों की हमारी क्यूरेटेड सूची है, जिसमें समुदाय,
शैक्षिक उपकरण, हॉटलाइन और सेवाएं शामिल हैं।
Expand Down
2 changes: 2 additions & 0 deletions config/locales/it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@ it:
qualcosa che ti preoccupa.</p> <p class="noMarginBottom"><em>Informativa
sulla Privacy adattata da %{jamie_king_media}.</em></p>
resources:
new: Suggerire una nuova risorsa
external_resource: Suggerire una risorsa
description: >-
Questa è la nostra lista curata di risorse per la salute mentale, che
include comunità, strumenti educativi, hotline e servizi.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/ko.yml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,8 @@ ko:
class="noMarginBottom"><em>개인 정보 보호 정책이 %{jamie_king_media}에서
조정되었습니다.</em></p>
resources:
new: 새로운 자원을 제안하다
external_resource: 자원 제안
description: '이것은 커뮤니티, 교육 도구, 핫라인, 서비스를 포함하는 정신 건강 자원의 큐레이션된 목록입니다.'
emergency: '만약 여러분이나 여러분이 아는 누군가가 정신 건강이 심각할 정도로 악화되고 있다면, 가능한 한 빨리 도움을 요청하세요.'
crisis_prevention:
Expand Down
2 changes: 2 additions & 0 deletions config/locales/nb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,8 @@ nb:
bekymringer.</p><p class="noMarginBottom"><em>Retningslinjer for
personvern tilpasset fra %{jamie_king_media}.</em></p>
resources:
new: Foreslå en ny ressurs
external_resource: Foreslå en ressurs
description: >-
Dette er vår kuraterte liste over mental helse ressurser, som inkluderer
lokalsamfunn, pedagogiske verktøy, hotlines og tjenester.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/nl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,8 @@ nl:
class="noMarginBottom"><em>Privacy Policy gebaseerd op
%{jamie_king_media}.</em></p>
resources:
new: Stel een nieuwe bron voor
external_resource: Stel een bron voor
description: >-
Dit is onze samengestelde lijst met bronnen voor geestelijke
gezondheidszorg, waaronder communities, educatieve tools, hotlines en
Expand Down
2 changes: 2 additions & 0 deletions config/locales/pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@ pt-BR:
class="noMarginBottom"><em>Política de Privacidade adaptada de
%{jamie_king_media}.</em></p>
resources:
new: Sugira um novo recurso
external_resource: Sugira um recurso
description: >-
Esta é a nossa lista de recursos de saúde mental, que inclui
comunidades, ferramentas educacionais, linhas diretas e serviços.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/sv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,8 @@ sv:
class="noMarginBottom"><em>Sekretesspolicy anpassad från
%{jamie_king_media}.</em></p>
resources:
new: Föreslå en ny resurs
external_resource: Föreslå en resurs
description: >-
Det här är vår kurerade lista över mentala resurser, som inkluderar
samhällen, pedagogiska verktyg, hotlines och tjänster.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/vi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@ vi:
nào.</p><p class="noMarginBottom"><em>Chính sách Bảo mật được điều chỉnh
từ% {jamie_king_media}.</em></p>
resources:
new: Đề xuất một tài nguyên mới
external_resource: Đề xuất một tài nguyên
description: >-
Đây là danh sách các tài nguyên sức khỏe tâm thần của chúng tôi, bao gồm
các cộng đồng, công cụ giáo dục, đường dây nóng và dịch vụ.
Expand Down
2 changes: 2 additions & 0 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ zh-CN:
<p>如果有任何疑问或疑问,请%{email_link}。</p> <p class =“ noMarginBottom”>
<em>根据%{jamie_king_media}改编的隐私权政策。 </em> </p>
resources:
new: 提出新资源
external_resource: 建议资源
description: 这是我们精选的精神健康参考资料小结,其中包括社区,教育工具,热线和服务。
emergency: 如果你或你认识的某个人遇到精神健康紧急情况,请尽快寻求专业帮助。
crisis_prevention:
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
end
end

resources :resources, only: %i[new create]

get 'pages/home'
match 'about', to: 'pages#about', via: :get
match 'admin_dashboard', to: 'pages#admin_dashboard', via: :get
Expand Down