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';
|
||||
import { categoriasDocumentos, getDocumentosByCategoria } from '$lib/utils/documentos';
|
||||
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();
|
||||
|
||||
@@ -236,8 +237,8 @@
|
||||
}
|
||||
|
||||
async function loadSimbolos() {
|
||||
const list = await client.query(api.simbolos.getAll, {} as any);
|
||||
simbolos = list.map((s: any) => ({
|
||||
const list = await client.query(api.simbolos.getAll, {});
|
||||
simbolos = list.map((s) => ({
|
||||
_id: s._id,
|
||||
nome: s.nome,
|
||||
tipo: s.tipo,
|
||||
@@ -275,8 +276,9 @@
|
||||
|
||||
const { storageId } = await result.json();
|
||||
documentosStorage[campo] = storageId;
|
||||
} catch (err: any) {
|
||||
throw new Error(err?.message || 'Erro ao fazer upload');
|
||||
} catch (err: unknown) {
|
||||
const error = err as Error;
|
||||
throw new Error(error.message || 'Erro ao fazer upload');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -321,7 +323,7 @@
|
||||
cep: onlyDigits(cep),
|
||||
cidade,
|
||||
uf: uf.toUpperCase(),
|
||||
simboloId: simboloId as any,
|
||||
simboloId: simboloId,
|
||||
simboloTipo: tipo,
|
||||
admissaoData: admissaoData || undefined,
|
||||
desligamentoData: undefined,
|
||||
@@ -410,9 +412,10 @@
|
||||
}
|
||||
|
||||
notice = { kind: 'success', text: 'Funcionário cadastrado com sucesso!' };
|
||||
setTimeout(() => goto('/recursos-humanos/funcionarios'), 600);
|
||||
} catch (e: any) {
|
||||
const msg = e?.message || String(e);
|
||||
setTimeout(() => goto(resolve('/recursos-humanos/funcionarios')), 600);
|
||||
} catch (e: unknown) {
|
||||
const error = e as Error;
|
||||
const msg = error.message || String(error);
|
||||
if (/CPF j[aá] cadastrado/i.test(msg)) {
|
||||
notice = { kind: 'error', text: 'CPF já cadastrado.' };
|
||||
} else if (/Matr[ií]cula j[aá] cadastrada/i.test(msg)) {
|
||||
@@ -440,9 +443,13 @@
|
||||
<!-- Breadcrumb -->
|
||||
<div class="breadcrumbs mb-4 text-sm">
|
||||
<ul>
|
||||
<li><a href="/recursos-humanos" class="text-primary hover:underline">Recursos Humanos</a></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
|
||||
>
|
||||
</li>
|
||||
@@ -947,7 +954,7 @@
|
||||
bind:value={grauInstrucao}
|
||||
>
|
||||
<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>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -992,7 +999,7 @@
|
||||
bind:value={grupoSanguineo}
|
||||
>
|
||||
<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>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -1005,7 +1012,7 @@
|
||||
</label>
|
||||
<select id="fatorRH" class="select select-bordered w-full" bind:value={fatorRH}>
|
||||
<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>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -1043,7 +1050,7 @@
|
||||
{#if cursos.length > 0}
|
||||
<div class="space-y-2">
|
||||
<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="flex-1">
|
||||
<p class="text-sm font-semibold">{curso.descricao}</p>
|
||||
@@ -1240,7 +1247,7 @@
|
||||
</label>
|
||||
<select id="uf" class="select select-bordered w-full" bind:value={uf} required>
|
||||
<option value="">Selecione...</option>
|
||||
{#each UFS_BRASIL as ufOption}
|
||||
{#each UFS_BRASIL as ufOption (ufOption)}
|
||||
<option value={ufOption}>{ufOption}</option>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -1560,7 +1567,7 @@
|
||||
required
|
||||
>
|
||||
<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>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -1688,7 +1695,7 @@
|
||||
<span class="label-text font-medium">Se Aposentado</span>
|
||||
</label>
|
||||
<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>
|
||||
{/each}
|
||||
</select>
|
||||
@@ -1792,11 +1799,11 @@
|
||||
Anexe os documentos necessários em formato PDF ou imagem (máximo 10MB cada)
|
||||
</p>
|
||||
|
||||
{#each categoriasDocumentos as categoria}
|
||||
{#each categoriasDocumentos as categoria (categoria)}
|
||||
<div class="space-y-3">
|
||||
<h3 class="text-primary text-lg font-semibold">{categoria}</h3>
|
||||
<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
|
||||
label={doc.nome}
|
||||
helpUrl={doc.helpUrl}
|
||||
@@ -1820,7 +1827,7 @@
|
||||
type="button"
|
||||
class="btn btn-lg"
|
||||
disabled={loading}
|
||||
onclick={() => goto('/recursos-humanos/funcionarios')}
|
||||
onclick={() => goto(resolve('/recursos-humanos/funcionarios'))}
|
||||
>
|
||||
<svg
|
||||
xmlns="http://www.w3.org/2000/svg"
|
||||
|
||||
Reference in New Issue
Block a user