|
1 | | -<h1>Welcome to SvelteKit</h1> |
2 | | -<p>Visit <a href="https://svelte.dev/docs/kit">svelte.dev/docs/kit</a> to read the documentation</p> |
| 1 | +<script lang="ts"> |
| 2 | + import axios from 'axios'; |
| 3 | +
|
| 4 | + let repo = { |
| 5 | + owner: '', |
| 6 | + name: '' |
| 7 | + }; |
| 8 | + let loading = false; |
| 9 | + let isRepoFetched = false; |
| 10 | + let repoData = null; |
| 11 | + let showModal = false; |
| 12 | +
|
| 13 | + async function handleSubmission(event: Event) { |
| 14 | + event.preventDefault(); |
| 15 | +
|
| 16 | + if (!repo.owner || !repo.name) { |
| 17 | + alert('Please provide both the repository owner and name.'); |
| 18 | + return; |
| 19 | + } |
| 20 | +
|
| 21 | + loading = true; |
| 22 | + isRepoFetched = false; |
| 23 | + repoData = null; |
| 24 | +
|
| 25 | + try { |
| 26 | + const response = await axios.get(`https://api.github.com/repos/${repo.owner}/${repo.name}`); |
| 27 | + repoData = response.data; |
| 28 | + isRepoFetched = true; |
| 29 | + showModal = true; |
| 30 | + } catch (error) { |
| 31 | + console.error('Error fetching repository:', error); |
| 32 | + alert(error.message); |
| 33 | + } finally { |
| 34 | + loading = false; |
| 35 | + } |
| 36 | + } |
| 37 | +
|
| 38 | + function closeModal() { |
| 39 | + showModal = false; |
| 40 | + } |
| 41 | +</script> |
| 42 | + |
| 43 | +{#if loading} |
| 44 | + <div class="absolute inset-0 z-10 flex items-center justify-center bg-gray-800 bg-opacity-50"> |
| 45 | + <div |
| 46 | + class="loader h-16 w-16 animate-spin rounded-full border-t-4 border-solid border-blue-500" |
| 47 | + ></div> |
| 48 | + </div> |
| 49 | +{/if} |
| 50 | + |
| 51 | +<div class="w-full max-w-md rounded-lg bg-white p-6 shadow-lg"> |
| 52 | + <h1 class="mb-4 text-2xl font-semibold text-gray-800">GitCalc</h1> |
| 53 | + <form on:submit={handleSubmission} class="space-y-4"> |
| 54 | + <div> |
| 55 | + <label class="mb-1 block text-sm font-medium text-gray-700"> Repository Owner </label> |
| 56 | + <input |
| 57 | + placeholder="GitHub repo's owner" |
| 58 | + bind:value={repo.owner} |
| 59 | + required |
| 60 | + class="w-full rounded-lg border px-4 py-2 text-gray-800 focus:outline-none focus:ring focus:ring-blue-300" |
| 61 | + /> |
| 62 | + </div> |
| 63 | + <div> |
| 64 | + <label class="mb-1 block text-sm font-medium text-gray-700"> Repository Name </label> |
| 65 | + <input |
| 66 | + placeholder="GitHub repo's name" |
| 67 | + bind:value={repo.name} |
| 68 | + required |
| 69 | + class="w-full rounded-lg border px-4 py-2 text-gray-800 focus:outline-none focus:ring focus:ring-blue-300" |
| 70 | + /> |
| 71 | + </div> |
| 72 | + <button |
| 73 | + type="submit" |
| 74 | + class="w-full rounded-lg bg-blue-500 px-4 py-2 font-medium text-white transition hover:bg-blue-600" |
| 75 | + disabled={loading} |
| 76 | + > |
| 77 | + {#if loading}Loading...{:else}Fetch Info{/if} |
| 78 | + </button> |
| 79 | + </form> |
| 80 | +</div> |
| 81 | + |
| 82 | +{#if showModal && isRepoFetched && repoData} |
| 83 | + <div class="fixed inset-0 z-20 flex items-center justify-center bg-gray-800 bg-opacity-50"> |
| 84 | + <div class="w-full max-w-3xl rounded-lg bg-white p-6 shadow-lg"> |
| 85 | + <h2 class="mb-4 text-xl font-bold text-gray-800">Repository Information</h2> |
| 86 | + <div class="space-y-4"> |
| 87 | + <div class="flex items-center space-x-2"> |
| 88 | + <i class="fas fa-user text-blue-500"></i> |
| 89 | + <span class="text-gray-700"><strong>Owner:</strong> {repoData.owner.login}</span> |
| 90 | + </div> |
| 91 | + <div class="flex items-center space-x-2"> |
| 92 | + <i class="fas fa-folder text-green-500"></i> |
| 93 | + <span class="text-gray-700"><strong>Repo Name:</strong> {repoData.name}</span> |
| 94 | + </div> |
| 95 | + <div class="flex items-center space-x-2"> |
| 96 | + <i class="fas fa-code-branch text-purple-500"></i> |
| 97 | + <span class="text-gray-700" |
| 98 | + ><strong>Default Branch:</strong> {repoData.default_branch}</span |
| 99 | + > |
| 100 | + </div> |
| 101 | + <div class="flex items-center space-x-2"> |
| 102 | + <i class="fas fa-code text-blue-500"></i> |
| 103 | + <span class="text-gray-700"><strong>Fork:</strong> {repoData.fork ? 'Yes' : 'No'}</span> |
| 104 | + </div> |
| 105 | + <div class="flex items-center space-x-2"> |
| 106 | + <i class="fas fa-globe text-green-500"></i> |
| 107 | + <span class="text-gray-700" |
| 108 | + ><strong>URL:</strong> |
| 109 | + <a href={repoData.html_url} target="_blank" class="text-blue-500 underline" |
| 110 | + >{repoData.html_url}</a |
| 111 | + > |
| 112 | + </span> |
| 113 | + </div> |
| 114 | + <div class="flex items-center space-x-2"> |
| 115 | + <i class="fas fa-calendar-alt text-yellow-500"></i> |
| 116 | + <span class="text-gray-700" |
| 117 | + ><strong>Created At:</strong> {new Date(repoData.created_at).toLocaleString()}</span |
| 118 | + > |
| 119 | + </div> |
| 120 | + <div class="flex items-center space-x-2"> |
| 121 | + <i class="fas fa-sync-alt text-purple-500"></i> |
| 122 | + <span class="text-gray-700" |
| 123 | + ><strong>Updated At:</strong> {new Date(repoData.updated_at).toLocaleString()}</span |
| 124 | + > |
| 125 | + </div> |
| 126 | + <div class="flex items-center space-x-2"> |
| 127 | + <i class="fas fa-star text-yellow-500"></i> |
| 128 | + <span class="text-gray-700"><strong>Stars:</strong> {repoData.stargazers_count}</span> |
| 129 | + </div> |
| 130 | + <div class="flex items-center space-x-2"> |
| 131 | + <i class="fas fa-star text-yellow-500"></i> |
| 132 | + <span class="text-gray-700"><strong>Size:</strong> {repoData.size}kb</span> |
| 133 | + </div> |
| 134 | + <div class="flex items-center space-x-2"> |
| 135 | + <i class="fas fa-eye text-blue-500"></i> |
| 136 | + <span class="text-gray-700"><strong>Watchers:</strong> {repoData.watchers_count}</span> |
| 137 | + </div> |
| 138 | + <div class="flex items-center space-x-2"> |
| 139 | + <i class="fas fa-info-circle text-red-500"></i> |
| 140 | + <span class="text-gray-700"><strong>Description:</strong> {repoData.description}</span> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + <button class="mt-4 w-full rounded-lg bg-red-500 py-2 text-white" on:click={closeModal}> |
| 144 | + Close |
| 145 | + </button> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | +{/if} |
0 commit comments