refactor: enhance error handling and improve component structure in cadastro page
- Updated error handling to provide clearer messages by casting errors to the Error type. - Cleaned up imports and improved breadcrumb navigation using the resolve function for better path management. - Streamlined the rendering of options in select elements by adding unique keys for better performance. - Enhanced the mapping of document categories and symbols for improved readability and maintainability.
This commit is contained in:
@@ -24,7 +24,8 @@
|
|||||||
} from '$lib/utils/constants';
|
} from '$lib/utils/constants';
|
||||||
import { categoriasDocumentos, getDocumentosByCategoria } from '$lib/utils/documentos';
|
import { categoriasDocumentos, getDocumentosByCategoria } from '$lib/utils/documentos';
|
||||||
import ModelosDeclaracoes from '$lib/components/ModelosDeclaracoes.svelte';
|
import ModelosDeclaracoes from '$lib/components/ModelosDeclaracoes.svelte';
|
||||||
import { Trash, X } from 'lucide-svelte';
|
import { Trash } from 'lucide-svelte';
|
||||||
|
import { resolve } from '$app/paths';
|
||||||
|
|
||||||
const client = useConvexClient();
|
const client = useConvexClient();
|
||||||
|
|
||||||
@@ -236,8 +237,8 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function loadSimbolos() {
|
async function loadSimbolos() {
|
||||||
const list = await client.query(api.simbolos.getAll, {} as any);
|
const list = await client.query(api.simbolos.getAll, {});
|
||||||
simbolos = list.map((s: any) => ({
|
simbolos = list.map((s) => ({
|
||||||
_id: s._id,
|
_id: s._id,
|
||||||
nome: s.nome,
|
nome: s.nome,
|
||||||
tipo: s.tipo,
|
tipo: s.tipo,
|
||||||
@@ -275,8 +276,9 @@
|
|||||||
|
|
||||||
const { storageId } = await result.json();
|
const { storageId } = await result.json();
|
||||||
documentosStorage[campo] = storageId;
|
documentosStorage[campo] = storageId;
|
||||||
} catch (err: any) {
|
} catch (err: unknown) {
|
||||||
throw new Error(err?.message || 'Erro ao fazer upload');
|
const error = err as Error;
|
||||||
|
throw new Error(error.message || 'Erro ao fazer upload');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,7 +323,7 @@
|
|||||||
cep: onlyDigits(cep),
|
cep: onlyDigits(cep),
|
||||||
cidade,
|
cidade,
|
||||||
uf: uf.toUpperCase(),
|
uf: uf.toUpperCase(),
|
||||||
simboloId: simboloId as any,
|
simboloId: simboloId,
|
||||||
simboloTipo: tipo,
|
simboloTipo: tipo,
|
||||||
admissaoData: admissaoData || undefined,
|
admissaoData: admissaoData || undefined,
|
||||||
desligamentoData: undefined,
|
desligamentoData: undefined,
|
||||||
@@ -410,9 +412,10 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
notice = { kind: 'success', text: 'Funcionário cadastrado com sucesso!' };
|
notice = { kind: 'success', text: 'Funcionário cadastrado com sucesso!' };
|
||||||
setTimeout(() => goto('/recursos-humanos/funcionarios'), 600);
|
setTimeout(() => goto(resolve('/recursos-humanos/funcionarios')), 600);
|
||||||
} catch (e: any) {
|
} catch (e: unknown) {
|
||||||
const msg = e?.message || String(e);
|
const error = e as Error;
|
||||||
|
const msg = error.message || String(error);
|
||||||
if (/CPF j[aá] cadastrado/i.test(msg)) {
|
if (/CPF j[aá] cadastrado/i.test(msg)) {
|
||||||
notice = { kind: 'error', text: 'CPF já cadastrado.' };
|
notice = { kind: 'error', text: 'CPF já cadastrado.' };
|
||||||
} else if (/Matr[ií]cula j[aá] cadastrada/i.test(msg)) {
|
} else if (/Matr[ií]cula j[aá] cadastrada/i.test(msg)) {
|
||||||
@@ -440,9 +443,13 @@
|
|||||||
<!-- Breadcrumb -->
|
<!-- Breadcrumb -->
|
||||||
<div class="breadcrumbs mb-4 text-sm">
|
<div class="breadcrumbs mb-4 text-sm">
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="/recursos-humanos" class="text-primary hover:underline">Recursos Humanos</a></li>
|
|
||||||
<li>
|
<li>
|
||||||
<a href="/recursos-humanos/funcionarios" class="text-primary hover:underline"
|
<a href={resolve('/recursos-humanos')} class="text-primary hover:underline"
|
||||||
|
>Recursos Humanos</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
<li>
|
||||||
|
<a href={resolve('/recursos-humanos/funcionarios')} class="text-primary hover:underline"
|
||||||
>Funcionários</a
|
>Funcionários</a
|
||||||
>
|
>
|
||||||
</li>
|
</li>
|
||||||
@@ -947,7 +954,7 @@
|
|||||||
bind:value={grauInstrucao}
|
bind:value={grauInstrucao}
|
||||||
>
|
>
|
||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{#each GRAU_INSTRUCAO_OPTIONS as option}
|
{#each GRAU_INSTRUCAO_OPTIONS as option (option.value)}
|
||||||
<option value={option.value}>{option.label}</option>
|
<option value={option.value}>{option.label}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -992,7 +999,7 @@
|
|||||||
bind:value={grupoSanguineo}
|
bind:value={grupoSanguineo}
|
||||||
>
|
>
|
||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{#each GRUPO_SANGUINEO_OPTIONS as option}
|
{#each GRUPO_SANGUINEO_OPTIONS as option (option.value)}
|
||||||
<option value={option.value}>{option.label}</option>
|
<option value={option.value}>{option.label}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -1005,7 +1012,7 @@
|
|||||||
</label>
|
</label>
|
||||||
<select id="fatorRH" class="select select-bordered w-full" bind:value={fatorRH}>
|
<select id="fatorRH" class="select select-bordered w-full" bind:value={fatorRH}>
|
||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{#each FATOR_RH_OPTIONS as option}
|
{#each FATOR_RH_OPTIONS as option (option.value)}
|
||||||
<option value={option.value}>{option.label}</option>
|
<option value={option.value}>{option.label}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -1043,7 +1050,7 @@
|
|||||||
{#if cursos.length > 0}
|
{#if cursos.length > 0}
|
||||||
<div class="space-y-2">
|
<div class="space-y-2">
|
||||||
<h3 class="text-sm font-semibold">Cursos adicionados ({cursos.length}/7)</h3>
|
<h3 class="text-sm font-semibold">Cursos adicionados ({cursos.length}/7)</h3>
|
||||||
{#each cursos as curso}
|
{#each cursos as curso (curso.id)}
|
||||||
<div class="bg-base-200 flex items-center gap-3 rounded-lg p-3">
|
<div class="bg-base-200 flex items-center gap-3 rounded-lg p-3">
|
||||||
<div class="flex-1">
|
<div class="flex-1">
|
||||||
<p class="text-sm font-semibold">{curso.descricao}</p>
|
<p class="text-sm font-semibold">{curso.descricao}</p>
|
||||||
@@ -1240,7 +1247,7 @@
|
|||||||
</label>
|
</label>
|
||||||
<select id="uf" class="select select-bordered w-full" bind:value={uf} required>
|
<select id="uf" class="select select-bordered w-full" bind:value={uf} required>
|
||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{#each UFS_BRASIL as ufOption}
|
{#each UFS_BRASIL as ufOption (ufOption)}
|
||||||
<option value={ufOption}>{ufOption}</option>
|
<option value={ufOption}>{ufOption}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -1560,7 +1567,7 @@
|
|||||||
required
|
required
|
||||||
>
|
>
|
||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{#each simbolos.filter((s) => s.tipo === tipo) as s}
|
{#each simbolos.filter((s) => s.tipo === tipo) as s (s._id)}
|
||||||
<option value={s._id}>{s.nome} — {s.descricao}</option>
|
<option value={s._id}>{s.nome} — {s.descricao}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -1688,7 +1695,7 @@
|
|||||||
<span class="label-text font-medium">Se Aposentado</span>
|
<span class="label-text font-medium">Se Aposentado</span>
|
||||||
</label>
|
</label>
|
||||||
<select id="aposentado" class="select select-bordered w-full" bind:value={aposentado}>
|
<select id="aposentado" class="select select-bordered w-full" bind:value={aposentado}>
|
||||||
{#each APOSENTADO_OPTIONS as option}
|
{#each APOSENTADO_OPTIONS as option (option.value)}
|
||||||
<option value={option.value}>{option.label}</option>
|
<option value={option.value}>{option.label}</option>
|
||||||
{/each}
|
{/each}
|
||||||
</select>
|
</select>
|
||||||
@@ -1792,11 +1799,11 @@
|
|||||||
Anexe os documentos necessários em formato PDF ou imagem (máximo 10MB cada)
|
Anexe os documentos necessários em formato PDF ou imagem (máximo 10MB cada)
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
{#each categoriasDocumentos as categoria}
|
{#each categoriasDocumentos as categoria (categoria)}
|
||||||
<div class="space-y-3">
|
<div class="space-y-3">
|
||||||
<h3 class="text-primary text-lg font-semibold">{categoria}</h3>
|
<h3 class="text-primary text-lg font-semibold">{categoria}</h3>
|
||||||
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
<div class="grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||||
{#each getDocumentosByCategoria(categoria) as doc}
|
{#each getDocumentosByCategoria(categoria) as doc (doc.campo)}
|
||||||
<FileUpload
|
<FileUpload
|
||||||
label={doc.nome}
|
label={doc.nome}
|
||||||
helpUrl={doc.helpUrl}
|
helpUrl={doc.helpUrl}
|
||||||
@@ -1820,7 +1827,7 @@
|
|||||||
type="button"
|
type="button"
|
||||||
class="btn btn-lg"
|
class="btn btn-lg"
|
||||||
disabled={loading}
|
disabled={loading}
|
||||||
onclick={() => goto('/recursos-humanos/funcionarios')}
|
onclick={() => goto(resolve('/recursos-humanos/funcionarios'))}
|
||||||
>
|
>
|
||||||
<svg
|
<svg
|
||||||
xmlns="http://www.w3.org/2000/svg"
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
|||||||
Reference in New Issue
Block a user