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
25 changes: 25 additions & 0 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
class ResourcesController < ApplicationController
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
skip_before_action :if_not_signed_in
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved

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("', '")
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
end
end

private

def external_resources_params
params.require(:external_resource).permit(:name, :link, languages: [])
end
end
95 changes: 95 additions & 0 deletions app/helpers/external_resources_form_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 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 }
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
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 'New Resource View' %>
<%= 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>
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
{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
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -632,11 +632,12 @@ en:
%{jamie_king_media}.</em></p>
resources:
description: >-
This is our curated list of mental health resources, which includes
This is our ed list of mental health resources, which includes
AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
communities, educational tools, hotlines, and services.
emergency: >-
If you or someone you know is experiencing a mental health emergency,
reach out for help as soon as possible.
external_resource: Suggest a resource
crisis_prevention:
title: How are you doing?
description: You are loved no matter what
Expand Down
3 changes: 3 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 All @@ -121,6 +123,7 @@
match 'resources', to: 'pages#resources', via: :get
get 'home_data', to: 'pages#home_data', defaults: { format: 'json' }


AlineRibeiro marked this conversation as resolved.
Show resolved Hide resolved
devise_for :users, controllers: { registrations: :registrations,
omniauth_callbacks: 'omniauth_callbacks',
invitations: 'users/invitations',
Expand Down