|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import clsx from "clsx"; |
| 4 | +import { useOptimistic, useRef, useTransition } from "react"; |
| 5 | +import { saveFeature, upvote } from "./actions"; |
| 6 | +import { v4 as uuidv4 } from "uuid"; |
| 7 | +import { Feature } from "./types"; |
| 8 | + |
| 9 | +function Item({ |
| 10 | + isFirst, |
| 11 | + isLast, |
| 12 | + isReleased, |
| 13 | + hasVoted, |
| 14 | + feature, |
| 15 | + pending, |
| 16 | + mutate, |
| 17 | +}: { |
| 18 | + isFirst: boolean; |
| 19 | + isLast: boolean; |
| 20 | + isReleased: boolean; |
| 21 | + hasVoted: boolean; |
| 22 | + feature: Feature; |
| 23 | + pending: boolean; |
| 24 | + mutate: any; |
| 25 | +}) { |
| 26 | + let upvoteWithId = upvote.bind(null, feature); |
| 27 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 28 | + let [isPending, startTransition] = useTransition(); |
| 29 | + |
| 30 | + return ( |
| 31 | + <form |
| 32 | + action={upvoteWithId} |
| 33 | + onSubmit={(event) => { |
| 34 | + event.preventDefault(); |
| 35 | + |
| 36 | + startTransition(async () => { |
| 37 | + mutate({ |
| 38 | + updatedFeature: { |
| 39 | + ...feature, |
| 40 | + score: Number(feature.score) + 1, |
| 41 | + }, |
| 42 | + pending: true, |
| 43 | + }); |
| 44 | + await upvote(feature); |
| 45 | + }); |
| 46 | + }} |
| 47 | + className={clsx( |
| 48 | + "p-6 mx-8 flex items-center border-t border-l border-r", |
| 49 | + isFirst && "rounded-t-md", |
| 50 | + isLast && "border-b rounded-b-md", |
| 51 | + )} |
| 52 | + > |
| 53 | + <button |
| 54 | + className={clsx( |
| 55 | + "ring-1 ring-gray-200 rounded-full w-8 min-w-[2rem] h-8 mr-4 focus:outline-none focus:ring focus:ring-blue-300", |
| 56 | + (isReleased || hasVoted) && |
| 57 | + "bg-green-100 cursor-not-allowed ring-green-300", |
| 58 | + pending && "bg-gray-100 cursor-not-allowed", |
| 59 | + )} |
| 60 | + disabled={isReleased || hasVoted || pending} |
| 61 | + type="submit" |
| 62 | + > |
| 63 | + {isReleased ? "✅" : "👍"} |
| 64 | + </button> |
| 65 | + <h3 className="text font-semibold w-full text-left">{feature.title}</h3> |
| 66 | + <div className="bg-gray-200 text-gray-700 text-sm rounded-xl px-2 ml-2"> |
| 67 | + {feature.score} |
| 68 | + </div> |
| 69 | + </form> |
| 70 | + ); |
| 71 | +} |
| 72 | + |
| 73 | +type FeatureState = { |
| 74 | + newFeature: Feature; |
| 75 | + updatedFeature?: Feature; |
| 76 | + pending: boolean; |
| 77 | +}; |
| 78 | + |
| 79 | +export default function FeatureForm({ features }: { features: Feature[] }) { |
| 80 | + let formRef = useRef<HTMLFormElement>(null); |
| 81 | + let [state, mutate] = useOptimistic( |
| 82 | + { features, pending: false }, |
| 83 | + function createReducer(state, newState: FeatureState) { |
| 84 | + if (newState.newFeature) { |
| 85 | + return { |
| 86 | + features: [...state.features, newState.newFeature], |
| 87 | + pending: newState.pending, |
| 88 | + }; |
| 89 | + } else { |
| 90 | + return { |
| 91 | + features: [ |
| 92 | + ...state.features.filter( |
| 93 | + (f) => f.id !== newState.updatedFeature!.id, |
| 94 | + ), |
| 95 | + newState.updatedFeature, |
| 96 | + ] as Feature[], |
| 97 | + pending: newState.pending, |
| 98 | + }; |
| 99 | + } |
| 100 | + }, |
| 101 | + ); |
| 102 | + |
| 103 | + let sortedFeatures = state.features.sort((a, b) => { |
| 104 | + // First, compare by score in descending order |
| 105 | + if (Number(a.score) > Number(b.score)) return -1; |
| 106 | + if (Number(a.score) < Number(b.score)) return 1; |
| 107 | + |
| 108 | + // If scores are equal, then sort by created_at in ascending order |
| 109 | + return new Date(a.created_at).getTime() - new Date(b.created_at).getTime(); |
| 110 | + }); |
| 111 | + |
| 112 | + let featureStub = { |
| 113 | + id: uuidv4(), |
| 114 | + title: "", // will used value from form |
| 115 | + created_at: new Date().toISOString(), |
| 116 | + score: "1", |
| 117 | + }; |
| 118 | + let saveWithNewFeature = saveFeature.bind(null, featureStub); |
| 119 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 120 | + let [isPending, startTransition] = useTransition(); |
| 121 | + |
| 122 | + return ( |
| 123 | + <> |
| 124 | + <div className="mx-8 w-full"> |
| 125 | + <form |
| 126 | + className="relative my-8" |
| 127 | + ref={formRef} |
| 128 | + action={saveWithNewFeature} |
| 129 | + onSubmit={(event) => { |
| 130 | + event.preventDefault(); |
| 131 | + let formData = new FormData(event.currentTarget); |
| 132 | + let newFeature = { |
| 133 | + ...featureStub, |
| 134 | + title: formData.get("feature") as string, |
| 135 | + }; |
| 136 | + |
| 137 | + formRef.current?.reset(); |
| 138 | + startTransition(async () => { |
| 139 | + mutate({ |
| 140 | + newFeature, |
| 141 | + pending: true, |
| 142 | + }); |
| 143 | + |
| 144 | + await saveFeature(newFeature, formData); |
| 145 | + }); |
| 146 | + }} |
| 147 | + > |
| 148 | + <input |
| 149 | + aria-label="Suggest a feature for our roadmap" |
| 150 | + className="pl-3 pr-28 py-3 mt-1 text-lg block w-full border border-gray-200 rounded-md text-gray-900 placeholder-gray-400 focus:outline-none focus:ring focus:ring-blue-300" |
| 151 | + maxLength={150} |
| 152 | + placeholder="I want..." |
| 153 | + required |
| 154 | + type="text" |
| 155 | + name="feature" |
| 156 | + disabled={state.pending} |
| 157 | + /> |
| 158 | + <button |
| 159 | + className={clsx( |
| 160 | + "flex items-center justify-center absolute right-2 top-2 px-4 h-10 text-lg border bg-black text-white rounded-md w-24 focus:outline-none focus:ring focus:ring-blue-300 focus:bg-gray-800", |
| 161 | + state.pending && "bg-gray-700 cursor-not-allowed", |
| 162 | + )} |
| 163 | + type="submit" |
| 164 | + disabled={state.pending} |
| 165 | + > |
| 166 | + Request |
| 167 | + </button> |
| 168 | + </form> |
| 169 | + </div> |
| 170 | + <div className="w-full"> |
| 171 | + {sortedFeatures.map((feature: any, index: number) => ( |
| 172 | + <Item |
| 173 | + key={feature.id} |
| 174 | + isFirst={index === 0} |
| 175 | + isLast={index === sortedFeatures.length - 1} |
| 176 | + isReleased={false} |
| 177 | + hasVoted={false} |
| 178 | + feature={feature} |
| 179 | + pending={state.pending} |
| 180 | + mutate={mutate} |
| 181 | + /> |
| 182 | + ))} |
| 183 | + </div> |
| 184 | + </> |
| 185 | + ); |
| 186 | +} |
0 commit comments