// Hosting-flyt: pakke først (beslutningen kunden kom for å ta),
// deretter domene (nytt / flytt / eget), så rett i handlevognen.
import React, { useState, useMemo } from 'react';
const NS = window.STWORDER;
const DOMAIN_RE = /^([a-z0-9åøæ-]+\.)+[a-zæøå]{2,}$/i;
function CycleOption({ active, onClick, title, price, note }) {
return (
);
}
function DomainModeOption({ active, onClick, title, children }) {
return (
{active && children ?
{children}
: null}
);
}
function Configurator({ product }) {
const { useI18n, nok, Btn, StepHeading } = NS.ui;
const { t } = useI18n();
const DomainPicker = NS.flows.DomainPicker;
const [cycle, setCycle] = useState('annually');
const [mode, setMode] = useState('new');
const [newDomain, setNewDomain] = useState(null); // {name, price}
const [transferName, setTransferName] = useState('');
const [ownName, setOwnName] = useState('');
const transferPriceFor = (name) => {
const tld = name.slice(name.lastIndexOf('.') + 1).toLowerCase();
const row = NS.data.tlds.find((r) => r.tld === tld);
return row ? row.transfer : 0;
};
const domainOk =
mode === 'new' ? !!newDomain
: mode === 'transfer' ? DOMAIN_RE.test(transferName.trim())
: DOMAIN_RE.test(ownName.trim());
const cyclePrice = NS.store.cyclePrice(product.monthly, cycle);
const annualSaving = product.monthly * 2;
const due =
cyclePrice +
(mode === 'new' && newDomain ? newDomain.price : 0) +
(mode === 'transfer' && domainOk ? transferPriceFor(transferName.trim()) : 0);
function add() {
const domain =
mode === 'new'
? { mode: 'new', name: newDomain.name, price: newDomain.price, years: 1 }
: mode === 'transfer'
? { mode: 'transfer', name: transferName.trim().toLowerCase(), price: transferPriceFor(transferName.trim()) }
: { mode: 'own', name: ownName.trim().toLowerCase() };
NS.store.add({
type: 'product',
productId: product.id,
name: product.name,
group: product.group,
cycle,
monthly: product.monthly,
domain,
});
NS.bus.emit('cart:open');
}
return (
{t('hosting.step2')} – {product.name}
{t('hosting.cycle')}
setCycle('monthly')}
title={t('hosting.cycleMonthly')}
price={nok(product.monthly) + t('common.perMonth')}
/>
setCycle('annually')}
title={t('hosting.cycleAnnually')}
price={nok(product.monthly * 10) + t('common.perYear')}
note={t('hosting.save', { amount: nok(annualSaving) })}
/>
{t('hosting.domainQ')}
{t('cart.dueToday')}
{nok(due)}
{t('common.addToCart')} →
);
}
function HostingFlow() {
const { useI18n, nok, ProductCard, StepHeading, PageHero, localName } = NS.ui;
const { t, lang } = useI18n();
const [group, setGroup] = useState('webhotell');
const [selId, setSelId] = useState(null);
const products = NS.data.products.filter((p) => p.group === group);
const selected = NS.data.products.find((p) => p.id === selId) || null;
return (
{NS.data.productGroups.map((g) => (
))}
}
>
{t('hosting.step1')}
{products.map((p) => (
setSelId(p.id)}
selected={selId === p.id}
/>
))}
{selected ?
: null}
);
}
NS.flows = NS.flows || {};
NS.flows.Hosting = HostingFlow;