Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions scripts/cohort-apps-script/Code.gs
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,44 @@ var UNDP_RENAMED = {
'Finally, is there anything else we should know about your learning or participation needs?'
};

function migrateUndpColumns() {
// Tech4Dev Sheet only (Nov 2026): the fit check gained Organisation name/website at the top and a
// phone number after the email. Its headers are the question texts (no Q numbers), so run
// migrateShortColumns once to move the new columns into place. Same safety rules as above.
var SHORT_LABELS = [
'Organisation name',
'Organisation website',
'Where is your AI solution today?',
'Which domain does your proposed AI use case address?',
'Can you organise testing with beneficiaries or frontline teams during the cohort?',
'Can you assign a Tech Lead or Product Owner with approximately 50% availability over six months?',
'Your email',
'Phone number with country code'
];

function migrateUndpColumns() { migrateColumns(TABS.long, UNDP_LABELS, true, UNDP_RENAMED); }
function migrateShortColumns() { migrateColumns(TABS.short, SHORT_LABELS, false, {}); }

// Rebuilds a tab's columns in `labels` order, matching existing columns by question text.
// numbered: headers are "Q<n>. <label>". Backs the tab up first; never drops a value.
function migrateColumns(tabName, labels, numbered, renamed) {
var ss = SHEET_ID ? SpreadsheetApp.openById(SHEET_ID) : SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(TABS.long);
if (!sheet || sheet.getLastRow() < 1) throw new Error('No "' + TABS.long + '" tab with data.');
sheet.copyTo(ss).setName(TABS.long + ' backup ' + Utilities.formatDate(new Date(), 'Asia/Kolkata', 'yyyy-MM-dd HH:mm'));
var sheet = ss.getSheetByName(tabName);
if (!sheet || sheet.getLastRow() < 1) throw new Error('No "' + tabName + '" tab with data.');
sheet.copyTo(ss).setName(tabName + ' backup ' + Utilities.formatDate(new Date(), 'Asia/Kolkata', 'yyyy-MM-dd HH:mm'));

var values = sheet.getDataRange().getValues();
var headers = values[0].map(String);
function label(h) {
var l = h.replace(/^Q\d+\.\s*/, '');
return UNDP_RENAMED[l] || l;
var l = numbered ? h.replace(/^Q\d+\.\s*/, '') : h;
return renamed[l] || l;
}
var used = {};
var cols = [{ header: 'Timestamp', from: [headers.indexOf('Timestamp')] }];
UNDP_LABELS.forEach(function (l, i) {
labels.forEach(function (l, i) {
var from = [];
headers.forEach(function (h, j) { if (h !== 'Timestamp' && label(h) === l) from.push(j); });
from.forEach(function (j) { used[j] = true; });
cols.push({ header: 'Q' + (i + 1) + '. ' + l, from: from });
cols.push({ header: numbered ? 'Q' + (i + 1) + '. ' + l : l, from: from });
});
headers.forEach(function (h, j) {
if (h === 'Timestamp' || used[j] || h === '') return;
Expand Down
2 changes: 1 addition & 1 deletion src/components/cohort/ApplyForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useEffect, useRef, useState } from 'react';
import styled from 'styled-components';
import {
EMAIL_RE,
PHONE_RE,
BLUE,
CORAL_DARK,
MUTED,
Expand All @@ -21,7 +22,6 @@ import {
Result
} from './ui';

const PHONE_RE = /^\+?[\d\s()-]{8,}$/;

const countWords = (text) => text.trim().split(/\s+/).filter(Boolean).length;

Expand Down
79 changes: 59 additions & 20 deletions src/components/cohort/FitCheck.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useRef, useState } from 'react';
import {
EMAIL_RE,
PHONE_RE,
Card,
Heading,
Muted,
Expand Down Expand Up @@ -61,8 +62,23 @@ const QUESTIONS = [
}
];

// Free-text fields: the first two sit above the questions, the last two below. `column` is the Sheet header.
const TEXT = {
org_name: { column: 'Organisation name', autoComplete: 'organization' },
org_website: { column: 'Organisation website', type: 'url', autoComplete: 'url' },
email: { column: 'Your email', type: 'email', autoComplete: 'email' },
phone: { column: 'Phone number with country code', type: 'tel', autoComplete: 'tel' }
};

export default function FitCheck({ scriptUrl }) {
const [answers, setAnswers] = useState({ email: '', domain_other: '', website: '' });
const [answers, setAnswers] = useState({
org_name: '',
org_website: '',
email: '',
phone: '',
domain_other: '',
website: ''
});
const [errors, setErrors] = useState({});
const [status, setStatus] = useState('idle'); // idle | saving | done
const [saveError, setSaveError] = useState('');
Expand All @@ -85,21 +101,30 @@ export default function FitCheck({ scriptUrl }) {
if (answers.domain === OTHER && !answers.domain_other.trim()) {
e.domain_other = 'Please specify the domain.';
}
if (!answers.email.trim()) e.email = REQUIRED;
else if (!EMAIL_RE.test(answers.email.trim())) e.email = 'Please enter a valid email address.';
Object.keys(TEXT).forEach((name) => {
if (!answers[name].trim()) e[name] = REQUIRED;
});
if (!e.email && !EMAIL_RE.test(answers.email.trim())) e.email = 'Please enter a valid email address.';
if (!e.phone && !PHONE_RE.test(answers.phone.trim())) {
e.phone = 'Please enter a phone number with country code, e.g. +91 98765 43210.';
}
return e;
};

const save = async () => {
setStatus('saving');
// The Sheet's column headers are the question texts.
// `website` is the spam honeypot, not the organisation's website.
const data = { form: 'short', website: answers.website };
data[TEXT.org_name.column] = answers.org_name.trim();
data[TEXT.org_website.column] = answers.org_website.trim();
QUESTIONS.forEach((q) => {
// "Other" is saved as "Other: <what they typed>" in the same column.
data[q.legend] =
q.name === 'domain' && answers.domain === OTHER ? `Other: ${answers.domain_other.trim()}` : answers[q.name];
});
data['Your email'] = answers.email.trim();
data[TEXT.email.column] = answers.email.trim();
data[TEXT.phone.column] = answers.phone.trim();
// Apps Script does not keep the order fields arrive in, so say what the column order should be.
data._order = JSON.stringify(Object.keys(data).filter((k) => k !== 'form' && k !== 'website'));
let error = '';
Expand All @@ -119,10 +144,10 @@ export default function FitCheck({ scriptUrl }) {
event.preventDefault();
const e = validate();
setErrors(e);
const order = [...QUESTIONS.map((q) => q.name), 'domain_other', 'email'];
const order = ['org_name', 'org_website', ...QUESTIONS.map((q) => q.name), 'domain_other', 'email', 'phone'];
const first = order.find((name) => e[name]);
if (first) {
const id = first === 'domain_other' || first === 'email' ? `fit-${first}` : `fit-${first}-0`;
const id = QUESTIONS.some((q) => q.name === first) ? `fit-${first}-0` : `fit-${first}`;
const el = document.getElementById(id);
if (el) {
el.scrollIntoView({ behavior: 'smooth', block: 'center' });
Expand All @@ -133,11 +158,37 @@ export default function FitCheck({ scriptUrl }) {
save();
};

const textField = (name) => {
const t = TEXT[name];
const err = errors[name];
return (
<div style={{ marginBottom: 16 }}>
<FieldLabel htmlFor={`fit-${name}`}>{t.column}</FieldLabel>
<TextInput
id={`fit-${name}`}
type={t.type || 'text'}
name={name}
required
autoComplete={t.autoComplete}
value={answers[name]}
invalid={!!err}
aria-invalid={!!err}
aria-describedby={err ? (err === REQUIRED ? 'fit-blanks' : `fit-${name}-error`) : undefined}
onChange={(ev) => set(name, ev.target.value)}
/>
{err && err !== REQUIRED && <ErrorText id={`fit-${name}-error`}>{err}</ErrorText>}
</div>
);
};

return (
<Card>
<Heading>Are we a fit? Check in a minute</Heading>
<Muted>We'll save your answers and your email so we can follow up.</Muted>
<form onSubmit={handleSubmit} noValidate>
{textField('org_name')}
{textField('org_website')}

{QUESTIONS.map((q) => (
<Fieldset
key={q.name}
Expand Down Expand Up @@ -182,20 +233,8 @@ export default function FitCheck({ scriptUrl }) {
</Fieldset>
))}

<FieldLabel htmlFor="fit-email">Your email</FieldLabel>
<TextInput
id="fit-email"
type="email"
name="email"
required
autoComplete="email"
value={answers.email}
invalid={!!errors.email}
aria-invalid={!!errors.email}
aria-describedby={errors.email ? (errors.email === REQUIRED ? 'fit-blanks' : 'fit-email-error') : undefined}
onChange={(ev) => set('email', ev.target.value)}
/>
{errors.email && errors.email !== REQUIRED && <ErrorText id="fit-email-error">{errors.email}</ErrorText>}
{textField('email')}
{textField('phone')}

<Honeypot
type="text"
Expand Down
1 change: 1 addition & 0 deletions src/components/cohort/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const LIGHT_BLUE = '#EAF0F7';
export const ERROR = '#B3261E';

export const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
export const PHONE_RE = /^\+?[\d\s()-]{8,}$/;

export const focusRing = `
outline: 3px solid ${BLUE};
Expand Down
Loading